Edit

Generate vector embeddings using the create_embeddings() AI function for Azure HorizonDB (Preview)

Vector embeddings are numerical representations of text that capture semantic meaning, enabling vector similarity search, clustering, and other vector-based operations. The azure_openai.create_embeddings() 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 (limited 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_openai.create_embeddings()

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

Syntax

-- Single text input
azure_openai.create_embeddings(model text, input text, dimensions integer DEFAULT NULL, timeout_ms integer DEFAULT 3600000, throw_on_error boolean DEFAULT true, max_attempts integer DEFAULT 1, retry_delay_ms integer DEFAULT 1000)

-- Batch input
azure_openai.create_embeddings(model text, input text[], batch_size integer DEFAULT 100, dimensions integer DEFAULT NULL, timeout_ms integer DEFAULT 3600000, throw_on_error boolean DEFAULT true, max_attempts integer DEFAULT 1, retry_delay_ms integer DEFAULT 1000)

Arguments

Argument Type Description
model (optional) text Model alias registered in the model registry. When omitted, uses the default-embedding Managed Model provisioned by AI Model Management (limited preview).
input 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 Number of records to process at a time. Only available for the text[] overload.
timeout_ms integer DEFAULT 3600000 Timeout in milliseconds after which the operation is stopped.
throw_on_error boolean DEFAULT true When true, the function throws an exception on error, resulting in a rollback of wrapping transactions.
max_attempts integer DEFAULT 1 Number of times the extension retries embedding creation on retryable errors.
retry_delay_ms integer DEFAULT 1000 Time in milliseconds to wait before retrying on retryable errors.

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.

Generate embeddings

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

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 = azure_openai.create_embeddings(
    input => session_abstract
)::vector
WHERE abstract_embedding IS NULL;

Note

BYOM users: Replace azure_openai.create_embeddings(input => session_abstract) with azure_openai.create_embeddings('your-model-alias', session_abstract).

Create a DiskANN index

For fast similarity search at scale, create a Scalable vector indexing with DiskANN (Preview) 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 <=> azure_openai.create_embeddings(
    input => 'Session about building chatbots'
)::vector
LIMIT 5;

Note

BYOM users: Replace azure_openai.create_embeddings(input => 'Session about building chatbots') with azure_openai.create_embeddings('your-model-alias', 'Session about building chatbots').