Skip to main content
Call Cohere’s API directly from SQL to access enterprise-grade embeddings, text classification, semantic reranking, and chat capabilities optimized for production use.

What is Cohere?

Cohere provides production-ready AI models for enterprises, specializing in embeddings, classification, and retrieval augmented generation (RAG). Cohere’s models excel at semantic search, text classification, and reranking results for improved relevance.

Key features

  • Enterprise embeddings: High-quality multilingual embeddings for semantic search
  • Text classification: Classify text into categories with custom examples
  • Semantic reranking: Improve search relevance by reordering results
  • Chat with RAG: Ground responses in your documents
  • Production-ready: Built for scale with enterprise support

Prerequisites

To use Cohere functions, you need:
  1. A Cohere API key from dashboard.cohere.com
  2. API key configured in your database (see configuration section below)

Quick start

Generate embeddings

Create vector embeddings for semantic search:
SELECT ai.cohere_embed(
    'embed-english-v3.0',
    'PostgreSQL is a powerful database'
);

Classify text

Categorize text using custom examples:
SELECT * FROM ai.cohere_classify_simple(
    'embed-english-v3.0',
    ARRAY['I love this product', 'This is terrible'],
    examples => '[
        {"text": "This is amazing", "label": "positive"},
        {"text": "This is awful", "label": "negative"}
    ]'::jsonb
);

Rerank search results

Improve search relevance by reordering results:
SELECT * FROM ai.cohere_rerank_simple(
    'rerank-english-v3.0',
    'What is a database?',
    ARRAY[
        'PostgreSQL is a relational database',
        'Python is a programming language',
        'A database stores and manages data'
    ]
)
ORDER BY relevance_score DESC;

Chat completion

Have conversations grounded in your documents:
SELECT ai.cohere_chat_complete(
    'command-r-plus',
    jsonb_build_array(
        jsonb_build_object('role', 'user', 'content', 'What is PostgreSQL?')
    )
)->'message'->>'content';

Configuration

Store your Cohere API key securely in the database:
-- Store API key as a secret
SELECT ai.create_secret('COHERE_API_KEY', 'your-api-key-here');

-- Use the secret by name
SELECT ai.cohere_embed(
    'embed-english-v3.0',
    'sample text',
    api_key_name => 'COHERE_API_KEY'
);

Available functions

Embeddings

Classification

Reranking

Chat

Tokenization

Model management

Available models

Cohere offers specialized models for different tasks:
  • Embeddings: embed-english-v3.0, embed-multilingual-v3.0
  • Reranking: rerank-english-v3.0, rerank-multilingual-v3.0
  • Chat: command-r-plus, command-r

Resources