Cohere tutorial: Q&A chatbot with heart from Cohere

2022-08-09T10:09:55.431Z by AdiAdi
Cohere tutorial: Q&A chatbot with heart from Cohere

Cohere provides natural language processing models that help companies improve human-machine interactions. It offers a clear way of understanding the world around us by providing dynamic and contextual information.

In this tutorial, we create the chatbot with cohere as a heart.

Before we have started with coding, you need to create an account on Cohere to get the API key.


To use cohere we need to install it

pip install cohere

Then we can use cohere in our code. In this tutorial, we use generate method. Entire docs for it you can found here. First, we have to initialize the client, I will also create a class CoHere.

In arguments of Client should be API key, which you have generated before, and version 2021-11-08.

class CoHere:
    def __init__(self, api_key): = cohere.Client(f'{api_key}', '2021-11-08')

Now, we should create a method to generate a text. We have to select a few arguments of cohere method.

model size of the model

prompt "instructions" for model, we use stevenQa function for it.

max_tokens is the max length of output

temperature is the degree of randomness

More avaiable arguments are here

    def cohere(self, question):


After that, we have to write a prompt for our model. The prompt is instructions and some examples. In brackets {question} will be new question.

def stevenAa(question):
    return f'''Steven is a chatbot that answers questions:

You: Who is better Pepsi or Coca-cola?
Steven: More people know that if they want to drink on the go, they should bring their own Coke
You: Where is the best restaurant?
Steven: Mexican and Chinese restaurant.
You: Which search engine is best?
Steven: Google search for best.
You: Do you like jazz?
Steven: I prefer to listen to jazz in the '80s.
You: {question}


Streamlit is a great tool to build a simple web app.


pip install streamlit

In this tutorial, we will build an app with two text inputs and a button to display the cohere result.

From docs of streamlit I will take four methods of streamlit

st.header() to make a header on our app

st.test_input() to send a text request

st.button() to create button

st.write() to display the results of cohere model.

import streamlit as st

st.header("Co:here application")

api_key = st.text_input("OpenAI API Key:", type="password")

st.header("Your personal chat bot - Steven")

question_for_steven = st.text_input("Question for Steven:")

cohere = CoHere(api_key)

if st.button("Answer"):

To run the streamlit app use command

streamlit run

The created app looks like this


The Cohere models are so powerful, that this tutorial shows only one usage of cohere model. Cohere is also able to embed and classify text. In my mind, there are a lot of ideas on how to use NLP models from cohere.

Stay tuned for future tutorials!

Thank you! - Adrian Banachowicz, Data Science Intern in New Native

Discover tutorials with similar technologies

Upcoming AI Hackathons and Events