Cohere tutorial: Q&A chatbot with heart from Cohere
🚀 Cohere: Powering Chatbots with Natural Language Processing
Cohere is revolutionizing the way we interact with machines, offering natural language processing models that enhance our understanding of the world. In this tutorial, we're crafting a chatbot with Cohere at its core. For more insights, check out our other Cohere tutorials.
Let's dive in into the world of Cohere Chatbots
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
🌟 Final Thoughts The power of Cohere models is immense, and this tutorial merely scratches the surface. From embedding to classifying text, Cohere opens up a world of possibilities for leveraging NLP models. Keep your creative juices flowing and stay tuned for more AI tutorials!
And we encourage you to join our upcoming AI Hackathons. Why woudln't you want to change the world with the power of AI?
Thank you! - Adrian Banachowicz, Data Science Intern in New Native