Objectives
- Setup an interactive command-line chat loop.
- 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:User: Hello, I am Satish.User: What is my name?
Sample Output
AI: Hello Satish! How can I help you today?AI: Your name is Satish.
Plan
- Initialize model using the core abstraction helper
init_chat_model("llama-3.3-70b-versatile", model_provider="groq"). - Initialize
chat_history = [SystemMessage(content="You are a helpful AI assistant.")]. - Create a
while Trueloop to take user console input, appendHumanMessage, invoke model on history, appendAIMessage, 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:User: What is 5 + 5?User: Who is the president of USA?
Sample Output
AI: 5 + 5 is 10.AI: I am sorry, but I can only answer math-related questions.
Plan
- Adjust the initial
SystemMessagecontent to instruct the model to only solve math questions and refuse other topics. - Run the loop to verify the constraint.
Solution
Solution
Practice & Exercises
To practice, open the interactive notebook:Practice & Exercises
Practice implementing live interactive CLI loops with memory history.💻 VS Code | 🚀 Colab | 📥 Download