Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.AzureOpenAI", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.AzureOpenAI.UnitTests", "src\Connectors\Connectors.AzureOpenAI.UnitTests\Connectors.AzureOpenAI.UnitTests.csproj", "{DB219924-208B-4CDD-8796-EE424689901E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "openai", "openai", "{2E79AD99-632F-411F-B3A5-1BAF3F5F89AB}"
ProjectSection(SolutionItems) = preProject
src\InternalUtilities\openai\OpenAIUtilities.props = src\InternalUtilities\openai\OpenAIUtilities.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Policies", "Policies", "{7308EF7D-5F9A-47B2-A62F-0898603262A8}"
ProjectSection(SolutionItems) = preProject
src\InternalUtilities\openai\Policies\GeneratedActionPipelinePolicy.cs = src\InternalUtilities\openai\Policies\GeneratedActionPipelinePolicy.cs
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -932,6 +942,8 @@ Global
{FDEB4884-89B9-4656-80A0-57C7464490F7} = {831DDCA2-7D2C-4C31-80DB-6BDB3E1F7AE0}
{6744272E-8326-48CE-9A3F-6BE227A5E777} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
{DB219924-208B-4CDD-8796-EE424689901E} = {1B4CBDE0-10C2-4E7D-9CD0-FE7586C96ED1}
{2E79AD99-632F-411F-B3A5-1BAF3F5F89AB} = {4D3DAE63-41C6-4E1C-A35A-E77BDFC40675}
{7308EF7D-5F9A-47B2-A62F-0898603262A8} = {2E79AD99-632F-411F-B3A5-1BAF3F5F89AB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBDC56A3-86AD-4323-AA0F-201E59123B83}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<!-- IMPORT NUGET PACKAGE SHARED PROPERTIES -->
<Import Project="$(RepoRoot)/dotnet/nuget/nuget-package.props" />
<Import Project="$(RepoRoot)/dotnet/src/InternalUtilities/src/InternalUtilities.props" />
<Import Project="$(RepoRoot)/dotnet/src/InternalUtilities/openai/OpenAIUtilities.props" />
Comment thread
rogerbarreto marked this conversation as resolved.

<PropertyGroup>
<!-- NuGet Package Settings -->
Expand Down
15 changes: 13 additions & 2 deletions dotnet/src/Connectors/Connectors.OpenAIV2/Core/ClientCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal ClientCore(
var options = GetOpenAIClientOptions(httpClient, this.Endpoint);
if (!string.IsNullOrWhiteSpace(organizationId))
{
options.AddPolicy(new AddHeaderRequestPolicy("OpenAI-Organization", organizationId!), PipelinePosition.PerCall);
options.AddPolicy(CreateRequestHeaderPolicy("OpenAI-Organization", organizationId!), PipelinePosition.PerCall);

this.AddAttribute(ClientCore.OrganizationKey, organizationId);
}
Expand Down Expand Up @@ -184,7 +184,7 @@ private static OpenAIClientOptions GetOpenAIClientOptions(HttpClient? httpClient
Endpoint = endpoint
};

options.AddPolicy(new AddHeaderRequestPolicy(HttpHeaderConstant.Names.SemanticKernelVersion, HttpHeaderConstant.Values.GetAssemblyVersion(typeof(ClientCore))), PipelinePosition.PerCall);
options.AddPolicy(CreateRequestHeaderPolicy(HttpHeaderConstant.Names.SemanticKernelVersion, HttpHeaderConstant.Values.GetAssemblyVersion(typeof(ClientCore))), PipelinePosition.PerCall);

if (httpClient is not null)
{
Expand Down Expand Up @@ -213,4 +213,15 @@ private static async Task<T> RunRequestAsync<T>(Func<Task<T>> request)
throw e.ToHttpOperationException();
}
}

private static GenericActionPipelinePolicy CreateRequestHeaderPolicy(string headerName, string headerValue)
{
return new GenericActionPipelinePolicy((message) =>
{
if (message?.Request?.Headers?.TryGetValue(headerName, out string? _) == false)
{
message.Request.Headers.Set(headerName, headerValue);
}
});
}
}

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions dotnet/src/InternalUtilities/openai/OpenAIUtilities.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<ItemGroup>
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/openai/**/*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft. All rights reserved.

/* Phase 03
Comment thread
rogerbarreto marked this conversation as resolved.
Adapted from OpenAI SDK original policy with warning updates.

Original file: https://github.com/openai/openai-dotnet/blob/0b97311f58dfb28bd883d990f68d548da040a807/src/Utility/GenericActionPipelinePolicy.cs#L8
*/

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;

/// <summary>
/// Generic action pipeline policy for processing messages.
/// </summary>
[ExcludeFromCodeCoverage]
internal sealed class GenericActionPipelinePolicy : PipelinePolicy
{
private readonly Action<PipelineMessage> _processMessageAction;

internal GenericActionPipelinePolicy(Action<PipelineMessage> processMessageAction)
{
this._processMessageAction = processMessageAction;
}

public override void Process(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
{
this._processMessageAction(message);
if (currentIndex < pipeline.Count - 1)
{
pipeline[currentIndex + 1].Process(message, pipeline, currentIndex + 1);
}
}

public override async ValueTask ProcessAsync(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
{
this._processMessageAction(message);
if (currentIndex < pipeline.Count - 1)
{
await pipeline[currentIndex + 1].ProcessAsync(message, pipeline, currentIndex + 1).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageReference Include="System.Threading.Tasks.Extensions" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="System.Memory.Data" />
<PackageReference Include="System.ClientModel" />
</ItemGroup>

<ItemGroup>
Expand All @@ -38,6 +39,7 @@

<ItemGroup>
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/test/AssertExtensions.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/openai/**/*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
// Copyright (c) Microsoft. All rights reserved.

using System.ClientModel.Primitives;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Xunit;

namespace SemanticKernel.Connectors.OpenAI.UnitTests.Core.Models;
namespace SemanticKernel.UnitTests.Utilities.OpenAI;

public class AddHeaderRequestPolicyTests
public class GenericActionPipelinePolicyTests
{
[Fact]
public void ItCanBeInstantiated()
{
// Arrange
var headerName = "headerName";
var headerValue = "headerValue";

// Act
var addHeaderRequestPolicy = new AddHeaderRequestPolicy(headerName, headerValue);
var addHeaderRequestPolicy = new GenericActionPipelinePolicy((message) => { });

// Assert
Assert.NotNull(addHeaderRequestPolicy);
}

[Fact]
public void ItOnSendingRequestAddsHeaderToRequest()
public void ItProcessAddsHeaderToRequest()
{
// Arrange
var headerName = "headerName";
var headerValue = "headerValue";
var addHeaderRequestPolicy = new AddHeaderRequestPolicy(headerName, headerValue);
var sut = new GenericActionPipelinePolicy((message) => { message.Request.Headers.Add(headerName, headerValue); });

var pipeline = ClientPipeline.Create();
var message = pipeline.CreateMessage();

// Act
addHeaderRequestPolicy.OnSendingRequest(message);
sut.Process(message, [sut], 0);

// Assert
message.Request.Headers.TryGetValue(headerName, out var value);
Expand Down