AI Hints
Home›Generative AI Tutorial›Building with APIs
Generative AI · Lesson 31 of 77

Your First Chatbot

A conversation with memory.

⏱ 2 min read▶ Video at 1:13:49🎓 Free
Watch this lesson in the Generative AI courseWatch on YouTube ↗Full Generative AI course

Now we build. A chatbot is just a loop, that asks for input, sends the whole conversation to the model, prints the reply, and remembers it. In this lesson, you will see the complete idea, in about fifteen lines of code.

Your First Chatbot
From the video: Your First Chatbot
  • A chatbot is a loop
  • Remember by sending history
  • Fifteen lines of code

The chatbot loop

Here is the loop. Step one, read what the user types. Step two, add it to the message list. Step three, send the whole list to the model. Step four, print the reply. Step five, add the reply to the list too. Then go back to step one. The list grows, and that growing list is the bot's memory.

Chat loop
  1. Read inputthe user types
  2. Add to historyappend the message
  3. Ask the modelsend the whole list
  4. Print and rememberappend the reply

The chatbot in code

Here it is in code. We start the list with a system message. Inside a while loop, we read the input, and stop if the user types quit. We append the user message, call the model, and read the reply text. We print it, and append it, as an assistant message. That is a complete chatbot. Everything else, is polish.

chatbot.pypython
from openai import OpenAI
client = OpenAI()

messages = [{"role": "system", "content": "You are a helpful tutor."}]

while True:
    text = input("You: ")
    if text == "quit":
        break
    messages.append({"role": "user", "content": text})
    reply = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
    answer = reply.choices[0].message.content
    print("Bot:", answer)
    messages.append({"role": "assistant", "content": answer})

Making it better

Now improve it. Write a better system message, with a clear role, and rules. Stream the reply, so words appear as they are generated. Trim old messages, when the history grows too long. Save conversations to a file, or database. Add a friendly interface, with a web page, instead of a terminal. Each of these is a small step, built on the same loop.

A better system messageclear role and rules
Stream the replywords appear as they are written
Trim long historystay within the context window
Save conversationsto a file or database
Add a web interfacea friendly chat page

Summary

Excellent. A chatbot is a loop, with a growing message list that gives it memory. You have seen the full idea in a handful of lines, and a path to improve it. Next, we make model outputs reliable enough for real software, with structured outputs.

Key takeaways
  • Chatbot = loop + message history
  • History is the bot's memory
  • Improve: stream, trim, save, interface
Next up: Structured Outputs — reliable data from messy text