Skip to content
derpx06Notes on systems, models & learning
2. LCEL & Prompt Architecture · lesson 8 of 68 · 2 min · January 10, 2026

Your First Chatbot: Talking to an LLM

Build a working CLI chatbot. We will explain every line of code so you understand how LLMs actually receive and process text.

We are going to build a command-line chatbot that you can talk to.

By the end of this lesson, you will have a script that runs a continuous conversation with an AI model. We will explain exactly what is happening under the hood.

First, we need to choose which "brain" needs to be used. In LangChain, every model (whether it's OpenAI, Google, or a local file) looks exactly the same to your code. This is called a Chat Model.

Select your provider below and run this setup code:

from langchain_openai import ChatOpenAI

# 1. Initialize the model
# We set temperature=0.7 to make it a bit creative.
model = ChatOpenAI(model="gpt-4o", temperature=0.7)

We created a model object. This object handles all the messy network requests, API keys, and JSON formatting for you. You just give it text, and it gives you back text.

Now, let's write the actual chatbot. We need a way to keep the conversation going forever (until we quit).

Copy this code into a file named chatbot.py:

chatbot.py
print("--- Chatbot Started (Type 'exit' to quit) ---")

while True:
  # 1. Get Input
  user_input = input("You: ")
  
  # Check if the user wants to quit
  if user_input.lower() in ["exit", "quit"]:
      break
  
  # 2. Invoke the Model
  # We send the user's string directly to the model.
  # It sends it to the API and waits for a reply.
  response = model.invoke(user_input)
  
  # 3. Print the Result
  # The response is an 'AIMessage' object. 
  # The actual text is hiding inside the '.content' property.
  print(f"AI: {response.content}")

Let's look at the key concepts we used here.

This is the most important function in LangChain. You passed it a simple string: "Hello". i I LangChain automatically wraps this string into a message format that the API understands (like {"role": "user", "content": "Hello"}), sends it, and waits.

The variable response is not just a string. It is an AIMessage object. It contains:

  • .content: The actual text reply.
  • .response_metadata: Extra data like how many tokens you used.

We used response.content to print just the text.

If you run this script, try this interaction:

Concept
You: Hi, my name is Bob.
AI: Hello Bob!
You: What is my name?
AI: I don't know your name.

Wait, what?

Every time the loop runs, we send ONLY the user_input for that turn. We are not sending the previous conversation history.

The model has no memory. It doesn't remember the last loop.

To fix this, we need to manage Chat History. We need to save what the AI said and send it back in the next turn. That is what we will build next.