Skip to content

Embeddings

Zoltan Juhasz edited this page May 2, 2023 · 1 revision

Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.

More info: https://platform.openai.com/docs/api-reference/embeddings

static async Task Main(string[] args)
{
    // This example demonstrates, how you can use embedding feature of OpenAI.
    // This feature is useful for search, clustering, recommendations, anomaly detection, etc
    // More information: https://platform.openai.com/docs/guides/embeddings/what-are-embeddings
    //
    // The very first step to create an account at OpenAI: https://platform.openai.com/
    // Using the loggedIn account, navigate to https://platform.openai.com/account/api-keys
    // Here you can create apiKey(s)

    using var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((builder, services) =>
    {
        services.AddForgeOpenAI(options => {
            options.AuthenticationInfo = builder.Configuration["OpenAI:ApiKey"]!;
        });
    })
    .Build();

    IOpenAIService openAi = host.Services.GetService<IOpenAIService>()!;

    EmbeddingsRequest request = new EmbeddingsRequest();
    request.InputTextsForEmbeddings.Add("The food was delicious and the waiter...");

    HttpOperationResult<EmbeddingsResponse> response = 
        await openAi.EmbeddingsService
            .GetAsync(request, CancellationToken.None)
                .ConfigureAwait(false);;

    if (response.IsSuccess)
    {
        Console.WriteLine(response.Result!);
    }
    else
    {
        Console.WriteLine(response);
    }

}