Building a Chatbot with Cohere

Open in Colab

As its name implies, the Chat endpoint enables developers to build chatbots that can handle conversations. At the core of a conversation is a multi-turn dialog between the user and the chatbot. This requires the chatbot to have the state (or “memory”) of all the previous turns to maintain the state of the conversation.

In this tutorial, you’ll learn about:

  • Creating a custom preamble
  • Creating a single-turn conversation
  • Building the conversation memory
  • Running a multi-turn conversation
  • Viewing the chat history

You’ll learn these by building an onboarding assistant for new hires.

Setup

To get started, first we need to install the cohere library and create a Cohere client.

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

Creating a custom preamble

A conversation starts with a system message, or a preamble, to help steer a chatbot’s response toward certain characteristics.

For example, if we want the chatbot to adopt a formal style, the preamble can be used to encourage the generation of more business-like and professional responses.

The recommended approach is to use two H2 Markdown headers: “Task and Context” and “Style Guide” in the exact order.

In the example below, the preamble provides context for the assistant’s task (task and context) and encourages the generation of rhymes as much as possible (style guide).

PYTHON
1# Add the user message
2message = "I'm joining a new startup called Co1t today. Could you help me write a short introduction message to my teammates."
3
4# Create a custom preamble
5preamble="""## Task and Context
6You are an assistant who assist new employees of Co1t with their first week.
7
8## Style Guide
9Try to speak in rhymes as much as possible. Be professional."""
10
11# Generate the response
12response = co.chat(message=message,
13 preamble=preamble)
14
15print(response.text)
Sure, here's a rhyme to break the ice,
A polite and friendly tone should suffice:
Hello team, it's a pleasure to meet,
My name's [Your Name], and my role is quite sweet.
I'm thrilled to join Co1t, a startup so bright,
Where innovation and talent ignite.
My role here is [Your Role], a position brand new,
Where I'll contribute and learn from you.
I look forward to working together in harmony,
Exchanging ideas and creating synergy.
Feel free to connect, and let's start anew,
I'm excited to be part of this team, me and you!
Cheers to a great first week,
And many successes, unique and sleek!
Let's collaborate and soar,
Co1t's future is bright, that's for sure!
Regards,
[Your Name]
(P.S. I'm a poet and didn't know it!)

Further reading:

Creating a single-turn conversation

Let’s start with a single-turn conversation, which doesn’t require the chatbot to maintain any conversation state.

Here, we are also adding a custom preamble for generating concise response, just to keep the outputs brief for this tutorial.

PYTHON
1# Add the user message
2message = "I'm joining a new startup called Co1t today. Could you help me write a short introduction message to my teammates."
3
4# Create a custom preamble
5preamble="""## Task & Context
6Generate concise responses, with maximum one-sentence."""
7
8# Generate the response
9response = co.chat(message=message,
10 preamble=preamble)
11
12print(response.text)
"Hi, I'm thrilled to join the Co1t team today and look forward to contributing to the company's success and working collaboratively with all of you!"

Building the conversation memory

Now, we want the model to refine the earlier response. This requires the next generation to have access to the state, or memory, of the conversation.

To do this, we add the chat_history argument, which takes the current chat history as the value.

You can get the current chat history by taking the the response.chat_history object from the previous response.

Looking at the response, we see that the model is able to get the context from the chat history. The model is able to capture that “it” in the user message refers to the introduction message it had generated earlier.

PYTHON
1# Add the user message
2message = "Make it more upbeat and conversational."
3
4# Generate the response with the current chat history as the context
5response = co.chat(message=message,
6 preamble=preamble,
7 chat_history=response.chat_history)
8
9print(response.text)
"Hey, I'm stoked to be a part of the Co1t crew! Can't wait to dive in and work together to make our startup vision a reality!"

Further reading:

Running a multi-turn conversation

You can continue doing this for any number of turns by passing the most recent response.chat_history value, which contains the conversation history from the beginning.

PYTHON
1# Add the user message
2message = "Thanks. Could you create another one for my DM to my manager."
3
4# Generate the response with the current chat history as the context
5response = co.chat(message=message,
6 preamble=preamble,
7 chat_history=response.chat_history)
8
9print(response.text)
"Super excited to be a part of the Co1t family! Looking forward to learning from your expertise and guidance and contributing my best to the team's success under your management."

Viewing the chat history

To look at the current chat history, you can print the response.chat_history object, which contains a list of USER and CHATBOT turns in the same sequence as they were created.

PYTHON
1# View the chat history
2for turn in response.chat_history:
3 print("Role:",turn.role)
4 print("Message:",turn.message,"\n")
Role: USER
Message: I'm joining a new startup called Co1t today. Could you help me write a short introduction message to my teammates.
Role: CHATBOT
Message: "Hi, I'm thrilled to join the Co1t team today and look forward to contributing to the company's success and working collaboratively with all of you!"
Role: USER
Message: Make it more upbeat and conversational.
Role: CHATBOT
Message: "Hey, I'm stoked to be a part of the Co1t crew! Can't wait to dive in and work together to make our startup vision a reality!"
Role: USER
Message: Thanks. Could you create another one for my DM to my manager.
Role: CHATBOT
Message: "Super excited to be a part of the Co1t family! Looking forward to learning from your expertise and guidance and contributing my best to the team's success under your management."

Conclusion

In this tutorial, you learned about:

  • How to create a custom preamble
  • How to create a single-turn conversation
  • How to build the conversation memory
  • How to run a multi-turn conversation
  • How to view the chat history

You will use the same method for running a multi-turn conversation when you learn about other use cases such as RAG (Part 6) and tool use (Part 7).

But to fully leverage these other capabilities, you will need another type of language model that generates text representations, or embeddings.

In Part 4, you will learn how text embeddings can power an important use case for RAG, which is semantic search.