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:

Use system prompts

Set the behavior and context with a system message:

Multi-turn conversation

Continue a conversation with message history:

Control response format

Specify max tokens and temperature:

Get the full response

Access all response metadata:

Arguments

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