Using the Chat API
The Chat API endpoint is used to generate text with Cohere LLMs. This endpoint facilitates a conversational interface, allowing users to send messages to the model and receive text responses.
Response Structure
Below is a sample response from the Chat API
Every response contains the following fields:
text
the generated message from the model.generation_id
the ID corresponding to this response. Can be used together with the Feedback API endpoint to promote great responses and flag bad ones.chat_history
the conversation presented in a chat log formatfinish_reason
can be one of the following:COMPLETE
the model successfully finished generating the messageMAX_TOKENS
the model’s context limit was reached before the generation could be completed
meta
contains information with token counts, billing etc.
Multi-turn conversations
The user message in the Chat request can be sent together with a chat_history
to provide the model with conversational context:
Instead of manually building the chat_history, we can grab it from the response of the previous turn.
Using conversation_id
to Save Chat History
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
.
Then, if you wanted to continue the conversation, you could do so like this (keeping the id
consistent):
Note that the conversation_id
should not be used in conjunction with the chat_history
. They are mutually exclusive.