Skip to content

Acquire the available models using OpenAI API

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

The OpenAI API is powered by a family of models with different capabilities and price points. You can also customize the base models for your specific use case with fine-tuning.

More info: https://platform.openai.com/docs/models/models

static async Task Main(string[] args)
{
    // This example demonstrates, how you can query the available OpenAI models,
    // which can be used for different purposes.
    // You can find additional information here: https://platform.openai.com/docs/models/overview
    //
    // 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>()!;
    HttpOperationResult<ModelsResponse> response = 
        await openAi.ModelService.GetAsync()
            .ConfigureAwait(false);

    if (response.IsSuccess)
    {
        string classText = KnownModelTypesClassGenerator
            .GenerateModelsLookup(response.Result!);

        Console.WriteLine(classText);

        File.WriteAllText("KnownModelTypes.cs", classText);
    }
    else
    {
        Console.WriteLine(response);
    }
}