Generate vector embeddings by using ai.embed() in Azure HorizonDB

Important

The AI function guidance in this article uses the ai schema in the azure_ai extension version 2.5.6 or later. Pipeline builders use the ai.*_step() names. For earlier versions or existing azure_ai.* and azure_openai.create_embeddings() calls, see Migrate AI functions to the ai schema for version checks, API differences, upgrade steps, and the HorizonDB legacy reference.

Vector embeddings are numerical representations of text that capture semantic meaning. They enable vector similarity search, clustering, and other vector-based operations. The ai.embed() AI function in the azure_ai extension generates vector embeddings directly inside your database.

Prerequisites

  • An Azure HorizonDB instance with one of the following configurations:

    • AI Model Management (Preview) enabled.
      • It automatically installs the azure_ai extension and provisions a default-embedding model (text-embedding-3-small) ready to use.
    • Manual setup: Install the azure_ai extension and register your own embedding model through the model registry. See Manual setup with model registry for detailed steps.
  • The pgvector extension enabled on your database for storing and querying vector data.

    CREATE EXTENSION IF NOT EXISTS vector;
    
  • azure_ai 2.5.6 or later. Check the installed version and follow Migrate AI functions to the ai schema if your database uses an earlier version:

    SELECT extversion
    FROM pg_extension
    WHERE extname = 'azure_ai';
    
  • A connection to the primary (read-write) endpoint to create tables, store embeddings, and create indexes. Read-only inference can run on reader nodes. See Use AI functions on reader nodes.

ai.embed()

Creates vector embeddings for the given input text by calling the embedding model registered in the model registry.

Syntax

-- Single text input
ai.embed(text text, model text DEFAULT 'default-embedding',
         batch_size integer DEFAULT 100, dimensions integer DEFAULT NULL,
         retry_attempts integer DEFAULT NULL, retry_delay_ms integer DEFAULT 1000)
    RETURNS real[]

-- Batch input
ai.embed(text text[], model text DEFAULT 'default-embedding',
         batch_size integer DEFAULT 100, dimensions integer DEFAULT NULL,
         retry_attempts integer DEFAULT NULL, retry_delay_ms integer DEFAULT 1000)
    RETURNS TABLE (embedding real[])

Arguments

Argument Type Description
model (optional) text Model alias registered in the model registry. When omitted, the function uses the default-embedding managed model provisioned by AI Model Management (Preview).
text text or text[] Single text or array of texts to generate embeddings for.
dimensions integer DEFAULT NULL Number of dimensions for the output embeddings. Only supported in text-embedding-3 and later models.
batch_size integer DEFAULT 100 Maximum texts per request. Both overloads accept this argument; use it to control batching for array input.
retry_attempts integer DEFAULT NULL Total attempts, including the initial request. NULL uses three total attempts.
retry_delay_ms integer DEFAULT 1000 Initial retry delay in milliseconds.

Rate-limited embedding calls can wait and retry rather than fail immediately. For retry headers, wait limits, and error behavior, see Embedding calls are rate-limited.

Return type

real[] for single text input, or TABLE(embedding real[]) for batch input. Each value is a vector representation of the corresponding input text. Cast the scalar result to vector for storage and similarity operators.

Migrate from create_embeddings()

For version checks, before-and-after SQL, and the HorizonDB legacy reference, see Migrate AI functions to the ai schema.

The older azure_openai.create_embeddings() function remains available. When migrating to ai.embed(), use named arguments: change input to text and max_attempts to retry_attempts. Both attempt parameters count the initial request. Positional argument order also changes: ai.embed() takes text before the model alias. The new function doesn't expose timeout_ms or throw_on_error; don't carry those arguments over.

ai.embed() performs synchronous inference. The ai.embed_step(input => ...) pipeline builder instead returns a JSONB descriptor for deferred work. Use it inside an AI pipeline, not in an UPDATE or similarity query.

Generate embeddings

The following examples show how to generate, store, and query vector embeddings.

The examples use 1,536 dimensions for both document and query embeddings. If you use another model or dimension count, update the table's vector type, casts, and dimensions arguments consistently.

Embed a single text or a batch

SELECT ai.embed(
    text => 'Session about building chatbots',
    dimensions => 1536
) AS embedding;

SELECT *
FROM ai.embed(
    text => ARRAY[
        'Session about building chatbots',
        'PostgreSQL storage engine internals'
    ],
    dimensions => 1536,
    batch_size => 100
);

Create a sample table

CREATE TABLE conference_sessions (
    session_id int PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
    title text NOT NULL,
    session_abstract text NOT NULL,
    abstract_embedding vector(1536)
);

INSERT INTO conference_sessions (title, session_abstract)
VALUES
    ('Gen AI with Azure HorizonDB',
     'Learn about building intelligent applications with the azure_ai extension and pgvector'),
    ('Deep dive: PostgreSQL storage engine internals',
     'We will dig deep into storage internals');

Generate and store embeddings

UPDATE conference_sessions
SET abstract_embedding = ai.embed(
    text => session_abstract,
    dimensions => 1536
)::vector(1536)
WHERE abstract_embedding IS NULL;

Note

BYOM users: Add model => 'your-model-alias' to each ai.embed() call, including the query embedding. Use the same model and dimensions for documents and queries.

Create a DiskANN index

For fast similarity search at scale, create a Scalable vector indexing with DiskANN on the embedding column:

CREATE EXTENSION IF NOT EXISTS pg_diskann;

CREATE INDEX ON conference_sessions USING diskann (abstract_embedding vector_cosine_ops);

Find the most relevant sessions for a given query:

SELECT session_id, title, session_abstract
FROM conference_sessions
ORDER BY abstract_embedding <=> ai.embed(
    text => 'Session about building chatbots',
    dimensions => 1536
)::vector(1536)
LIMIT 5;

For durable ingestion of larger tables, use ai.embed_step() in an AI pipeline with retries, checkpointing, and incremental processing.