Skip to main content
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

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.

Practice & Exercises

To practice, open the interactive notebook:

Practice & Exercises

Practice implementing live interactive CLI loops with memory history.💻 VS Code | 🚀 Colab | 📥 Download