Using a Language Model in Code › Putting It Together: A Simple Chat Loop

Michael BrenndoerferJune 27, 20252 min read
AI Agent HandbookAll chapters
Part 2 of 15Language Models - The Brain of the Agent
Chapter 5Using a Language Model in Code › Putting It Together: A Simple Chat Loop
Reading section 9 of 15 · Putting It Together: A Simple Chat Loop · 2 minSection 9 of 15 · 2 min

Book contents

Section 9 of 15 · 2 min read

Using a Language Model in Code › Putting It Together: A Simple Chat LoopOne section · Sections in this chapter9 / 15 ↓

Let's build a basic interactive chat with our assistant:

In[2]:
Code
def chat():
    """
    Simple chat loop with the assistant.
    Type 'quit' to exit.
    """
    print(
        "Assistant: Hello! I'm your personal assistant. How can I help you today?"
    )
    print("(Type 'quit' to exit)\n")

    while True:
        # Get user input
        user_input = input("You: ").strip()

        # Check for exit
        if user_input.lower() in ["quit", "exit", "bye"]:
            print("Assistant: Goodbye! Have a great day!")
            break

        # Skip empty inputs
        if not user_input:
            continue

        # Get response from the model
        response = ask_assistant_safe(user_input)
        print(f"Assistant: {response}\n")


## Run the chat
chat()

Try this out. You now have a working conversational assistant! It's basic (it doesn't remember previous messages in the conversation, can't use tools, and has no special capabilities beyond text generation), but it's a real AI agent that can understand and respond to natural language.

Example interaction:

Assistant: Hello! I'm your personal assistant. How can I help you today?
(Type 'quit' to exit)

You: What's the largest planet in our solar system?
Assistant: The largest planet in our solar system is Jupiter.

You: How much larger is it than Earth?
Assistant: Jupiter is significantly larger than Earth. Its diameter is about 
11 times that of Earth, and its volume is roughly 1,321 times greater.

You: quit
Assistant: Goodbye! Have a great day!

Notice a limitation: when we asked "How much larger is it than Earth?", the assistant understood we were asking about Jupiter because that's a reasonable inference from the question alone (the pronoun "it" plus the context of planetary comparisons). But it doesn't actually remember the previous exchange. Each call to ask_assistant_safe() is independent. We'll fix this in Chapter 6 when we add memory.