question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUERY] What is the configuration structure for configuring the `OpenAIClient`?

See original GitHub issue

Library name and version

Azure.AI.OpenAI 1.0.0-beta.5

Query/Question

I’m currently configuring the OpenAIClient by registering it into DI like this:

builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddOpenAIClient(
        new Uri(builder.Configuration["Azure:OpenAI:Endpoint"]),
        new AzureKeyCredential(builder.Configuration["Azure:OpenAI:ApiKey"])
    );
});

I know there’s also an overload to pass in configuration like this:

builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddOpenAIClient(builder.Configuration.GetSection("Azure:OpenAI"));
});

But I don’t know how to configure the Endpoint and ApiKey with this overload. So what should my configuration structure look like to configure it like the first sample?

Ideally, this would work with .NET’s autoreloading configuration, so I can swap API keys without having to restart my application.

Environment

No response

Issue Analytics

  • State:open
  • Created 5 months ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
IEvangelistcommented, May 4, 2023

After looking a bit at this @Swimburger - there are some folks speculating that if you were to configure a section like this:

{
  "Azure": {
    "OpenAI": {
      "Endpoint": "< your endpoint >",
      "Crendtial": "< your credential >"
    }
  }
}

And then get the section by its name:

builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddOpenAIClient(
        builder.Configuration.GetSection("Azure:OpenAI"));

    // This section would be the following values:
    // {
    //  "Endpoint": "< your endpoint >",
    //  "Crendtial": "< your credential >"
    // }
});

Notice that the property names match the parameter names of the corresponding parameterized call, I wonder if that’s how it maps?

builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddOpenAIClient(
        endpoint: new Uri(builder.Configuration["Azure:OpenAI:Endpoint"]),
        credential: new AzureKeyCredential(builder.Configuration["Azure:OpenAI:ApiKey"])
    );
});

Also, see here, as a somewhat related sample: https://learn.microsoft.com/dotnet/azure/sdk/dependency-injection#configure-a-new-retry-policy

0reactions
sjkpcommented, May 18, 2023

Doesnt seem like that overload works unless you have already registered the OpenAIClient with your DI container

 public static class AzureOpenAIClientBuilderExtensions
  {
    /// <summary> Registers a <see cref="T:Azure.AI.OpenAI.OpenAIClient" /> instance. </summary>
    /// <param name="builder"> The builder to register with. </param>
    /// <param name="endpoint">
    /// Supported Cognitive Services endpoints (protocol and hostname, for example:
    /// https://westus.api.cognitive.microsoft.com).
    /// </param>
    /// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
    public static IAzureClientBuilder<OpenAIClient, OpenAIClientOptions> AddOpenAIClient<TBuilder>(this TBuilder builder, Uri endpoint, AzureKeyCredential credential) where TBuilder : IAzureClientFactoryBuilder
    {
      return builder.RegisterClientFactory<OpenAIClient, OpenAIClientOptions>((Func<OpenAIClientOptions, OpenAIClient>) (options => new OpenAIClient(endpoint, credential, options)));
    }

    /// <summary> Registers a <see cref="T:Azure.AI.OpenAI.OpenAIClient" /> instance. </summary>
    /// <param name="builder"> The builder to register with. </param>
    /// <param name="endpoint">
    /// Supported Cognitive Services endpoints (protocol and hostname, for example:
    /// https://westus.api.cognitive.microsoft.com).
    /// </param>
    public static IAzureClientBuilder<OpenAIClient, OpenAIClientOptions> AddOpenAIClient<TBuilder>(this TBuilder builder, Uri endpoint) where TBuilder : IAzureClientFactoryBuilderWithCredential
    {
      return builder.RegisterClientFactory<OpenAIClient, OpenAIClientOptions>((Func<OpenAIClientOptions, TokenCredential, OpenAIClient>) ((options, cred) => new OpenAIClient(endpoint, cred, options)), true);
    }

    /// <summary> Registers a <see cref="T:Azure.AI.OpenAI.OpenAIClient" /> instance. </summary>
    /// <param name="builder"> The builder to register with. </param>
    /// <param name="configuration"> The configuration values. </param>
    public static IAzureClientBuilder<OpenAIClient, OpenAIClientOptions> AddOpenAIClient<TBuilder, TConfiguration>(this TBuilder builder, TConfiguration configuration) where TBuilder : IAzureClientFactoryBuilderWithConfiguration<TConfiguration>
    {
      return builder.RegisterClientFactory<OpenAIClient, OpenAIClientOptions>(configuration);
    }
  }

```
Read more comments on GitHub >

github_iconTop Results From Across the Web

Get started generating text using Azure OpenAI Service
You can experiment with the configuration settings such as temperature and pre-response text to improve the performance of your task.
Read more >
Get started using GPT-35-Turbo and GPT-4 with Azure ...
Walkthrough on how to get started with GPT-35-Turbo and GPT-4 on Azure OpenAI Service.
Read more >
Building an Azure OpenAI-Powered PDF Question- ...
Configure OpenAI API client. OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));. Step 3: Load the PDF File and ...
Read more >
How to use the Azure OpenAI Embedding model to find ...
This article explains how to use OpenAI's text-embedding-ada-002 model for text embedding to find the most relevant documents at a lower ...
Read more >
Beginner's Guide to OpenAI's GPT-3.5-Turbo Model
From GPT-3 to GPT-3.5-Turbo: Understanding the Latest Upgrades in OpenAI's Language Model API.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found