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.
Cohere
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):
self.co = 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):
return self.co.generate(
model='medium',
prompt=stevenQa(question),
max_tokens=50,
temperature=1).generations[0].text
Prompt
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}
Steven:'''
Streamlit
Streamlit is a great tool to build a simple web app.
Installation
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"):
st.write(cohere.cohere(question_for_steven))
To run the streamlit app use command
streamlit run name_of_your_file.py
The created app looks like this
Conclusion
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