Skip to content

.Net: Endpoint and API key identification heuristic for AzureOpenAI services #7065

Description

@SergeyMenshykh

Consider porting the same endpoint and API key identification heuristic, recently added to the new OpenAI.ClientCore class, to the AzureOpenAI.ClientCore:

internal ClientCore(
    string modelId,
    string? apiKey = null,
    string? organizationId = null,
    Uri? endpoint = null,
    HttpClient? httpClient = null,
    ILogger? logger = null)
{
    Verify.NotNullOrWhiteSpace(modelId);

    this.Logger = logger ?? NullLogger.Instance;
    this.ModelId = modelId;

    this.AddAttribute(AIServiceExtensions.ModelIdKey, modelId);

    // Accepts the endpoint if provided, otherwise uses the default OpenAI endpoint.
    this.Endpoint = endpoint ?? httpClient?.BaseAddress;
    if (this.Endpoint is null)
    {
        Verify.NotNullOrWhiteSpace(apiKey); // For Public OpenAI Endpoint a key must be provided.
        this.Endpoint = new Uri(OpenAIV1Endpoint);
    }
    else if (string.IsNullOrEmpty(apiKey))
    {
        // Avoids an exception from OpenAI Client when a custom endpoint is provided without an API key.
        apiKey = SingleSpace;
    }

    this.AddAttribute(AIServiceExtensions.EndpointKey, this.Endpoint.ToString());

    var options = GetOpenAIClientOptions(httpClient, this.Endpoint);
    if (!string.IsNullOrWhiteSpace(organizationId))
    {
        options.AddPolicy(CreateRequestHeaderPolicy("OpenAI-Organization", organizationId!), PipelinePosition.PerCall);

        this.AddAttribute(ClientCore.OrganizationKey, organizationId);
    }

    this.Client = new OpenAIClient(apiKey!, options);
}

Related comment - #7060 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETIssue or Pull requests regarding .NET codeai connectorAnything related to AI connectorsopenai_sdk_v2Related to the OpenAI V2 SDK upgradesk team issueA tag to denote issues that where created by the Semantic Kernel team (i.e., not the community)

    Type

    No type

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions