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

# 4. Chat Model Conversations

> Build an interactive terminal chat loop with state history

In this section, you will learn how to build an interactive command-line interface chatbot that remembers previous inputs in a loop.

## Objectives

1. Setup an interactive command-line chat loop.
2. Accumulate user queries and AI responses in a shared history list.

## User Chat Loop

#### Goal

Establish a live conversation session in the terminal where the AI remembers previous statements.

#### Sample Input

Sequential console inputs:

1. `User: Hello, I am Satish.`
2. `User: What is my name?`

#### Sample Output

1. `AI: Hello Satish! How can I help you today?`
2. `AI: Your name is Satish.`

#### Plan

1. Initialize model using the core abstraction helper `init_chat_model("llama-3.3-70b-versatile", model_provider="groq")`.
2. Initialize `chat_history = [SystemMessage(content="You are a helpful AI assistant.")]`.
3. Create a `while True` loop to take user console input, append `HumanMessage`, invoke model on history, append `AIMessage`, and print the response.

### Code Implementation

#### 1. Implement Chat Loop

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

load_dotenv()

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

chat_history = []
chat_history.append(SystemMessage(content="You are a helpful AI assistant."))

while True:
    query = input("You: ")
    if query.lower() == "exit":
        break
    chat_history.append(HumanMessage(content=query))

    result = model.invoke(chat_history)
    response = result.content
    chat_history.append(AIMessage(content=response))

    print(f"AI: {response}")

print("---- Message History ----")
print(chat_history)
```

## Exercise: Strict Math Helper Bot 🧮

#### Goal

Modify the chat loop so the agent only answers math questions. If a query is not math-related, the agent should refuse politely.

#### Sample Input

Sequential console inputs:

1. `User: What is 5 + 5?`
2. `User: Who is the president of USA?`

#### Sample Output

1. `AI: 5 + 5 is 10.`
2. `AI: I am sorry, but I can only answer math-related questions.`

#### Plan

1. Adjust the initial `SystemMessage` content to instruct the model to only solve math questions and refuse other topics.
2. Run the loop to verify the constraint.

<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 (Google Gemini)
  model = init_chat_model("gemini-2.5-flash", model_provider="google_genai")
  chat_history = [SystemMessage(content="You are a math tutor. You must only answer math equations. Politeness refuse any other topic.")]

  while True:
      query = input("User: ")
      if query.lower() == "exit":
          break
      chat_history.append(HumanMessage(content=query))
      res = model.invoke(chat_history)
      chat_history.append(AIMessage(content=res.content))
      print(f"AI: {res.content}")
  ```
</Accordion>

## Practice & Exercises

To practice, open the interactive notebook:

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Practice implementing live interactive CLI loops with memory history.

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