Using the Cohere Chat API for Text Generation
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:
textthe generated message from the model.generation_idthe ID corresponding to this response. Can be used together with the Feedback API endpoint to promote great responses and flag bad ones.chat_historythe conversation presented in a chat log formatfinish_reasoncan be one of the following:COMPLETEthe model successfully finished generating the messageMAX_TOKENSthe model’s context limit was reached before the generation could be completed
metacontains 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.