Skip to main content
Generate text completions using OpenAI’s chat models like GPT-4 and GPT-3.5. This function enables you to have multi-turn conversations, generate text from prompts, and leverage advanced language model capabilities directly from PostgreSQL.

Samples

Generate a simple completion

Ask a question and get a response:
SELECT ai.openai_chat_complete(
    'gpt-4o-mini',
    jsonb_build_array(
        jsonb_build_object('role', 'user', 'content', 'What is PostgreSQL?')
    )
)->'choices'->0->'message'->>'content';

Use system prompts

Set the behavior and context with a system message:
SELECT ai.openai_chat_complete(
    'gpt-4o',
    jsonb_build_array(
        jsonb_build_object('role', 'system', 'content', 'You are a helpful database expert'),
        jsonb_build_object('role', 'user', 'content', 'Explain hypertables in simple terms')
    )
)->'choices'->0->'message'->>'content';

Multi-turn conversation

Continue a conversation with message history:
SELECT ai.openai_chat_complete(
    'gpt-4o-mini',
    jsonb_build_array(
        jsonb_build_object('role', 'system', 'content', 'You are a SQL expert'),
        jsonb_build_object('role', 'user', 'content', 'How do I create a table?'),
        jsonb_build_object('role', 'assistant', 'content', 'Use CREATE TABLE...'),
        jsonb_build_object('role', 'user', 'content', 'Now show me how to add an index')
    )
)->'choices'->0->'message'->>'content';

Control response format

Specify max tokens and temperature:
SELECT ai.openai_chat_complete(
    'gpt-4o-mini',
    jsonb_build_array(
        jsonb_build_object('role', 'user', 'content', 'Write a haiku about databases')
    ),
    max_tokens => 50,
    temperature => 0.7
);

Get the full response

Access all response metadata:
SELECT jsonb_pretty(
    ai.openai_chat_complete(
        'gpt-4o-mini',
        jsonb_build_array(
            jsonb_build_object('role', 'user', 'content', 'Hello!')
        )
    )
);

Arguments

NameTypeDefaultRequiredDescription
modelTEXT-The OpenAI model to use (e.g., gpt-4o, gpt-4o-mini, gpt-3.5-turbo)
messagesJSONB-Array of message objects with role and content fields
api_keyTEXTNULLOpenAI API key. If not provided, uses ai.openai_api_key setting
api_key_nameTEXTNULLName of the secret containing the API key
frequency_penaltyFLOATNULLPenalize new tokens based on their frequency (-2.0 to 2.0)
logit_biasJSONBNULLModify the likelihood of specified tokens appearing
logprobsBOOLEANNULLReturn log probabilities of output tokens
top_logprobsINTNULLNumber of most likely tokens to return at each position
max_tokensINTNULLMaximum number of tokens to generate
nINTNULLNumber of chat completion choices to generate
presence_penaltyFLOATNULLPenalize new tokens based on their presence (-2.0 to 2.0)
response_formatJSONBNULLFormat of the response (e.g., {"type": "json_object"})
seedINTNULLRandom seed for deterministic sampling
stopTEXTNULLUp to 4 sequences where the API will stop generating
temperatureFLOATNULLSampling temperature (0 to 2). Higher = more random
top_pFLOATNULLNucleus sampling parameter (0 to 1)
toolsJSONBNULLList of tools the model may call
tool_choiceJSONBNULLControls which tool is called
openai_userTEXTNULLUnique identifier for the end-user
extra_headersJSONBNULLAdditional HTTP headers
extra_queryJSONBNULLAdditional query parameters
verboseBOOLEANFALSEEnable verbose logging
client_configJSONBNULLAdvanced client configuration

Returns

JSONB: A JSON object containing the completion response with the following structure:
  • id: Unique identifier for the completion
  • object: Always "chat.completion"
  • created: Unix timestamp of when the completion was created
  • model: The model used for completion
  • choices: Array of completion choices
    • index: Choice index
    • message: The generated message with role and content
    • finish_reason: Why the model stopped generating
  • usage: Token usage information
    • prompt_tokens: Tokens in the prompt
    • completion_tokens: Tokens in the completion
    • total_tokens: Total tokens used