> ## Documentation Index
> Fetch the complete documentation index at: https://genai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat Models

> Learn how to interact with chat models in LangChain and compare with direct APIs

In this section, you will learn how to initialize and invoke LLM Chat Models using LangChain.

## Objectives

1. Understand the differences between raw API integrations and LangChain.
2. Swapping model providers seamlessly using standard message formatting.

## Direct APIs vs. LangChain

Directly writing code against raw LLM provider APIs (like OpenAI, Google, or Anthropic) introduces API fragmentation. Every model provider has its own proprietary SDK, request payload structure, and response format.

### 1. Direct Provider APIs (Fragmentation)

Every provider requires a unique SDK, setup protocol, and response extraction syntax:

##### OpenAI Direct API

```python theme={null}
from openai import OpenAI
client = OpenAI(api_key="your_openai_key")

res = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "What is Python?"}]
)
# Accessing content requires deep nesting:
print(res.choices[0].message.content)
```

##### Google Gemini Direct API

```python theme={null}
import google.generativeai as genai
genai.configure(api_key="your_gemini_key")
model = genai.GenerativeModel("gemini-2.5-flash")

res = model.generate_content("What is Python?")
# Accessing content uses .text:
print(res.text)
```

##### Hugging Face Inference API

```python theme={null}
import requests
API_URL = "https://api-inference.huggingface.co/models/gpt2"
headers = {"Authorization": "Bearer your_hf_token"}

res = requests.post(API_URL, headers=headers, json={"inputs": "What is Python?"})
# Accessing content requires list/dictionary parsing:
print(res.json()[0]['generated_text'])
```

### 2. LangChain's Simplified & Unified Syntax

LangChain unifies all these disparate APIs behind a single interface. Switching between providers only requires changing model configuration variables:

```python theme={null}
from langchain.chat_models import init_chat_model

# Standardized Initialization:
llm = init_chat_model("gemini-2.5-flash", model_provider="google_genai")
# To switch to OpenAI: llm = init_chat_model("gpt-4o", model_provider="openai")
# To switch to Groq: llm = init_chat_model("llama-3.3-70b-versatile", model_provider="groq")

# Standardized Invocation & Response Extraction (.content)
response = llm.invoke("What is Python?")
print(response.content)
```

## Next Steps: What We Will Build

Now that you understand the advantages of LangChain's unified core abstractions, we will implement and analyze five progressively advanced chat model configurations:

1. **Chat Model Basic**: Set up and run your first model invocation using unified abstractions.
2. **Chat Model Basic Conversation**: Format message arrays with specific system instructions and structured message roles.
3. **Chat Model Alternatives**: Swap between OpenAI, Anthropic, Google, and Groq providers effortlessly.
4. **Chat Model Conversation With User**: Create an interactive command-line chat session with live memory.
5. **Chat Model Save Message History**: Persist your conversation sessions securely inside a Google Cloud Firestore database.

## Practice & Exercises

To reinforce what you've learned in this section, practice with the interactive notebook:

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Practice initializing chat models, building conversations, and managing chat message structures.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/GenAI%20With%20Python/public/notebooks/langchain/chat_models_intro.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/genai-course/blob/master/public/notebooks/langchain/chat_models_intro.ipynb) | <a href="/public/notebooks/langchain/chat_models_intro.ipynb" download>📥 Download</a>
  </Card>
</CardGroup>
