Text Generation

Cohere’s Command family of LLMs are available via the Chat endpoint. This endpoint enables you to build generative AI applications and facilitates a conversational interface for building chatbots.

This quickstart guide shows you how to perform text generation with the Chat endpoint.

1

Setup

First, install the Cohere Python SDK with the following command.

$pip install -U cohere

Next, import the library and create a client.

PYTHON
1import cohere
2
3co = cohere.Client(
4 "COHERE_API_KEY"
5) # Get your free API key here: https://dashboard.cohere.com/api-keys
2

Basic Text Generation

To perform a basic text generation, call the Chat endpoint by passing the message parameter containing the user message.

PYTHON
1response = co.chat(
2 model="command-r-plus-08-2024",
3 message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?",
4)
5
6print(response.text)
1"Excited to be part of the Co1t team, I'm [Your Name], a [Your Role], passionate about [Your Area of Expertise] and looking forward to contributing to the company's success."
3

State Management

To maintain the state of a conversation, such as for building chatbots, append a sequence of user and chatbot messages to the chat_history list. You can also include a preamble parameter, which will act as a system message to set the context of the conversation.

PYTHON
1response = co.chat(
2 model="command-r-plus-08-2024",
3 preamble="You respond in concise sentences.",
4 chat_history=[
5 {"role": "user", "message": "Hello"},
6 {
7 "role": "chatbot",
8 "message": "Hi, how can I help you today?",
9 },
10 ],
11 message="I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates?",
12)
13
14print(response.text)
1"Excited to join the team at Co1t, looking forward to contributing my skills and collaborating with everyone!"
4

Streaming

To stream the generated text, call the Chat endpoint using chat_stream instead of chat. This returns a generator that yields chunk objects, which you can access the generated text from.

PYTHON
1message = "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates."
2
3response = co.chat_stream(
4 model="command-r-plus-08-2024", message=message
5)
6
7for chunk in response:
8 if chunk.event_type == "text-generation":
9 print(chunk.text, end="")
1"Excited to be part of the Co1t team, I'm [Your Name], a [Your Role/Position], looking forward to contributing my skills and collaborating with this talented group to drive innovation and success."

Further Resources

Built with