Exercise: Chat Completion¶
Objective¶
Learn how the Chat Completions API processes single-turn and multi-turn conversations using a growing messages list.
Concepts Covered¶
- Messages list and roles (
system,user,assistant) - Single-turn vs. multi-turn conversations
- Temperature and
max_tokensparameters - How the model "remembers" via the full message history
How It Works¶
The script shows how the messages list works. In single-turn mode, you send a system prompt and one user message, and the model replies. In multi-turn mode, you append the assistant's reply and a follow-up question back to the same messages list, then re-send the full history — this is how the model "remembers" the conversation.
sequenceDiagram
participant User as Script
participant LLM as LLM Provider
Note over User,LLM: Part 1 — Single-turn
User->>LLM: messages=[system, user]
LLM-->>User: assistant reply
Note over User,LLM: Part 2 — Multi-turn
User->>User: Append assistant reply +<br/>new user message to messages list
User->>LLM: messages=[system, user, assistant, user]
LLM-->>User: assistant reply (with full context)
Context sharing: The growing messages list IS the context. Each call sends the full conversation history to the model.
Interactive Message Flow¶
File¶
01_chat_completion.py— Basic chat completion with a travel assistant
How to Run¶
Expected Output¶
Structured logging showing the LLM interaction, the messages sent, and the response received.