Skip to content

MEAI Embeddings

Generate embeddings using the Microsoft.Extensions.AI IEmbeddingGenerator interface.

This example assumes using tryAGI.OpenAI; is in scope and apiKey contains your tryAGI.OpenAI API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using var client = new OpenAiClient(apiKey);

// using Meai = Microsoft.Extensions.AI;
Meai.IEmbeddingGenerator<string, Meai.Embedding<float>> generator = client;

var result = await generator.GenerateAsync(
    new List<string> { "Hello, world!" },
    new Meai.EmbeddingGenerationOptions
    {
        ModelId = "text-embedding-3-small",
    });

Console.WriteLine($"Embedding dimension: {result[0].Vector.Length}");