Docs/Recipes

Build a chat with history

Send the conversation so far, and keep adding to it.

Instead of a string, pass the messages so far. Add a system message to set the tone.

chat.js
const messages = [
  { role: 'system', content: 'You are a helpful assistant for a bakery in Pune. Keep answers short.' },
]

async function send(text) {
  messages.push({ role: 'user', content: text })
  const reply = await charge.ai.chat(messages)
  messages.push({ role: 'assistant', content: reply.text })
  return reply.text
}

Keep the history short (the last 10 to 20 messages). Each message can be up to 8,000 characters.

Read next

Still stuck? Talk to us and we'll help.