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.responses.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 large. Putting everything together with the Classify endpoint looks like the following:

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

print(response.classifications)

Example Output

{
  "id": "[your_id]",
  "classifications": [
    {
      "id": "[your_id]",
      "input": "This item was broken when it arrived",
      "prediction": "negative review",
      "confidence": 0.98532915,
      "confidences": [
        {
          "option": "positive review",
          "confidence": 0.001107097
        },
        {
          "option": "negative review",
          "confidence": 0.98532915
        },
        {
          "option": "neutral review",
          "confidence": 0.013563732
        }
      ],
      "labels": {
        "negative review": {
          "confidence": 0.98532915
        },
        "neutral review": {
          "confidence": 0.013563732
        },
        "positive review": {
          "confidence": 0.001107097
        }
      }
    },
    {
      "id": "[your_id]",
      "input": "The product is amazing",
      "prediction": "neutral review",
      "confidence": 0.9015253,
      "confidences": [
        {
          "option": "positive review",
          "confidence": 0.09711582
        },
        {
          "option": "negative review",
          "confidence": 0.0013588573
        },
        {
          "option": "neutral review",
          "confidence": 0.9015253
        }
      ],
      "labels": {
        "negative review": {
          "confidence": 0.0013588573
        },
        "neutral review": {
          "confidence": 0.9015253
        },
        "positive review": {
          "confidence": 0.09711582
        }
      }
    },
    {
      "id": "[your_id]",
      "input": "The product was not too bad",
      "prediction": "neutral review",
      "confidence": 0.9977567,
      "confidences": [
        {
          "option": "positive review",
          "confidence": 0.0017497388
        },
        {
          "option": "negative review",
          "confidence": 0.0004935372
        },
        {
          "option": "neutral review",
          "confidence": 0.9977567
        }
      ],
      "labels": {
        "negative review": {
          "confidence": 0.0004935372
        },
        "neutral review": {
          "confidence": 0.9977567
        },
        "positive review": {
          "confidence": 0.0017497388
        }
      }
    }
  ],
  ...
}
Language
Authorization
Bearer
Click Try It! to start a request and see the response here!