You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the concrete IEmbeddingGenerator implementation that lives inside the Microsoft.Azure.Cosmos.AI package. It wraps Azure.AI.OpenAI's EmbeddingClient to generate text embeddings, and is wired up from VectorEmbeddingPolicy.Embedding.EmbeddingSource (endpoint, deploymentName, authType) or from explicit constructor parameters.
Reference implementation exists in the CosmosDB monorepo at: /Product/Cosmos/EmbeddingGenerator/Service/Providers/AzureOpenAITextEmbeddingProvider.cs
(adapt ΓÇö do not copy verbatim; the monorepo class has service-side concerns not relevant here).
Public API
namespaceMicrosoft.Azure.Cosmos.AI{/// <summary>/// IEmbeddingGenerator implementation backed by Azure OpenAI./// Construct from an EmbeddingSource (read from VectorEmbeddingPolicy) or/// provide endpoint/deployment/credential directly./// </summary>publicsealedclassAzureOpenAIEmbeddingGenerator:IEmbeddingGenerator,IDisposable{// Constructor 1: from VectorEmbeddingPolicy EmbeddingSource + ApiKeypublicAzureOpenAIEmbeddingGenerator(EmbeddingSourcesource,stringapiKey);// Constructor 2: from VectorEmbeddingPolicy EmbeddingSource + TokenCredential (Entra)publicAzureOpenAIEmbeddingGenerator(EmbeddingSourcesource,TokenCredentialcredential);// Constructor 3: explicit params + ApiKey (for customers not using VectorEmbeddingPolicy)publicAzureOpenAIEmbeddingGenerator(stringendpoint,stringdeploymentName,intdimensions,stringapiKey);// Constructor 4: explicit params + TokenCredential (Entra)publicAzureOpenAIEmbeddingGenerator(stringendpoint,stringdeploymentName,intdimensions,TokenCredentialcredential);publicTask<IEnumerable<ReadOnlyMemory<double>>>GenerateEmbeddingsAsync(IEnumerable<string>text,CancellationTokencancellationToken=default);publicvoidDispose();}}
Behavioral requirements
Batching: if the input list is longer than the Azure OpenAI batch limit (2048 items), split into multiple requests automatically and concatenate results in order.
Dimensions: use the dimensions parameter from EmbeddingSource (or constructor arg) when calling EmbeddingGenerationOptions.Dimensions so the output vector size matches the container policy.
Return type: materialize each OpenAI.Embeddings.Embedding float value to double, wrap in ReadOnlyMemory<double>.
Auth:
ApiKey path: new AzureOpenAIClient(endpoint, new AzureKeyCredential(apiKey))
Entra path: new AzureOpenAIClient(endpoint, tokenCredential)
Error handling: surface Azure SDK RequestFailedException as CosmosException (HTTP 503) with innerException preserved and an actionable message identifying the embedding endpoint + deployment.
Thread safety: EmbeddingClient is thread-safe; multiple concurrent calls are allowed.
Dispose: disposes the underlying AzureOpenAIClient if it was created internally.
Acceptance criteria
Unit tests (in test project) cover:
Happy path: single batch, returns correct count and double[] values.
Multi-batch: input > batch limit, two API calls made, results concatenated in order.
Wrong dimensions in response → wraps as CosmosException.
RequestFailedException → wrapped as CosmosException with inner exception.
Constructed from EmbeddingSource (round-trip: source → client → call).
Dispose called on the underlying client exactly once.
API surface matches the public constructors above.
[Embedding V0]
AzureOpenAIEmbeddingGeneratorΓÇö provider implementationPart of: #5830 (V0 embedding generation).
Background
This is the concrete
IEmbeddingGeneratorimplementation that lives inside theMicrosoft.Azure.Cosmos.AIpackage. It wrapsAzure.AI.OpenAI'sEmbeddingClientto generate text embeddings, and is wired up fromVectorEmbeddingPolicy.Embedding.EmbeddingSource(endpoint, deploymentName, authType) or from explicit constructor parameters.Reference implementation exists in the CosmosDB monorepo at:
/Product/Cosmos/EmbeddingGenerator/Service/Providers/AzureOpenAITextEmbeddingProvider.cs(adapt ΓÇö do not copy verbatim; the monorepo class has service-side concerns not relevant here).
Public API
Behavioral requirements
dimensionsparameter fromEmbeddingSource(or constructor arg) when callingEmbeddingGenerationOptions.Dimensionsso the output vector size matches the container policy.OpenAI.Embeddings.Embeddingfloat value todouble, wrap inReadOnlyMemory<double>.ApiKeypath:new AzureOpenAIClient(endpoint, new AzureKeyCredential(apiKey))Entrapath:new AzureOpenAIClient(endpoint, tokenCredential)RequestFailedExceptionasCosmosException(HTTP 503) withinnerExceptionpreserved and an actionable message identifying the embedding endpoint + deployment.EmbeddingClientis thread-safe; multiple concurrent calls are allowed.AzureOpenAIClientif it was created internally.Acceptance criteria
CosmosException.RequestFailedException→ wrapped asCosmosExceptionwith inner exception.EmbeddingSource(round-trip: source → client → call).Disposecalled on the underlying client exactly once.AzureOpenAIEmbeddingGeneratorworks end-to-end when set onQueryRequestOptions.EmbeddingGeneratoragainst an emulator running a hybrid-search query (covered by [Embedding V0] Emulator tests + baseline regeneration #5837).Files likely touched
Microsoft.Azure.Cosmos.AI/src/AzureOpenAIEmbeddingGenerator.cs(new)Microsoft.Azure.Cosmos.AI/tests/.../AzureOpenAIEmbeddingGeneratorTests.cs(new)Dependencies
EmbeddingSourcemodel ([Embedding V0] DTO additions + new QueryFeatures.EmbeddingGeneration bit #5832 / VectorEmbeddingPolicy issue)IEmbeddingGeneratorinterface ([Embedding V0] Public surface: IEmbeddingGenerator + QueryRequestOptions.EmbeddingGenerator #5831 ΓÇö done, merged in PR EmbeddingGenerator: Adds ICosmosEmbeddingGenerator client-wide configuration (preview) #5838)