> ## 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.

# 2. Chat Model Swapping

> Swap between OpenAI, Anthropic, Google, and Groq chat models in LangChain

In this section, you will learn how to switch between different LLM providers using LangChain's unified core abstractions.

## Objectives

1. Configure Groq, Google Gemini, Anthropic, and OpenAI APIs.
2. Initialize models using `init_chat_model` with multiple provider strings.
3. Reuse the same list of messages across different models without rewriting logic.

## Initializing and Calling Alternative Models

Here is how to configure and invoke different model providers using LangChain's core abstractions.

#### 1. Initializing with Groq

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

load_dotenv()

# Initialize the Groq model
llm = init_chat_model(
    "llama-3.3-70b-versatile",
    model_provider="groq"
)

response = llm.invoke("Explain why developers use virtual environments in Python.")
print(response.content)
```

#### 2. Initializing with Google Gemini

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

load_dotenv()

# Initialize the Gemini model
llm = init_chat_model(
    "gemini-2.5-flash",
    model_provider="google_genai"
)

response = llm.invoke("What is the difference between concurrency and parallelism?")
print(response.content)
```

> \[!NOTE]
> When using `init_chat_model`, LangChain automatically detects the `GROQ_API_KEY` or `GOOGLE_API_KEY` from your environment variables.

## Swap Providers

#### Goal

Query the same math problem to OpenAI, Anthropic, and Google models.

#### Sample Input

```python theme={null}
messages = [
    SystemMessage(content="Solve the following math problems"),
    HumanMessage(content="What is 81 divided by 9?"),
]
```

#### Sample Output

Outputs matching standard math solving from all three API providers.

#### Plan

1. Import `init_chat_model` from `langchain.chat_models`.
2. Initialize each model using the unified initializer.
3. Call `invoke()` on each model using the same messages list and print results.

### Code Implementation

```python theme={null}
from dotenv import load_dotenv
from langchain_core.messages import HumanMessage, SystemMessage
from langchain.chat_models import init_chat_model

load_dotenv()

messages = [
    SystemMessage(content="Solve the following math problems"),
    HumanMessage(content="What is 81 divided by 9?"),
]

# 1. OpenAI
openai_model = init_chat_model("gpt-4o", model_provider="openai")
print(f"OpenAI: {openai_model.invoke(messages).content}")

# 2. Anthropic
anthropic_model = init_chat_model("claude-3-opus-20240229", model_provider="anthropic")
print(f"Anthropic: {anthropic_model.invoke(messages).content}")

# 3. Google
google_model = init_chat_model("gemini-1.5-flash", model_provider="google_genai")
print(f"Google: {google_model.invoke(messages).content}")
```

## Exercise: Multi-Provider Greeting Agent 🤝

#### Goal

Initialize OpenAI and Google models and verify how they respond to a simple creative greeting message.

#### Sample Input

```python theme={null}
"Write a creative 1-sentence hello greeting."
```

#### Sample Output

Greetings from both OpenAI and Google models.

#### Plan

1. Initialize both OpenAI and Google models using `init_chat_model`.
2. Call `invoke()` with the greeting query and print the responses side-by-side.

<Accordion title="Solution">
  ```python theme={null}
  from langchain.chat_models import init_chat_model
  from dotenv import load_dotenv

  load_dotenv()
  openai = init_chat_model("gpt-4o", model_provider="openai")
  google = init_chat_model("gemini-1.5-flash", model_provider="google_genai")

  query = "Write a creative 1-sentence hello greeting."
  print("OpenAI:", openai.invoke(query).content)
  print("Google:", google.invoke(query).content)
  ```
</Accordion>

## Practice & Exercises

To practice, open the interactive notebook:

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Practice configuring multiple API models and running unified message formats.

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