Skip to content

Commit

Permalink
Merge branch 'main' into function-kernel-results-to-string
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrostruk authored Oct 12, 2023
2 parents 62d7687 + f5dc51e commit 292c85d
Show file tree
Hide file tree
Showing 36 changed files with 1,075 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ private protected AzureOpenAIClientBase(
Verify.StartsWith(endpoint, "https://", "The Azure OpenAI endpoint must start with 'https://'");
Verify.NotNullOrWhiteSpace(apiKey);

var options = GetClientOptions();
if (httpClient != null)
{
options.Transport = new HttpClientTransport(httpClient);
}
var options = GetClientOptions(httpClient);

this.ModelId = modelId;
this.Client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(apiKey), options);
Expand All @@ -70,11 +66,7 @@ private protected AzureOpenAIClientBase(
Verify.NotNullOrWhiteSpace(endpoint);
Verify.StartsWith(endpoint, "https://", "The Azure OpenAI endpoint must start with 'https://'");

var options = GetClientOptions();
if (httpClient != null)
{
options.Transport = new HttpClientTransport(httpClient);
}
var options = GetClientOptions(httpClient);

this.ModelId = modelId;
this.Client = new OpenAIClient(new Uri(endpoint), credential, options);
Expand Down Expand Up @@ -103,17 +95,26 @@ private protected AzureOpenAIClientBase(
/// <summary>
/// Options used by the Azure OpenAI client, e.g. User Agent.
/// </summary>
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <returns>An instance of <see cref="OpenAIClientOptions"/>.</returns>
private static OpenAIClientOptions GetClientOptions()
private static OpenAIClientOptions GetClientOptions(HttpClient? httpClient)
{
return new OpenAIClientOptions
var options = new OpenAIClientOptions
{
Diagnostics =
{
IsTelemetryEnabled = Telemetry.IsTelemetryEnabled,
ApplicationId = Telemetry.HttpUserAgent,
}
};

if (httpClient != null)
{
options.Transport = new HttpClientTransport(httpClient);
options.RetryPolicy = new RetryPolicy(maxRetries: 0); //Disabling Azure SDK retry policy to use the one provided by the custom HTTP client.
}

return options;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ private protected OpenAIClientBase(

this.ModelId = modelId;

var options = GetClientOptions();
if (httpClient != null)
{
options.Transport = new HttpClientTransport(httpClient);
}
var options = GetClientOptions(httpClient);

if (!string.IsNullOrWhiteSpace(organization))
{
Expand Down Expand Up @@ -86,16 +82,25 @@ private protected void LogActionDetails([CallerMemberName] string? callerMemberN
/// <summary>
/// Options used by the OpenAI client, e.g. User Agent.
/// </summary>
/// <returns>An instance of <see cref="OpenAIClientOptions"/> with the configured options.</returns>
private static OpenAIClientOptions GetClientOptions()
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <returns>An instance of <see cref="OpenAIClientOptions"/>.</returns>
private static OpenAIClientOptions GetClientOptions(HttpClient? httpClient)
{
return new OpenAIClientOptions
var options = new OpenAIClientOptions
{
Diagnostics =
{
IsTelemetryEnabled = Telemetry.IsTelemetryEnabled,
ApplicationId = Telemetry.HttpUserAgent,
}
};

if (httpClient != null)
{
options.Transport = new HttpClientTransport(httpClient);
options.RetryPolicy = new RetryPolicy(maxRetries: 0); //Disabling Azure SDK retry policy to use the one provided by the custom HTTP client.
}

return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ private static OpenAIClientOptions CreateOpenAIClientOptions(ILoggerFactory logg
OpenAIClientOptions options = new();
#pragma warning disable CA2000 // Dispose objects before losing scope
options.Transport = new HttpClientTransport(HttpClientProvider.GetHttpClient(httpHandlerFactory, httpClient, loggerFactory));
options.RetryPolicy = new RetryPolicy(maxRetries: 0); //Disabling Azure SDK retry policy to use the one provided by the delegating handler factory or the HTTP client.
#pragma warning restore CA2000 // Dispose objects before losing scope

return options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace SemanticKernel.IntegrationTests.Connectors.OpenAI;

public sealed class OpenAICompletionTests : IDisposable
{
private readonly KernelBuilder _kernelBuilder;
private readonly IConfigurationRoot _configuration;

public OpenAICompletionTests(ITestOutputHelper output)
Expand All @@ -37,6 +38,9 @@ public OpenAICompletionTests(ITestOutputHelper output)
.AddEnvironmentVariables()
.AddUserSecrets<OpenAICompletionTests>()
.Build();

this._kernelBuilder = new KernelBuilder();
this._kernelBuilder.WithRetryBasic();
}

[Theory(Skip = "OpenAI will often throttle requests. This test is for manual verification.")]
Expand All @@ -47,7 +51,7 @@ public async Task OpenAITestAsync(string prompt, string expectedAnswerContains)
var openAIConfiguration = this._configuration.GetSection("OpenAI").Get<OpenAIConfiguration>();
Assert.NotNull(openAIConfiguration);

IKernel target = Kernel.Builder
IKernel target = this._kernelBuilder
.WithLoggerFactory(this._logger)
.WithOpenAITextCompletionService(
serviceId: openAIConfiguration.ServiceId,
Expand All @@ -70,7 +74,7 @@ public async Task OpenAITestAsync(string prompt, string expectedAnswerContains)
public async Task OpenAIChatAsTextTestAsync(string prompt, string expectedAnswerContains)
{
// Arrange
KernelBuilder builder = Kernel.Builder.WithLoggerFactory(this._logger);
KernelBuilder builder = this._kernelBuilder.WithLoggerFactory(this._logger);

this.ConfigureChatOpenAI(builder);

Expand All @@ -89,7 +93,7 @@ public async Task OpenAIChatAsTextTestAsync(string prompt, string expectedAnswer
public async Task CanUseOpenAiChatForTextCompletionAsync()
{
// Note: we use OpenAi Chat Completion and GPT 3.5 Turbo
KernelBuilder builder = Kernel.Builder.WithLoggerFactory(this._logger);
KernelBuilder builder = this._kernelBuilder.WithLoggerFactory(this._logger);
this.ConfigureChatOpenAI(builder);

IKernel target = builder.Build();
Expand All @@ -110,7 +114,7 @@ public async Task CanUseOpenAiChatForTextCompletionAsync()
public async Task AzureOpenAITestAsync(bool useChatModel, string prompt, string expectedAnswerContains)
{
// Arrange
var builder = Kernel.Builder.WithLoggerFactory(this._logger);
var builder = this._kernelBuilder.WithLoggerFactory(this._logger);

if (useChatModel)
{
Expand Down Expand Up @@ -145,7 +149,7 @@ public async Task OpenAIHttpRetryPolicyTestAsync(string prompt, string expectedO
OpenAIConfiguration? openAIConfiguration = this._configuration.GetSection("OpenAI").Get<OpenAIConfiguration>();
Assert.NotNull(openAIConfiguration);

IKernel target = Kernel.Builder
IKernel target = this._kernelBuilder
.WithLoggerFactory(this._testOutputHelper)
.WithRetryBasic(retryConfig)
.WithOpenAITextCompletionService(
Expand Down Expand Up @@ -173,7 +177,7 @@ public async Task AzureOpenAIHttpRetryPolicyTestAsync(string prompt, string expe
var retryConfig = new BasicRetryConfig();
retryConfig.RetryableStatusCodes.Add(HttpStatusCode.Unauthorized);

KernelBuilder builder = Kernel.Builder
KernelBuilder builder = this._kernelBuilder
.WithLoggerFactory(this._testOutputHelper)
.WithRetryBasic(retryConfig);

Expand Down Expand Up @@ -205,7 +209,7 @@ public async Task OpenAIHttpInvalidKeyShouldReturnErrorDetailAsync()
Assert.NotNull(openAIConfiguration);

// Use an invalid API key to force a 401 Unauthorized response
IKernel target = Kernel.Builder
IKernel target = this._kernelBuilder
.WithOpenAITextCompletionService(
modelId: openAIConfiguration.ModelId,
apiKey: "INVALID_KEY",
Expand All @@ -227,7 +231,7 @@ public async Task AzureOpenAIHttpInvalidKeyShouldReturnErrorDetailAsync()
var azureOpenAIConfiguration = this._configuration.GetSection("AzureOpenAI").Get<AzureOpenAIConfiguration>();
Assert.NotNull(azureOpenAIConfiguration);

IKernel target = Kernel.Builder
IKernel target = this._kernelBuilder
.WithLoggerFactory(this._testOutputHelper)
.WithAzureTextCompletionService(
deploymentName: azureOpenAIConfiguration.DeploymentName,
Expand All @@ -251,7 +255,7 @@ public async Task AzureOpenAIHttpExceededMaxTokensShouldReturnErrorDetailAsync()
Assert.NotNull(azureOpenAIConfiguration);

// Arrange
IKernel target = Kernel.Builder
IKernel target = this._kernelBuilder
.WithLoggerFactory(this._testOutputHelper)
.WithAzureTextCompletionService(
deploymentName: azureOpenAIConfiguration.DeploymentName,
Expand Down Expand Up @@ -282,7 +286,7 @@ public async Task CompletionWithDifferentLineEndingsAsync(string lineEnding, AIS

const string ExpectedAnswerContains = "<result>John</result>";

IKernel target = Kernel.Builder.WithLoggerFactory(this._logger).Build();
IKernel target = this._kernelBuilder.WithLoggerFactory(this._logger).Build();

this._serviceConfiguration[service](target);

Expand All @@ -299,7 +303,7 @@ public async Task CompletionWithDifferentLineEndingsAsync(string lineEnding, AIS
public async Task AzureOpenAIInvokePromptTestAsync()
{
// Arrange
var builder = Kernel.Builder.WithLoggerFactory(this._logger);
var builder = this._kernelBuilder.WithLoggerFactory(this._logger);
this.ConfigureAzureOpenAI(builder);
IKernel target = builder.Build();

Expand All @@ -316,7 +320,7 @@ public async Task AzureOpenAIInvokePromptTestAsync()
public async Task AzureOpenAIDefaultValueTestAsync()
{
// Arrange
var builder = Kernel.Builder.WithLoggerFactory(this._logger);
var builder = this._kernelBuilder.WithLoggerFactory(this._logger);
this.ConfigureAzureOpenAI(builder);
IKernel target = builder.Build();

Expand All @@ -333,7 +337,7 @@ public async Task AzureOpenAIDefaultValueTestAsync()
public async Task MultipleServiceLoadPromptConfigTestAsync()
{
// Arrange
var builder = Kernel.Builder.WithLoggerFactory(this._logger);
var builder = this._kernelBuilder.WithLoggerFactory(this._logger);
this.ConfigureAzureOpenAI(builder);
this.ConfigureInvalidAzureOpenAI(builder);

Expand Down Expand Up @@ -397,22 +401,6 @@ private void Dispose(bool disposing)
}
}

private void ConfigureOpenAI(KernelBuilder kernelBuilder)
{
var openAIConfiguration = this._configuration.GetSection("OpenAI").Get<OpenAIConfiguration>();

Assert.NotNull(openAIConfiguration);
Assert.NotNull(openAIConfiguration.ModelId);
Assert.NotNull(openAIConfiguration.ApiKey);
Assert.NotNull(openAIConfiguration.ServiceId);

kernelBuilder.WithOpenAITextCompletionService(
modelId: openAIConfiguration.ModelId,
apiKey: openAIConfiguration.ApiKey,
serviceId: openAIConfiguration.ServiceId,
setAsDefault: true);
}

private void ConfigureChatOpenAI(KernelBuilder kernelBuilder)
{
var openAIConfiguration = this._configuration.GetSection("OpenAI").Get<OpenAIConfiguration>();
Expand Down
4 changes: 3 additions & 1 deletion dotnet/src/IntegrationTests/Planners/PlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,9 @@ private IKernel InitializeKernel(bool useEmbeddings = false, bool useChatModel =
AzureOpenAIConfiguration? azureOpenAIEmbeddingsConfiguration = this._configuration.GetSection("AzureOpenAIEmbeddings").Get<AzureOpenAIConfiguration>();
Assert.NotNull(azureOpenAIEmbeddingsConfiguration);

var builder = Kernel.Builder.WithLoggerFactory(this._loggerFactory);
var builder = Kernel.Builder
.WithLoggerFactory(this._loggerFactory)
.WithRetryBasic();

if (useChatModel)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void CanCallToPlanFromXml()
Assert.NotNull(azureOpenAIConfiguration);

IKernel kernel = Kernel.Builder
.WithRetryBasic()
.WithAzureTextCompletionService(
deploymentName: azureOpenAIConfiguration.DeploymentName,
endpoint: azureOpenAIConfiguration.Endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private IKernel InitializeKernel(bool useEmbeddings = false, bool useChatModel =
Assert.NotNull(azureOpenAIEmbeddingsConfiguration);

var builder = Kernel.Builder.WithLoggerFactory(this._logger);
builder.WithRetryBasic();

if (useChatModel)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ private IKernel InitializeKernel(bool useEmbeddings = false, bool useChatModel =
AzureOpenAIConfiguration? azureOpenAIEmbeddingsConfiguration = this._configuration.GetSection("AzureOpenAIEmbeddings").Get<AzureOpenAIConfiguration>();
Assert.NotNull(azureOpenAIEmbeddingsConfiguration);

var builder = Kernel.Builder.WithLoggerFactory(this._loggerFactory);
var builder = Kernel.Builder
.WithLoggerFactory(this._loggerFactory)
.WithRetryBasic();

if (useChatModel)
{
Expand Down
5 changes: 5 additions & 0 deletions python/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
"OPENAI",
"skfunction"
],
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
}
Loading

0 comments on commit 292c85d

Please sign in to comment.