Skip to main content
Classify text into categories using Cohere’s models with custom examples. This function returns a simplified table format with the input text, predicted label, and confidence score.

Samples

Classify sentiment

Categorize text as positive or negative:
SELECT *
FROM ai.cohere_classify_simple(
    'embed-english-v3.0',
    ARRAY['I love this product', 'This is terrible', 'Pretty good overall'],
    examples => '[
        {"text": "This is amazing", "label": "positive"},
        {"text": "Absolutely wonderful", "label": "positive"},
        {"text": "This is awful", "label": "negative"},
        {"text": "Terrible experience", "label": "negative"}
    ]'::jsonb
);
Returns:
       input        | prediction | confidence
--------------------+------------+------------
 I love this product| positive   |       0.98
 This is terrible   | negative   |       0.95
 Pretty good overall| positive   |       0.72

Classify support tickets

Categorize customer inquiries:
SELECT *
FROM ai.cohere_classify_simple(
    'embed-english-v3.0',
    ARRAY[
        'My password is not working',
        'When will my order arrive?',
        'I want to cancel my subscription'
    ],
    examples => '[
        {"text": "Cannot log in", "label": "technical"},
        {"text": "Forgot my password", "label": "technical"},
        {"text": "Where is my package", "label": "shipping"},
        {"text": "Delivery status", "label": "shipping"},
        {"text": "Cancel my account", "label": "billing"},
        {"text": "Refund request", "label": "billing"}
    ]'::jsonb
);

Batch classification

Classify multiple texts at once:
INSERT INTO classified_feedback (feedback_text, category, confidence)
SELECT input, prediction, confidence
FROM ai.cohere_classify_simple(
    'embed-english-v3.0',
    ARRAY(SELECT feedback FROM pending_feedback LIMIT 100),
    examples => (SELECT classification_examples FROM model_config WHERE model = 'feedback')
);

Arguments

NameTypeDefaultRequiredDescription
modelTEXT-The Cohere model to use (e.g., embed-english-v3.0)
inputsTEXT[]-Array of texts to classify
api_keyTEXTNULLCohere API key. If not provided, uses configured secret
api_key_nameTEXTNULLName of the secret containing the API key
examplesJSONBNULLTraining examples as array of {"text": "...", "label": "..."} objects
truncate_long_inputsTEXTNULLHow to handle long inputs: START, END, NONE
verboseBOOLEANFALSEEnable verbose logging for debugging

Returns

TABLE: A table with the following columns:
ColumnTypeDescription
inputTEXTThe input text that was classified
predictionTEXTThe predicted category label
confidenceFLOAT8Confidence score (0.0 to 1.0) for the prediction