OpenAICompatibleEmbeddingGenerator Class

An IEmbeddingGenerator<T1, T2> for OpenAI-compatible embeddings endpoints (OpenAI, Azure OpenAI, Ollama, Cloudflare Workers AI and others).

Inheritance

Object

OpenAICompatibleEmbeddingGenerator

Implements

Microsoft.Extensions.AI.IEmbeddingGenerator<string, Microsoft.Extensions.AI.Embedding<float>>

Microsoft.Extensions.AI.IEmbeddingGenerator

Namespace: Radzen

Assembly: Radzen.Blazor.dll

Syntax

public sealed class OpenAICompatibleEmbeddingGenerator : Microsoft.Extensions.AI.IEmbeddingGenerator<string, Microsoft.Extensions.AI.Embedding<float>>, Microsoft.Extensions.AI.IEmbeddingGenerator

Examples

builder.Services.AddEmbeddingGenerator(services =>
    new OpenAICompatibleEmbeddingGenerator(services.GetRequiredService<HttpClient>(), "https://api.openai.com/v1/embeddings", apiKey, defaultModel: "text-embedding-3-small"));

Constructors

OpenAICompatibleEmbeddingGeneratorLink to this section

Initializes a new instance of the OpenAICompatibleEmbeddingGenerator class.

Declaration
public OpenAICompatibleEmbeddingGenerator(Net.Http.HttpClient httpClient, string endpoint, string apiKey, string apiKeyHeader, string defaultModel)
Parameters
Type Name Description
Net.Http.HttpClient httpClient The HttpClient used to send requests. It is not disposed by the generator.
string endpoint The embeddings URL, for example https://api.openai.com/v1/embeddings. A relative URL is resolved against the BaseAddress.
string apiKey The API key. When null or empty no authentication header is sent.
string apiKeyHeader The header that carries the API key. Authorization (the default) sends it as a bearer token.
string defaultModel The model used when ModelId is not set.

Properties

MetadataLink to this section

Gets the metadata that describes the generator.

Declaration
public Microsoft.Extensions.AI.EmbeddingGeneratorMetadata Metadata { get; }
Property Value
Type Description
Microsoft.Extensions.AI.EmbeddingGeneratorMetadataGets the metadata that describes the generator.

Methods

DisposeLink to this section

Declaration
public void Dispose()

GenerateAsyncLink to this section

Declaration
public Task<Microsoft.Extensions.AI.GeneratedEmbeddings<Microsoft.Extensions.AI.Embedding<float>>> GenerateAsync(IEnumerable<string> values, Microsoft.Extensions.AI.EmbeddingGenerationOptions options, Threading.CancellationToken cancellationToken)
Parameters
Type Name Description
IEnumerable<string> values
Microsoft.Extensions.AI.EmbeddingGenerationOptions options
Threading.CancellationToken cancellationToken
Returns
Type Description
Task<Microsoft.Extensions.AI.GeneratedEmbeddings<Microsoft.Extensions.AI.Embedding<float>>>

GetServiceLink to this section

Declaration
public object GetService(Type serviceType, object serviceKey)
Parameters
Type Name Description
Type serviceType
object serviceKey
Returns
Type Description
object
An error has occurred. This app may no longer respond until reloaded. Reload 🗙