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.

Customer support tickets can come from all directions, and manually analyzing and routing information is an overwhelming job. A text classification system can help support teams accelerate this process. It can augment a team’s capacity by automatically assigning each ticket to the right type or next action.

Here we look at an example of classifying customer emails to an insurance company into four types of requests, Finding policy details, Change account settings, Filing a claim and viewing status, and Cancelling coverage.

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 two per class.

from cohere.responses.classify import Example


examples=[
  Example("How do I find my insurance policy?", "Finding policy details"),
  Example("How do I download a copy of my insurance policy?", "Finding policy details"),
  Example("How do I find my policy effective date?", "Finding policy details"),
  Example("When does my insurance policy end?", "Finding policy details"),
  Example("Could you please tell me the date my policy becomes effective?", "Finding policy details"),
  Example("How do I sign up for electronic filing?", "Change account settings"),
  Example("How do I change my policy?", "Change account settings"),
  Example("How do I sign up for direct deposit?", "Change account settings"),
  Example("I want direct deposit. Can you help with that?", "Change account settings"),
  Example("Could you deposit money into my account rather than mailing me a physical cheque?", "Change account settings"),
  Example("How do I file an insurance claim?", "Filing a claim and viewing status"),
  Example("How do I file a reimbursement claim?", "Filing a claim and viewing status"),
  Example("How do I check my claim status?", "Filing a claim and viewing status"),
  Example("When will my claim be reimbursed?", "Filing a claim and viewing status"),
  Example("I filed my claim 2 weeks ago but I still haven’t received a deposit for it.", "Filing a claim and viewing status"),
  Example("I want to cancel my policy immediately! This is nonsense.", "Cancelling coverage"),
  Example("Could you please help my end my insurance coverage? Thank you.",
  "Cancelling coverage"),
  Example("Your service sucks. I’m switching providers. Cancel my coverage.", "Cancelling coverage"),
  Example("Hello there! How do I cancel my coverage?", "Cancelling coverage"),
  Example("How do I delete my account?", "Cancelling coverage")
]

Add inputs

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

inputs=[" I want to change my password”,
        "Does my policy cover prescription medication?"
        ]

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

{
  "results": [
    {
      "text": "I want to change my password",
      "prediction": "Change account settings",
      "confidence": 0.82,
      "confidences": [
        {
          "option": "Finding policy details",
          "confidence": 0.05
        },
        {
          "option": "Change account settings",
          "confidence": 0.82
        },
        {
          "option": "Filing a claim and viewing status",
          "confidence": 0.05
        },
       {
          "option":  "Cancelling coverage",
          "confidence": 0.08
        }
      ],
      
      "labels": {
       "Finding policy details": {
          "confidence": 0.05
        },
        "Change account settings": {
          "confidence": 0.82
        },
        "Filing a claim and viewing status": {
          "confidence": 0.05
        },
         "Cancelling coverage": {
          "confidence": 0.08
        }
      }
    },
    {
      "text":  "Does my policy cover prescription medication?",
      "prediction": "Finding policy details",
      "confidence": 0.75,
      "confidences": [
        {
          "option": "Finding policy details",
          "confidence": 0.75
        },
        {
          "option": "Change account settings",
          "confidence": 0.15
        },
        {
          "option": "Filing a claim and viewing status",
          "confidence": 0.05
        },
       {
          "option":  "Cancelling coverage",
          "confidence": 0.05
        }
      ],
      "labels": {
         "Finding policy details": {
          "confidence": 0.75
        },
        "Change account settings": {
          "confidence": 0.15
        },
        "Filing a claim and viewing status": {
          "confidence": 0.05
        },
         "Cancelling coverage": {
          "confidence": 0.05
        }
      }
    }
  ]
}
Language
Authorization
Bearer
Click Try It! to start a request and see the response here!