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.

Chatbots are designed to understand and respond to human language. They need to be able to understand the text they hear and understand the context of the conversation. They also need to be able to respond to people’s questions and comments in a meaningful way. To accomplish this, chatbots must be able to recognize specific intents that people express in conversation.

Here is an example of classifying the intent of customer inquiries on an eCommerce website into Shipping and handling policy, Start return or exchange, or Track order.

Set up

Install the SDK.

$ pip install cohere

Set up the Cohere client.

import cohere  
co = cohere.Client(api_key)

Add examples

These are the training examples we give the model to show the classes we want it to classify. Each example contains the text itself and the corresponding label, or class. The minimum number of examples required is five per class.

from cohere.classify import Example

examples =  [
  Example("Do you offer same day shipping?", "Shipping and handling policy"),  
  Example("Can you ship to Italy?", "Shipping and handling policy"),  
    Example("How long does shipping take?", "Shipping and handling policy"),  
    Example("Can I buy online and pick up in store?", "Shipping and handling policy"),  
    Example("What are your shipping options?", "Shipping and handling policy"),  
    Example("My order arrived damaged, can I get a refund?", "Start return or exchange"),  
    Example("You sent me the wrong item", "Start return or exchange"),  
    Example("I want to exchange my item for another colour", "Start return or exchange"),  
    Example("I ordered something and it wasn't what I expected. Can I return it?", "Start return or exchange"),  
    Example("What's your return policy?", "Start return or exchange"),  
    Example("Where's my package?", "Track order"),  
    Example("When will my order arrive?", "Track order"),  
    Example("What's my shipping number?", "Track order"),  
    Example("Which carrier is my package with?", "Track order"),  
    Example("Is my package delayed?", "Track order")
]

Add inputs

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

inputs=[" Am I still able to return my order?",  
        "When can I expect my package?",  
        "Do you ship overseas?",  
        ]

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": "Am I still able to return my order?",
      "prediction": "Start return or exchange",
      "confidence": 0.85,
      "confidences": [
        {
          "option": "Track order",
          "confidence": 0.05
        },
        {
          "option": "Start return or exchange",
          "confidence": 0.85
        },
        {
          "option": "Shipping and handling policy",
          "confidence": 0.1
        }
      ],
      "labels": {
        "Track order": {
          "confidence": 0.05
        },
        "Start return or exchange": {
          "confidence": 0.85
        },
        "Shipping and handling policy": {
          "confidence": 0.1
        }
      }
    },
    {
      "text":  "When can I expect my package?",
      "prediction": "positive review",
      "confidence": 0.57,
      "confidences": [
        {
          "option": "Track order",
          "confidence": 0.57
        },
        {
          "option": "Start return or exchange",
          "confidence": 0.13
        },
        {
          "option": "Shipping and handling policy",
          "confidence": 0.3
        }
      ],
      "labels": {
        "Track order": {
          "confidence": 0.57
        },
        "Start return or exchange": {
          "confidence": 0.13
        },
        "Shipping and handling policy": {
          "confidence": 0.3
        }
      }
    },
     {
      "text":  "Do you ship overseas?",
      "prediction": "Shipping and handling policy",
      "confidence": 0.75,
      "confidences": [
        {
          "option": "Track order",
          "confidence": 0.15
        },
        {
          "option": "Start return or exchange",
          "confidence": 0.1
        },
        {
          "option": "Shipping and handling policy",
          "confidence": 0.75
        }
      ],
      "labels": {
        "Track order": {
          "confidence": 0.15
        },
        "Start return or exchange": {
          "confidence": 0.01
        },
       "Shipping and handling policy": {
          "confidence": 0.75
        }
      }
    }
  ]
}
Language
Authentication
Bearer
Click Try It! to start a request and see the response here!