We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Multiple Messages

LLM APIs aren't typically used in a "one-shot" manner, for example:

  • Prompt: "What is the meaning of life?"
  • Response: "42"

They work the same way ChatGPT works: in a conversation. The conversation has a history, and if we keep track of that history, then with each new prompt, the model can see the entire conversation and respond within the larger context of the conversation.

Roles

Importantly, each message in the conversation has a "role." In the context of a chat app like ChatGPT, your conversations would look like this:

  • user: "What is the meaning of life?"
  • assistant: "42"
  • user: "Wait, what did you just say?"
  • assistant: "42. It's the answer to the ultimate question of life, the universe, and everything."
  • user: "But why?"
  • assistant: "Because Douglas Adams said so."

The two roles we care about right now are user (messages from us) and assistant (messages from the model). Later we'll add system and tool messages too.

So, while our program will still be "one-shot" for now, let's update our code to store a list of messages in the conversation, and pass in the "role" appropriately.

Assignment

  1. messages = [
        {"role": "user", "content": args.user_prompt},
    ]
    
  2. response = client.chat.completions.create(
        model="openrouter/free",
        messages=messages,
    )
    

In the future, we'll add more messages to the list as the agent does its tasks in a loop.

The solution extracts the API call into a separate generate_content(client, messages) function for better code organization. You can keep your code in main() for now, or extract it to a helper function if you prefer. Both approaches work fine.

If everything is still working normally, submit the CLI tests.