This endpoint classifies text into one of several classes. It uses a few examples to create a classifier from a generative model. In the background, it constructs a few-shot classification prompt and uses it classify the input texts you pass to it.

📘

This is an interactive tutorial!

To run this tutorial, click on Examples and select one of the options.

Sentiment analysis is a type of classification task that analyzes the tone of a piece of text. It is used in a variety of different ways, or example, on social media comments and customer reviews. It is commonly used to see how people feel about their products or company, but it can also be used to help businesses understand how different trends in the economy may impact their business.

Here we look at an example of analyzing the sentiments of customer feedback about a product into Positive, Negative, or Neutral.

1. Set up

Install the SDK

$ pip install cohere

Set up the Cohere client.

import cohere
co = cohere.Client('<<apiKey>>')

2. Add examples

from cohere.classify import Example

examples=[
  Example("The order came 5 days early", "positive"), 
  Example("The item exceeded my expectations", "positive"), 
  Example("I ordered more for my friends", "positive"), 
  Example("I would buy this again", "positive"), 
  Example("I would recommend this to others", "positive"), 
  Example("The package was damaged", "negative"), 
  Example("The order is 5 days late", "negative"), 
  Example("The order was incorrect", "negative"), 
  Example("I want to return my item", "negative"), 
  Example("The item\'s material feels low quality", "negative"), 
  Example("The product was okay", "neutral"), 
  Example("I received five items in total", "neutral"), 
  Example("I bought it from the website", "neutral"), 
  Example("I used the product this morning", "neutral"), 
  Example("The product arrived yesterday", "neutral"),
]

3. Add inputs

These are the list of text pieces you’d like to classify.

inputs=[
  "This item was broken when it arrived",
  "The product is amazing",
  "The product was not too bad",
]

4. Get classifications

With the Classify endpoint, setting up the model is quite straightforward. The main thing to do is to define the model type. For our example, we’ll use the default, which is medium. Putting everything together with the Classify endpoint looks like the following:

response = co.classify(
  model='medium',
  inputs=inputs,
  examples=examples,
)

print(response.classifications)

Example Output

{
  "results": [
    {
      "text": "This item was broken when it arrived",
      "prediction": "negative review",
      "confidence": 0.85,
      "confidences": [
        {
          "option": "negative review",
          "confidence": 0.85
        },
        {
          "option": "positive review",
          "confidence": 0.05
        },
        {
          "option": "neutral review",
          "confidence": 0.1
        }
      ],
      "labels": {
        "positive review": {
          "confidence": 0.05
        },
        "negative review": {
          "confidence": 0.85
        },
        "neutral review": {
          "confidence": 0.1
        }
      }
    },
    {
      "text": "The product is amazing",
      "prediction": "positive review",
      "confidence": 0.57,
      "confidences": [
        {
          "option": "positive review",
          "confidence": 0.57
        },
        {
          "option": "neutral review",
          "confidence": 0.3
        },
        {
          "option": "negative review",
          "confidence": 0.13
        }
      ],
      "labels": {
        "positive review": {
          "confidence": 0.57
        },
        "negative review": {
          "confidence": 0.13
        },
        "neutral review": {
          "confidence": 0.3
        }
      }
    }
  ]
}
Language
Authentication
Bearer
Click Try It! to start a request and see the response here!