Text generation - quickstart

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.ClientV2(
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 messages parameter containing the user message.

PYTHON
1response = co.chat(
2 model="command-r-plus-08-2024",
3 messages=[
4 {
5 "role": "user",
6 "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
7 }
8 ],
9)
10
11print(response.message.content[0].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 assistant messages to the messages list. You can also include a system message at the start of the list to set the context of the conversation.

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

Streaming

To stream text generation, 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
1res = co.chat_stream(
2 model="command-r-plus-08-2024",
3 messages=[
4 {
5 "role": "user",
6 "content": "I'm joining a new startup called Co1t today. Could you help me write a one-sentence introduction message to my teammates.",
7 }
8 ],
9)
10
11for chunk in res:
12 if chunk:
13 if chunk.type == "content-delta":
14 print(chunk.delta.message.content.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