4. Assistant Messages
In this lesson, we will explore how to utilize assistant messages for maintaining conversation contexts when interacting with the Chat Completion API, specifically with the gpt-4o model.
Independent Requests¶
It's crucial to understand that each request to the API is independent. Without context linking, subsequent requests cannot reference earlier ones. So, how can we maintain continuity in a conversation with models like gpt-4o? The answer lies in using assistant messages, which are the responses generated by the model to user queries. Including these messages in the messages array of our requests allows us to create a coherent dialogue.
Example Conversation¶
Let's illustrate this with a simple conversation. We start by sending a basic request with the user message "Hello!":
In response, we receive an assistant message:
{
"id": "chatcmpl-B3Kxois3s7jGJLIVLg6m5xPNQpmWg",
"object": "chat.completion",
"created": 1740135268,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I assist you today?",
"refusal": null
},
...
}
],
...
}
Continuing the Dialogue¶
Next, we take the assistant's message and include it in the messages array for the next request. We also add a new user message expressing hunger:
{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Hello!"
},
{
"role": "assistant",
"content": "Hello! How can I assist you today?"
},
{
"role": "user",
"content": "I'm hungry."
}
]
}
When we send this request, the model responds as follows:
{
...
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm here to help! What type of food are you
in the mood for? Are you thinking of making something at home,
or would you prefer to order out?",
"refusal": null
},
...
}
],
...
}
Building Context¶
We continue to enhance the conversation by adding the latest assistant message before posing another user question, this time focusing on cooking at home:
{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Hello!"
},
{
"role": "assistant",
"content": "Hello! How can I assist you today?"
},
{
"role": "user",
"content": "I'm hungry."
},
{
"role": "assistant",
"content": "I'm here to help! What type of food are you
in the mood for? Are you thinking of making something at home,
or would you prefer to order out?"
},
{
"role": "user",
"content": "home"
}
]
}
Final Response¶
Based on the entire conversation context, the model processes the latest input and replies:
{
...
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Great! Do you have any specific ingredients
on hand, or any particular type of cuisine you’re interested in?
Let me know so I can suggest a recipe!",
"refusal": null
},
...
}
],
...
}
Conclusion¶
This demonstrates how to leverage assistant messages for interactive conversations with LLMs. In the next lesson, we will transition to writing our Emacs package.