Multi-Message Conversations

There are various ways to interact with the Chat API. We covered interacting with Chat directly in the Chat API companion doc, here we'll discuss having a multi-message conversation with the Chat endpoint.

Use the Previous Messages to Continue a Conversation

By specifying the chat_history parameter, we can provide our model with context that it can use in its replies.

chat_history = [
	{"role": "USER", "text": "Hey!"},
	{"role": "CHATBOT", "text": "Hey! How can I help you today?"},
]
message = "Can you tell me about LLMs?"

response = co.chat(
	message=message,
	chat_history=chat_history
)

answer = response.text

Keeping Track of Responses

Instead of hardcoding the chat_history, we can build it dynamically as we have a conversation.

chat_history = []
max_turns = 10

for _ in range(max_turns):
	# get user input
	message = input("Send the model a message: ")
	
	# generate a response with the current chat history
	response = co.chat(
		message=message,
		temperature=0.8,
		chat_history=chat_history
	)
	answer = response.text
		
	print(answer)

	# add message and answer to the chat history
	user_message = {"role": "USER", "text": message}
	bot_message = {"role": "CHATBOT", "text": answer}
	
	chat_history.append(user_message)
	chat_history.append(bot_message)

Using the 'Managed Conversation' Feature

Providing the model with the conversation history is one way to have a multi-turn conversation with the model. Cohere has developed another option for users who do not wish to save the conversation history, and it works through a user-defined conversation_id.

response = co.chat(
	message=message,
  conversation_id='user_defined_id_1',
	model="command", 
	temperature=0.9
)

answer = response.text

Then, if you wanted to continue the conversation, you could do so like this (keeping the id consistent):

message2 = "What was my first message to you?"

response2 = co.chat(
    message=message2,
    conversation_id='user_defined_id_1',
    model="command", 
    temperature=0.9)

Note that the conversation_id should not be used in conjunction with the chat_history. They are mutually exclusive.