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

# 3. Chat Model Basic Conversation

> Structure conversations using System, Human, and AI messages

In this section, you will learn how to structure multi-turn conversations using LangChain's message schemas.

## Objectives

1. Structure conversations with `SystemMessage`, `HumanMessage`, and `AIMessage`.
2. Prime model behavior with System messages.
3. Simulate multi-turn dialogue memory.

## Understanding Chat Message Roles

When building multi-turn conversations, LLMs require messages to be structured with specific roles. LangChain provides specialized API classes under `langchain_core.messages` to handle these roles:

* **`SystemMessage`**: Sets the persona, behavior, constraints, and instructions for the AI model (e.g., "You are a helpful French translation assistant").
* **`HumanMessage`**: Represents the queries or prompts sent directly by the user.
* **`AIMessage`**: Represents the responses returned by the AI model. In multi-turn chat logs, previous AI responses are passed back to the model as `AIMessage` instances to simulate context memory.

## Conversation Structure

#### Goal

Instruct the model to act as a math solver and provide the history of a previous calculation.

#### Sample Input

Sequential messages:

1. `SystemMessage(content="Solve the following math problems")`
2. `HumanMessage(content="What is 81 divided by 9?")`
3. `AIMessage(content="81 divided by 9 is 9.")`
4. `HumanMessage(content="What is 10 times 5?")`

#### Sample Output

```python theme={null}
"10 times 5 is 50."
```

#### Plan

1. Import `SystemMessage`, `HumanMessage`, and `AIMessage`.
2. Package messages in a list representing the chat conversation history.
3. Pass the list to `model.invoke()` and print the response.

### Code Implementation

#### 1. Setup and Invoke

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

load_dotenv()

# Initialize model using core abstractions (Google Gemini)
model = init_chat_model("gemini-2.5-flash", model_provider="google_genai")

# Package conversation history
messages = [
    SystemMessage(content="Solve the following math problems"),
    HumanMessage(content="What is 81 divided by 9?"),
    AIMessage(content="81 divided by 9 is 9."),
    HumanMessage(content="What is 10 times 5?"),
]

result = model.invoke(messages)
print(f"Answer from AI: {result.content}")
```

## Exercise: Translation Memory 🌍

#### Goal

Build a conversation history list where the user instructs the model to translate words to French, translates one word, and then queries a second word.

#### Sample Input

1. System message: "Translate words to French."
2. Human message: "Cat" -> AI: "Chat"
3. Human message: "Dog"

#### Sample Output

```python theme={null}
"Chien"
```

#### Plan

1. Form a conversation list using `SystemMessage`, `HumanMessage`, and `AIMessage`.
2. Call `model.invoke()` with the list using a Groq-provided model (`llama-3.3-70b-versatile`) initialized via core abstractions and print the output.

<Accordion title="Solution">
  ```python theme={null}
  from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
  from langchain.chat_models import init_chat_model
  from dotenv import load_dotenv

  load_dotenv()

  # Initialize model using core abstractions (Groq Llama model)
  model = init_chat_model("llama-3.3-70b-versatile", model_provider="groq")

  messages = [
      SystemMessage(content="You are a French translator."),
      HumanMessage(content="Cat"),
      AIMessage(content="Chat"),
      HumanMessage(content="Dog")
  ]
  res = model.invoke(messages)
  print(res.content)
  ```
</Accordion>

## Practice & Exercises

To practice, open the interactive notebook:

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Practice structuring conversational message flows.

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