Cohere tutorial: chat/conversation Summarizer with Cohere

Thursday, December 01, 2022 by ezzcodeezzlife
Cohere tutorial: chat/conversation Summarizer with Cohere

Cohere AI: Your Ally for Innovative Applications

Picture yourself partaking in an exciting lablab.ai AI Hackathon, creating a Cohere-based tool. As your team engages in a flurry of brainstorming over Discord, an ingenious idea emerges amidst the cascade of content.

Alternatively, if you have a chatbot on your website and need to summarize user-bot interactions, this tutorial has you covered. Learn to effortlessly generate chat summaries using Cohere and Python.

Ready to embark on your Cohere journey? Dive into our comprehensive Cohere guides for a smooth start in harnessing the power of AI applications.

Let’s start!

This data often is valuable for the business and can be used for further analysis. A conversation would typically look like this:

AI: Hi there, how can I assist you today?
Customer: I want to get more information about your pricing.
AI: Our pricing is $10 per month for the first 10 users, and $5 per month for each additional user.
Customer: Where can I find more information about your pricing?
AI: You can find more information about our pricing on our website.
Customer: What is your website?
AI: Our website is www.abc.com

With the help of our small program, wecan just summarize the entire conversation in one simple sentence like:

Customer wants to get more information about your pricing.

Prerequisites

  • Python 3.6 or higher
  • Cohere API key

Install the Cohere Python library

The first step is to install the Cohere Python library. You can do this by running the following command:

pip install cohere

Create a Cohere client

The next step is to create a Cohere client. You can do this by running the following code: (You can find your API on the Cohere dashboard)

import cohere 
co = cohere.Client('YOUR_KEY_HERE') 

Next, let's create a response object and call Generate on the Cohere client.

response = co.generate( 
  model='xlarge', 
  prompt="""Summarize this dialogue:
  Customer: Please connect me with a support agent.
  AI: Hi there, how can I assist you today?
  Customer: I forgot my password and lost access to the email affiliated to my account. Can you please help me?
  AI: Yes of course. First I\'ll need to confirm your identity and then I can connect you with one of our support agents.
  TLDR: A customer lost access to their account.
  --
  Summarize this dialogue:
  AI: Hi there, how can I assist you today?
  Customer: I want to book a product demo.
  AI: Sounds great. What country are you located in?
  Customer: I\'ll connect you with a support agent who can get something scheduled for you.
  TLDR: A customer wants to book a product demo.
  --
  Summarize this dialogue:
  AI: Hi there, how can I assist you today?
  Customer: I want to get more information about your pricing.
  AI: I can pull this for you, just a moment.
  TLDR:""", 
  max_tokens=20, 
  temperature=0.6, 
  k=0, 
  p=1, 
  frequency_penalty=0, 
  presence_penalty=0, 
  stop_sequences=["--"], 
) 
print('Prediction: {}'.format(response.generations[0].text)) 

We will choose the model xlarge for this example, and you can find more information about different model sizes on the Cohere website. Next, let's call the prompt and passin two examples of a summarized dialog. So right here you can see the summary of this dialogue at the top. Next, let's pass in our new chat that hasn't been summarized yet. You can see the TLDR is empty. Then let's pass in a few more parameters. You can choose these to your needs. And then let's pass in a stop sequence. This stop sequence tells the AI to stop generating when exactly this sequence is reached. So for example, you can see that the example sequence is always at the end of the summary. At the end, let's print out our prediction.

Now we can check if our program works by executing the Python file. You can use the summarizer to summarize conversations, for example on Slack, Discord, Telegram or Emails.

python filename.py

You can find the code for this tutorial on github.

Beyond Cohere Tutorials: Unleashing Your AI App Creativity

So, you've mastered creating a Cohere appβ€”what's next? Consider participating in an AI Hackathon organized by lablab.ai, where you can collaborate with fellow enthusiasts to develop functional AI apps in just a few days.

Simply identify a problem and tailor a solutionβ€”unlock the full potential of your newfound Cohere expertise.

Thank you! If you enjoyed this tutorial you can find more and continue reading our AI tutorials - Fabian Stehle, Data Science Intern at New Native

Discover tutorials with similar technologies

Upcoming AI Hackathons and Events