Chat Messages Structure: Roles and Configurations
The Chat Completions API is the core engine for conversing with OpenAI models. It processes messages using specific role configurations. Let us review how they work.
1. Chat Message Roles
To compile a conversation history, send an array of message objects. Each object contains a role and content.
const messages = [
{ role: "system", content: "You are a math tutor." },
{ role: "user", content: "What is 2 + 2?" },
{ role: "assistant", content: "2 + 2 equals 4." },
{ role: "user", content: "Multiply that by 10." },
];system: Sets the global persona, rules, boundaries, and tone of the assistant. It runs first and guides how subsequent messages are processed.user: Represents the end-user queries.assistant: Represents the previous responses generated by the model. By appending assistant messages, you allow the model to remember conversational context.
2. Calling the API in Node.js
Pass your array to the .chat.completions.create() endpoint:
import { openai } from "../lib/openai";
async function runChat() {
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "system",
content: "You are a technical editor. Reply in a concise developer style."
},
{
role: "user",
content: "Explain what Server-Side Rendering (SSR) is."
},
],
});
const responseText = completion.choices[0].message.content;
console.log("AI Assistant response:", responseText);
}3. Designing Context History limits
LLMs do not store state between API calls. To maintain a chat session, you must send the entire message history on every request.
Because LLMs have limit boundaries (context windows), truncate or summarize older conversation messages as the history grows to avoid exceeding token limits.
Published on Last updated: