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

# 1. Chat Model Basic

> Invoke a basic chat model using LangChain core abstractions

In this section, you will learn the absolute basics of initializing and invoking a LangChain chat model.

## Objectives

1. Understand the difference between using proprietary wrappers and core abstractions.
2. Develop using both approaches to see how they differ in code.
3. Adopt core abstractions for our implementations.
4. Practice invoking various provider APIs in a uniform manner.

## Chat Model Basic

#### Goal

Create a model instance using core abstractions and query it for the result of a simple math calculation.

#### Sample Input

```python theme={null}
"What is 81 divided by 9?"
```

#### Sample Output

```python theme={null}
"81 divided by 9 is 9."
```

#### Plan & Code Implementation

##### Method 1: Using Core Abstraction Helper (Recommended)

**Plan:**

1. Import the core abstraction helper `init_chat_model` from `langchain.chat_models`.
2. Initialize the model using `init_chat_model("gpt-4o", model_provider="openai")`.
3. Call `model.invoke()` with the input string and print the response.

**Code Implementation:**

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

load_dotenv()

# Initialize using the core abstraction helper
model = init_chat_model("gpt-4o", model_provider="openai")

# Invoke the model
result = model.invoke("What is 81 divided by 9?")
print("Full result:")
print(result)
print("Content only:")
print(result.content)
```

##### Method 2: Using Proprietary Wrapper Class

**Plan:**

1. Import the proprietary model wrapper `ChatOpenAI` from `langchain_openai`.
2. Initialize the model using `ChatOpenAI(model="gpt-4o")`.
3. Call `model.invoke()` with the input string and print the response.

**Code Implementation:**

```python theme={null}
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

load_dotenv()

# Initialize using the proprietary class
model = ChatOpenAI(model="gpt-4o")

# Invoke the model
result = model.invoke("What is 81 divided by 9?")
print("Full result:")
print(result)
print("Content only:")
print(result.content)
```

## Proprietary Wrappers vs. Core Abstractions

As shown above, we used the unified core abstraction helper `init_chat_model`. Let's compare this with proprietary wrappers:

* **Proprietary Wrappers**: Using specific provider classes (e.g., `ChatOpenAI` from `langchain_openai`).
  * **Advantages**: Direct access to unique provider features and parameter optimizations.
  * **Disadvantages**: High code coupling; swapping providers requires refactoring imports and classes across your codebase.
  * **Example**:
    ```python theme={null}
    from langchain_openai import ChatOpenAI
    model = ChatOpenAI(model="gpt-4o")
    ```
* **Core Abstractions**: Using the unified initializer helper (`init_chat_model` from `langchain.chat_models`).
  * **Advantages**: Standardized interface; swap providers simply by changing parameter strings.
  * **Disadvantages**: Advanced provider-specific parameters must be passed indirectly via `model_kwargs`.
  * **Example**:
    ```python theme={null}
    from langchain.chat_models import init_chat_model
    model = init_chat_model("gpt-4o", model_provider="openai")
    ```

> \[!IMPORTANT]
> To ensure maximum flexibility and avoid having to remember different SDK endpoints, imports, and API conventions, **we will adopt Core Abstractions (`init_chat_model`) throughout this module** to practice invoking various provider APIs in a uniform manner.

## Shifting Providers Seamlessly (Gemini and Groq)

Because we use core abstractions, shifting our implementation to another provider requires zero changes to imports. We can swap the model provider simply by updating parameter strings:

##### 1. Shifting to Google Gemini

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

load_dotenv()

# Simply change the parameters using core abstractions
model = init_chat_model("gemini-2.5-flash", model_provider="google_genai")

result = model.invoke("What is 81 divided by 9?")
print(result.content)
```

##### 2. Shifting to Groq

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

load_dotenv()

# Simply change the parameters using core abstractions
model = init_chat_model("llama-3.3-70b-versatile", model_provider="groq")

result = model.invoke("What is 81 divided by 9?")
print(result.content)
```

## Exercise: Capital City Agent 🏗

#### Goal

Build a basic prompt program that queries the chat model to find the capital of any given country.

#### Sample Input

```python theme={null}
"What is the capital of India?"
```

#### Sample Output

```python theme={null}
"The capital of India is New Delhi."
```

#### Plan

1. Use `init_chat_model` to load the OpenAI model.
2. Invoke the model with a string querying the capital of India.
3. Print the result content.

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

  load_dotenv()
  model = init_chat_model("gpt-4o", model_provider="openai")
  res = model.invoke("What is the capital of India?")
  print(res.content)
  ```
</Accordion>

## Practice & Exercises

To practice, open the interactive notebook:

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Practice initializing and running basic chat model queries.

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