Skip to main content
Generate vector embeddings from any LLM provider through LiteLLM’s unified interface. Switch between OpenAI, Azure, AWS Bedrock, Google Vertex AI, and 100+ other providers by simply changing the model name.

Samples

Use OpenAI

SELECT ai.litellm_embed(
    'text-embedding-ada-002',
    'PostgreSQL is a powerful database',
    api_key_name => 'OPENAI_API_KEY'
);

Use Azure OpenAI

Embed with Azure OpenAI deployment:
SELECT ai.litellm_embed(
    'azure/my-embedding-deployment',
    'PostgreSQL is a powerful database',
    api_key_name => 'AZURE_API_KEY',
    extra_options => '{
        "api_base": "https://my-resource.openai.azure.com/",
        "api_version": "2023-05-15"
    }'::jsonb
);

Use AWS Bedrock

Embed with Amazon Titan:
SELECT ai.litellm_embed(
    'bedrock/amazon.titan-embed-text-v1',
    'PostgreSQL is a powerful database',
    extra_options => '{
        "aws_region_name": "us-east-1",
        "aws_access_key_id": "your-key",
        "aws_secret_access_key": "your-secret"
    }'::jsonb
);

Use Google Vertex AI

Embed with Vertex AI:
SELECT ai.litellm_embed(
    'vertex_ai/textembedding-gecko',
    'PostgreSQL is a powerful database',
    extra_options => '{
        "vertex_project": "my-project-id",
        "vertex_location": "us-central1"
    }'::jsonb
);

Batch embeddings

Process multiple texts efficiently:
SELECT index, embedding
FROM ai.litellm_embed(
    'text-embedding-ada-002',
    ARRAY[
        'PostgreSQL is a powerful database',
        'TimescaleDB extends PostgreSQL',
        'pgai brings AI to PostgreSQL'
    ],
    api_key_name => 'OPENAI_API_KEY'
);

Store embeddings in a table

UPDATE documents
SET embedding = ai.litellm_embed(
    'text-embedding-ada-002',
    content,
    api_key_name => 'OPENAI_API_KEY'
)
WHERE embedding IS NULL;

Arguments

NameTypeDefaultRequiredDescription
modelTEXT-Model identifier with optional provider prefix (e.g., text-embedding-ada-002, azure/deployment, bedrock/model-id)
input_textTEXT-Single text input to embed (use this OR input_texts)
input_textsTEXT[]-Array of text inputs to embed in a batch
api_keyTEXTNULLAPI key for the provider
api_key_nameTEXTNULLName of the secret containing the API key
extra_optionsJSONBNULLProvider-specific options (API base URL, region, project, etc.)
verboseBOOLEANFALSEEnable verbose logging for debugging

Returns

For single text input:
  • vector: A pgvector compatible vector containing the embedding
For array input:
  • TABLE(index INT, embedding vector): A table with an index and embedding for each input text

Provider-specific configuration

Different providers require different configurations through the extra_options parameter:

Azure OpenAI

{
  "api_base": "https://resource.openai.azure.com/",
  "api_version": "2023-05-15"
}

AWS Bedrock

{
  "aws_region_name": "us-east-1",
  "aws_access_key_id": "key",
  "aws_secret_access_key": "secret"
}

Google Vertex AI

{
  "vertex_project": "project-id",
  "vertex_location": "us-central1"
}
See LiteLLM providers documentation for complete configuration options.