Skip to main content

Command Palette

Search for a command to run...

How Does Chat-Model Handles Multi-Turn Conversations

Published
2 min read
T
Backend | Systems Thinking | System Design

Multi-Turn Conversations

What is Multi-turn conversations?

Multi-turn connvo’s are basically the chat model maintains history of previously inputted user input and model’s responses, and can use it later for eg if user asked to summarize whole chat up until now or asks a ques on relation with prev inputed user-input

In short its basically who said to whom

How does is work?

Chat-models like chatgpt has a server to maintain users chat history and its responses.

But in a openai api call of an application the backend person has to maintain this history manually.

messages = [
  { role: "system", content: "You are a helpful assistant." },
  { role: "user", content: "What is JavaScript?" },
  { role: "assistant", content: "JavaScript is a programming language..." },
  { role: "user", content: "Give me an example." }
];

as u can see above first is role: systema we have set a initial context as “You are an assistant“.When user asks some ques it wil be saved as role:user and content: “what is JS“ .Once api call is done and w get the chat model’s response i.e. output., then we store it as role:assistant and its content wil be response which we got from the api calling i.e. content””JS is a programming language used in web Dev”.

Example:Consider a backend app using openai api call to answer ques, and showing res of chatgpt model on frontend.

# Define the initial system prompt to set the assistant's behavior
const messages = [
  { role: "system", content: "You are a helpful assistant." }
];

# When the user sends a new message, add it to the messages array
messages.push({ role: "user", content: "What is JavaScript?" });

# Make the API call to OpenAI with the updated messages array
const result = await openai.chat.completions.create({
  model: "gpt-4",
  messages: messages
});

# Extract the assistant's reply from the API response
const reply = result.choices[0].message;
# Save the assistant's reply in the messages array to maintain conversation history
messages.push({ role: "assistant", content: reply.content });

Each time the user sends a new message, push it to the array, send it to OpenAI, and store the assistant’s reply again. This way, the context keeps building, and the assistant feels "aware" of the entire chat!.