Skip to content

Commit 9347423

Browse files
authored
.Net: Updated User-Agent header for agent types (#10822)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Resolves: #10447 ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent b1a81e7 commit 9347423

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

dotnet/src/Agents/AzureAI/AzureAIAgent.ClientFactory.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ public static AIProjectClient CreateAzureAIClient(
3333

3434
private static AIProjectClientOptions CreateAzureClientOptions(HttpClient? httpClient)
3535
{
36-
AIProjectClientOptions options =
37-
new()
38-
{
39-
Diagnostics = {
40-
ApplicationId = HttpHeaderConstant.Values.UserAgent,
41-
}
42-
};
36+
AIProjectClientOptions options = new();
4337

4438
options.AddPolicy(new SemanticKernelHeadersPolicy(), HttpPipelinePosition.PerCall);
4539

@@ -57,6 +51,10 @@ private class SemanticKernelHeadersPolicy : HttpPipelineSynchronousPolicy
5751
{
5852
public override void OnSendingRequest(HttpMessage message)
5953
{
54+
message.Request.Headers.Add(
55+
HttpHeaderConstant.Names.UserAgent,
56+
$"{HttpHeaderConstant.Values.UserAgent} {nameof(AzureAIAgent)}");
57+
6058
message.Request.Headers.Add(
6159
HttpHeaderConstant.Names.SemanticKernelVersion,
6260
HttpHeaderConstant.Values.GetAssemblyVersion(typeof(AzureAIAgent)));

dotnet/src/Agents/OpenAI/OpenAIAssistantAgent.ClientFactory.cs

+10-7
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ public static OpenAIClient CreateOpenAIClient(ApiKeyCredential apiKey, Uri? endp
7575

7676
private static AzureOpenAIClientOptions CreateAzureClientOptions(HttpClient? httpClient)
7777
{
78-
AzureOpenAIClientOptions options = new()
79-
{
80-
UserAgentApplicationId = HttpHeaderConstant.Values.UserAgent
81-
};
78+
AzureOpenAIClientOptions options = new();
8279

8380
ConfigureClientOptions(httpClient, options);
8481

@@ -89,7 +86,6 @@ private static OpenAIClientOptions CreateOpenAIClientOptions(Uri? endpoint, Http
8986
{
9087
OpenAIClientOptions options = new()
9188
{
92-
UserAgentApplicationId = HttpHeaderConstant.Values.UserAgent,
9389
Endpoint = endpoint ?? httpClient?.BaseAddress,
9490
};
9591

@@ -101,6 +97,7 @@ private static OpenAIClientOptions CreateOpenAIClientOptions(Uri? endpoint, Http
10197
private static void ConfigureClientOptions(HttpClient? httpClient, ClientPipelineOptions options)
10298
{
10399
options.AddPolicy(CreateRequestHeaderPolicy(HttpHeaderConstant.Names.SemanticKernelVersion, HttpHeaderConstant.Values.GetAssemblyVersion(typeof(OpenAIAssistantAgent))), PipelinePosition.PerCall);
100+
options.AddPolicy(CreateRequestHeaderPolicy(HttpHeaderConstant.Names.UserAgent, $"{HttpHeaderConstant.Values.UserAgent} {nameof(OpenAIAssistantAgent)}"), PipelinePosition.PerCall);
104101

105102
if (httpClient is not null)
106103
{
@@ -114,9 +111,15 @@ private static GenericActionPipelinePolicy CreateRequestHeaderPolicy(string head
114111
=>
115112
new((message) =>
116113
{
117-
if (message?.Request?.Headers?.TryGetValue(headerName, out string? _) == false)
114+
var headers = message?.Request?.Headers;
115+
116+
if (headers is not null)
118117
{
119-
message.Request.Headers.Set(headerName, headerValue);
118+
var value = !headers.TryGetValue(headerName, out string? existingHeaderValue) || string.IsNullOrWhiteSpace(existingHeaderValue) ?
119+
headerValue :
120+
$"{headerValue} {existingHeaderValue}";
121+
122+
headers.Set(headerName, value);
120123
}
121124
});
122125
}

dotnet/src/InternalUtilities/src/Http/HttpHeaderConstant.cs

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public static class Names
1313
{
1414
/// <summary>HTTP header name to use to include the Semantic Kernel package version in all HTTP requests issued by Semantic Kernel.</summary>
1515
public static string SemanticKernelVersion => "Semantic-Kernel-Version";
16+
17+
/// <summary>HTTP User-Agent header name.</summary>
18+
public static string UserAgent => "User-Agent";
1619
}
1720

1821
public static class Values

0 commit comments

Comments
 (0)