|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +using System; |
| 3 | +using System.ClientModel; |
| 4 | +using System.ClientModel.Primitives; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Threading; |
| 7 | +using Azure.AI.OpenAI; |
| 8 | +using Azure.Core; |
| 9 | +using Microsoft.SemanticKernel.Http; |
| 10 | +using OpenAI; |
| 11 | + |
| 12 | +namespace Microsoft.SemanticKernel.Agents.OpenAI; |
| 13 | + |
| 14 | +public sealed partial class OpenAIAssistantAgent : KernelAgent |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Specifies a key that avoids an exception from OpenAI Client when a custom endpoint is provided without an API key. |
| 18 | + /// </summary> |
| 19 | + private const string SingleSpaceKey = " "; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Produces an <see cref="AzureOpenAIClient"/>. |
| 23 | + /// </summary> |
| 24 | + /// <param name="apiKey">The API key.</param> |
| 25 | + /// <param name="endpoint">The service endpoint.</param> |
| 26 | + /// <param name="httpClient">A custom <see cref="HttpClient"/> for HTTP requests.</param> |
| 27 | + public static AzureOpenAIClient CreateAzureOpenAIClient(ApiKeyCredential apiKey, Uri endpoint, HttpClient? httpClient = null) |
| 28 | + { |
| 29 | + Verify.NotNull(apiKey, nameof(apiKey)); |
| 30 | + Verify.NotNull(endpoint, nameof(endpoint)); |
| 31 | + |
| 32 | + AzureOpenAIClientOptions clientOptions = CreateAzureClientOptions(httpClient); |
| 33 | + |
| 34 | + return new AzureOpenAIClient(endpoint, apiKey!, clientOptions); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Produces an <see cref="AzureOpenAIClient"/>. |
| 39 | + /// </summary> |
| 40 | + /// <param name="credential">The credentials.</param> |
| 41 | + /// <param name="endpoint">The service endpoint.</param> |
| 42 | + /// <param name="httpClient">A custom <see cref="HttpClient"/> for HTTP requests.</param> |
| 43 | + public static AzureOpenAIClient CreateAzureOpenAIClient(TokenCredential credential, Uri endpoint, HttpClient? httpClient = null) |
| 44 | + { |
| 45 | + Verify.NotNull(credential, nameof(credential)); |
| 46 | + Verify.NotNull(endpoint, nameof(endpoint)); |
| 47 | + |
| 48 | + AzureOpenAIClientOptions clientOptions = CreateAzureClientOptions(httpClient); |
| 49 | + |
| 50 | + return new AzureOpenAIClient(endpoint, credential, clientOptions); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Produces an <see cref="OpenAIClient"/>. |
| 55 | + /// </summary> |
| 56 | + /// <param name="endpoint">An optional endpoint.</param> |
| 57 | + /// <param name="httpClient">A custom <see cref="HttpClient"/> for HTTP requests.</param> |
| 58 | + public static OpenAIClient CreateOpenAIClient(Uri? endpoint = null, HttpClient? httpClient = null) |
| 59 | + { |
| 60 | + OpenAIClientOptions clientOptions = CreateOpenAIClientOptions(endpoint, httpClient); |
| 61 | + return new OpenAIClient(new ApiKeyCredential(SingleSpaceKey), clientOptions); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Produces an <see cref="OpenAIClient"/>. |
| 66 | + /// </summary> |
| 67 | + /// <param name="apiKey">The API key.</param> |
| 68 | + /// <param name="endpoint">An optional endpoint.</param> |
| 69 | + /// <param name="httpClient">A custom <see cref="HttpClient"/> for HTTP requests.</param> |
| 70 | + public static OpenAIClient CreateOpenAIClient(ApiKeyCredential apiKey, Uri? endpoint = null, HttpClient? httpClient = null) |
| 71 | + { |
| 72 | + OpenAIClientOptions clientOptions = CreateOpenAIClientOptions(endpoint, httpClient); |
| 73 | + return new OpenAIClient(apiKey, clientOptions); |
| 74 | + } |
| 75 | + |
| 76 | + private static AzureOpenAIClientOptions CreateAzureClientOptions(HttpClient? httpClient) |
| 77 | + { |
| 78 | + AzureOpenAIClientOptions options = new() |
| 79 | + { |
| 80 | + UserAgentApplicationId = HttpHeaderConstant.Values.UserAgent |
| 81 | + }; |
| 82 | + |
| 83 | + ConfigureClientOptions(httpClient, options); |
| 84 | + |
| 85 | + return options; |
| 86 | + } |
| 87 | + |
| 88 | + private static OpenAIClientOptions CreateOpenAIClientOptions(Uri? endpoint, HttpClient? httpClient) |
| 89 | + { |
| 90 | + OpenAIClientOptions options = new() |
| 91 | + { |
| 92 | + UserAgentApplicationId = HttpHeaderConstant.Values.UserAgent, |
| 93 | + Endpoint = endpoint ?? httpClient?.BaseAddress, |
| 94 | + }; |
| 95 | + |
| 96 | + ConfigureClientOptions(httpClient, options); |
| 97 | + |
| 98 | + return options; |
| 99 | + } |
| 100 | + |
| 101 | + private static void ConfigureClientOptions(HttpClient? httpClient, ClientPipelineOptions options) |
| 102 | + { |
| 103 | + options.AddPolicy(CreateRequestHeaderPolicy(HttpHeaderConstant.Names.SemanticKernelVersion, HttpHeaderConstant.Values.GetAssemblyVersion(typeof(OpenAIAssistantAgent))), PipelinePosition.PerCall); |
| 104 | + |
| 105 | + if (httpClient is not null) |
| 106 | + { |
| 107 | + options.Transport = new HttpClientPipelineTransport(httpClient); |
| 108 | + options.RetryPolicy = new ClientRetryPolicy(maxRetries: 0); // Disable retry policy if and only if a custom HttpClient is provided. |
| 109 | + options.NetworkTimeout = Timeout.InfiniteTimeSpan; // Disable default timeout |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static GenericActionPipelinePolicy CreateRequestHeaderPolicy(string headerName, string headerValue) |
| 114 | + => |
| 115 | + new((message) => |
| 116 | + { |
| 117 | + if (message?.Request?.Headers?.TryGetValue(headerName, out string? _) == false) |
| 118 | + { |
| 119 | + message.Request.Headers.Set(headerName, headerValue); |
| 120 | + } |
| 121 | + }); |
| 122 | +} |
0 commit comments