Skip to main content
Generate chat completions using Cohere’s Command models with built-in support for retrieval augmented generation (RAG). Ground responses in your documents and get automatic citations.

Samples

Basic chat

SELECT ai.cohere_chat_complete(
    'command-r-plus',
    jsonb_build_array(
        jsonb_build_object('role', 'user', 'content', 'What is PostgreSQL?')
    )
)->'message'->>'content';

Chat with documents (RAG)

SELECT ai.cohere_chat_complete(
    'command-r-plus',
    jsonb_build_array(
        jsonb_build_object('role', 'user', 'content', 'What are the key features?')
    ),
    documents => '[
        {"text": "PostgreSQL supports ACID transactions"},
        {"text": "TimescaleDB extends PostgreSQL for time-series"}
    ]'::jsonb
);

Use tools

SELECT ai.cohere_chat_complete(
    'command-r-plus',
    jsonb_build_array(
        jsonb_build_object('role', 'user', 'content', 'What is the weather?')
    ),
    tools => '[
        {
            "name": "get_weather",
            "description": "Get current weather",
            "parameter_definitions": {
                "location": {"type": "str", "required": true}
            }
        }
    ]'::jsonb
);

Arguments

NameTypeDefaultRequiredDescription
modelTEXT-Cohere model (e.g., command-r-plus, command-r)
messagesJSONB-Array of message objects with role and content
api_keyTEXTNULLCohere API key
api_key_nameTEXTNULLName of secret containing the API key
toolsJSONBNULLTool definitions for function calling
documentsJSONBNULLDocuments for RAG
citation_optionsJSONBNULLCitation configuration
response_formatJSONBNULLResponse format specification
safety_modeTEXTNULLSafety mode setting
max_tokensINTNULLMaximum tokens to generate
stop_sequencesTEXT[]NULLSequences that stop generation
temperatureFLOAT8NULLSampling temperature (0.0 to 1.0)
seedINTNULLRandom seed for reproducibility
frequency_penaltyFLOAT8NULLFrequency penalty
presence_penaltyFLOAT8NULLPresence penalty
kINTNULLTop-k sampling parameter
pFLOAT8NULLTop-p (nucleus) sampling parameter
logprobsBOOLEANNULLReturn log probabilities
tool_choiceTEXTNULLTool choice strategy
strict_toolsBOOLNULLEnforce strict tool schemas
verboseBOOLEANFALSEEnable verbose logging

Returns

JSONB: Complete API response with message, citations, and metadata.