Sub topic 5
Sending your first chat completion
This chapter assumes you have Python and pip, a local server such as Ollama running on port 11434, and the llama3.2 model downloaded. It also assumes you can read JSON. You will send one chat request, first as raw HTTP and then through the OpenAI Python client. You will also learn how the response is laid out.
What a chat completion is
A chat completion is a request that sends a list of messages to a model and gets back the model's next message. The endpoint is POST /chat/completions, placed under the base URL. On a default Ollama install the full address is http://localhost:11434/v1/chat/completions.
The server is stateless. Each request must carry everything the model needs to see. The server does not remember earlier calls.
Step 1: Send a raw HTTP request
Start with curl so you can see the exact contract, with no library hiding it.
curl http://localhost:11434/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model": "llama3.2", "messages": [{"role": "user", "content": "Say hello in one sentence."}]}'The body needs two fields. model names the model the server should use. messages is a list of objects, each with a role and content. Here there is one message, from the user.
Step 2: Read the response
The server replies with JSON. A trimmed example looks like this. Your IDs, timestamps, and text will differ.
{
"id": "chatcmpl-123",
"object": "chat.completion",
"model": "llama3.2",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "Hello! Nice to meet you."},
"finish_reason": "stop"
}
],
"usage": {"prompt_tokens": 31, "completion_tokens": 8, "total_tokens": 39}
}These fields matter most:
| Field | Meaning |
|---|---|
| `choices` | A list of candidate replies. You normally get one. |
| `choices[0].message.content` | The reply text. |
| `finish_reason` | `stop` means the model finished naturally. `length` means it hit the token limit and was cut off. |
| `usage` | Token counts for the prompt and the reply. Some servers omit or approximate it. |
Step 3: Use the Python client
Install the client with pip install openai. The same request in Python looks like this.
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="local")
response = client.chat.completions.create(
model="llama3.2",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain what a context window is."},
],
temperature=0.2,
max_tokens=200,
)
print(response.choices[0].message.content)The client builds the same JSON body you wrote by hand, sends it to base_url plus /chat/completions, and parses the reply into Python objects. That is why you write response.choices[0].message.content and not a dictionary lookup.
The system message sets the behavior you want. The user message holds the question. Chapter-level detail on roles comes later, so for now treat the system message as standing instructions.
Common mistakes
- Leaving off
/v1in the base URL. The client then calls/chat/completionsat the server root and gets a 404. - Using a model name the server does not have. The name must match what the server reports, for example
llama3.2. Ollama returns an error if the model has not been pulled. - Not checking
finish_reason. A reply cut off atmax_tokenslooks like a complete answer unless you check forlength. - Assuming the server remembers the last call. If you want a follow-up question to make sense, you must send the earlier messages again.
Check your understanding
Exercise
You call the server and response.choices[0].finish_reason is "length". What happened, and what can you change to fix it?
Show answer
The model reached the maxtokens limit and stopped before finishing its answer. Raise maxtokens (or remove it so the server default applies), or ask the model for a shorter answer.
Exercise
Your Python script raises a 404 error. The server is running and the model is downloaded. Name two things to check in the client setup.
Show answer
Check that base_url ends with /v1, and check that the model name matches exactly what the server lists.