Lesson 3 of 4 Embeddings

Generating Embeddings

This lesson explains AI_GENERATE_EMBEDDINGS, alternatives outside the database and how to backfill large tables safely.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how AI_GENERATE_EMBEDDINGS is used.
  • Identify other ways to generate and store embeddings.
  • Explain how to backfill embeddings within rate limits.

AI_GENERATE_EMBEDDINGS

AI_GENERATE_EMBEDDINGS(text USE MODEL model_name [PARAMETERS json]) calls the external model and returns the embedding, which can be stored in a vector column or assigned to a variable. It works in SELECT, UPDATE, INSERT ... SELECT and MERGE, and PARAMETERS overrides model parameters for one call, such as dimensions or retry count. The caller needs EXECUTE on the external model.

UPDATE Support.ArticleChunk
SET Embedding = AI_GENERATE_EMBEDDINGS(ChunkText USE MODEL EmbeddingModel)
WHERE Embedding IS NULL;

DECLARE @q vector(1536) = AI_GENERATE_EMBEDDINGS(N'How do I reset my tent poles?' USE MODEL EmbeddingModel);
True or false

AI_GENERATE_EMBEDDINGS

Exam skills 3.1.6

Decide whether each statement about AI_GENERATE_EMBEDDINGS is true.

Log in to try this activity Interactive activities are free with an account.

Other ways to generate embeddings

Embeddings can also be created outside the database: an application or pipeline calls the model with the OpenAI or Foundry SDK and writes vectors as JSON arrays or, with drivers that support the vector type natively (such as Microsoft.Data.SqlClient 6.1 and later), as binary vectors. sp_invoke_external_rest_endpoint can call the embeddings REST API directly from T-SQL when an external model is not available, with the vector read from $.result.data[0].embedding. Generating outside the database suits large batch jobs, custom chunking and pipelines that also write to other stores.

Sort into groups

In the database or outside?

Exam skills 3.1.6

Decide where embedding generation fits best for each case.

Log in to try this activity Interactive activities are free with an account.

Backfilling large tables

Embedding millions of rows is bound by the model deployment's tokens-per-minute quota and by the database's limit on concurrent outbound connections. A safe backfill follows the steps below.

  1. Work in batches

    Update a few hundred rows at a time, selected with TOP and WHERE Embedding IS NULL, in a loop.

  2. Handle throttling

    Configure retry_count so 429 responses are retried, and add a short delay between batches.

  3. Make it restartable

    Because each batch commits, a failed run resumes from the remaining NULL rows.

  4. Index afterwards

    Create the vector index after the backfill, once at least 100 rows have vectors.

Every vector in a column must come from the same model and dimensions as the query embeddings, so the model name is recorded (for example in a column or table metadata) to detect mixing.

Put in order

A safe backfill

Exam skills 3.1.6

Put the steps of a safe embedding backfill in order.

Log in to try this activity Interactive activities are free with an account.
Worked example

Embedding a new catalogue

A developer runs one UPDATE that calls AI_GENERATE_EMBEDDINGS for 2 million product rows. After an hour it fails with throttling errors and the whole transaction rolls back. How should the developer restructure the job?

Process the rows in batches (for example 500 rows per UPDATE with WHERE Embedding IS NULL) in a loop, with a retry count on the model and a delay between batches, so each batch commits independently and the job can resume after failures. A single statement holds one transaction for the whole table and loses all progress on one failure, and raising the timeout does not address the rate limit.

Check yourself

What must match between stored embeddings and the query embedding?

Show answer

The model and the number of dimensions.

Exam Tips

Remember for the exam

  • Generate in T-SQL: AI_GENERATE_EMBEDDINGS(text USE MODEL name).
  • Query embeddings must use the same model and dimensions as stored embeddings.
  • Backfill in committed batches with retries to respect rate limits.
  • Create the vector index after the backfill.