Skip to content

.Net: Add model diagnostics to non-streaming APIs #6150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 8 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: 8 additions & 4 deletions dotnet/src/Agents/Abstractions/Agents.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
<PropertyGroup>
<!-- NuGet Package Settings -->
<Title>Semantic Kernel Agents - Abstractions</Title>
<Description>Semantic Kernel Agents abstractions. This package is automatically installed by Semantic Kernel Agents packages if needed.</Description>
<Description>Semantic Kernel Agents abstractions. This package is automatically installed by
Semantic Kernel Agents packages if needed.</Description>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Diagnostics/*" Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Diagnostics/*"
Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/System/AppContextSwitchHelper.cs"
Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
Expand All @@ -31,10 +35,10 @@
<ItemGroup>
<ProjectReference Include="..\..\SemanticKernel.Core\SemanticKernel.Core.csproj" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="SemanticKernel.Agents.UnitTests" />
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>

</Project>
</Project>
13 changes: 9 additions & 4 deletions dotnet/src/Agents/Core/Agents.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
<PropertyGroup>
<!-- NuGet Package Settings -->
<Title>Semantic Kernel Agents - Core</Title>
<Description>Defines core set of concrete Agent and AgentChat classes, based on the Agent Abstractions.</Description>
<Description>Defines core set of concrete Agent and AgentChat classes, based on the Agent
Abstractions.</Description>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Diagnostics/*" Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/System/TypeConverterFactory.cs" Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Diagnostics/*"
Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/System/TypeConverterFactory.cs"
Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/System/AppContextSwitchHelper.cs"
Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
Expand All @@ -33,4 +38,4 @@
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>

</Project>
</Project>
16 changes: 11 additions & 5 deletions dotnet/src/Agents/OpenAI/Agents.OpenAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Diagnostics/*" Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Http/*" Link="%(RecursiveDir)Http/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Text/*" Link="%(RecursiveDir)Text/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/System/IListExtensions.cs" Link="%(RecursiveDir)System/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Diagnostics/*"
Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Http/*"
Link="%(RecursiveDir)Http/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/Text/*"
Link="%(RecursiveDir)Text/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/System/IListExtensions.cs"
Link="%(RecursiveDir)System/%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/src/System/AppContextSwitchHelper.cs"
Link="%(RecursiveDir)Utilities/%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
Expand All @@ -39,4 +45,4 @@
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -161,11 +162,29 @@ public async Task<IReadOnlyList<ChatMessageContent>> GenerateChatMessageAsync(

for (state.Iteration = 1; ; state.Iteration++)
{
var geminiResponse = await this.SendRequestAndReturnValidGeminiResponseAsync(
this._chatGenerationEndpoint, state.GeminiRequest, cancellationToken)
.ConfigureAwait(false);
GeminiResponse geminiResponse;
List<GeminiChatMessageContent> chatResponses;
using (var activity = ModelDiagnostics.StartCompletionActivity(
this._chatGenerationEndpoint, this._modelId, "Google", chatHistory, executionSettings))
{
try
{
geminiResponse = await this.SendRequestAndReturnValidGeminiResponseAsync(
this._chatGenerationEndpoint, state.GeminiRequest, cancellationToken)
.ConfigureAwait(false);
chatResponses = this.ProcessChatResponse(geminiResponse);
}
catch (Exception ex)
{
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}

var chatResponses = this.ProcessChatResponse(geminiResponse);
activity?.SetCompletionResponse(
chatResponses,
geminiResponse.UsageMetadata?.PromptTokenCount,
geminiResponse.UsageMetadata?.CandidatesTokenCount);
}

// If we don't want to attempt to invoke any functions, just return the result.
// Or if we are auto-invoking but we somehow end up with other than 1 choice even though only 1 was requested, similarly bail.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -136,14 +137,27 @@ public async Task<IReadOnlyList<TextContent>> GenerateTextAsync(
string modelId = executionSettings?.ModelId ?? this.ModelId;
var endpoint = this.GetTextGenerationEndpoint(modelId);
var request = this.CreateTextRequest(prompt, executionSettings);

using var httpRequestMessage = this.CreatePost(request, endpoint, this.ApiKey);
using var activity = ModelDiagnostics.StartCompletionActivity(endpoint, modelId, "HuggingFace", prompt, executionSettings);

string body = await this.SendRequestAndGetStringBodyAsync(httpRequestMessage, cancellationToken)
.ConfigureAwait(false);
TextGenerationResponse response;
try
{
string body = await this.SendRequestAndGetStringBodyAsync(httpRequestMessage, cancellationToken)
.ConfigureAwait(false);

response = DeserializeResponse<TextGenerationResponse>(body);
}
catch (Exception ex)
{
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}

var response = DeserializeResponse<TextGenerationResponse>(body);
var textContents = GetTextContentsFromResponse(response, modelId);

activity?.SetCompletionResponse(textContents);
this.LogTextGenerationUsage(executionSettings);

return textContents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -106,14 +107,27 @@ internal async Task<IReadOnlyList<ChatMessageContent>> CompleteChatMessageAsync(
string modelId = executionSettings?.ModelId ?? this._clientCore.ModelId;
var endpoint = this.GetChatGenerationEndpoint();
var request = this.CreateChatRequest(chatHistory, executionSettings);

using var httpRequestMessage = this._clientCore.CreatePost(request, endpoint, this._clientCore.ApiKey);
using var activity = ModelDiagnostics.StartCompletionActivity(endpoint, modelId, "HuggingFace", chatHistory, executionSettings);

string body = await this._clientCore.SendRequestAndGetStringBodyAsync(httpRequestMessage, cancellationToken)
.ConfigureAwait(false);
ChatCompletionResponse response;
try
{
string body = await this._clientCore.SendRequestAndGetStringBodyAsync(httpRequestMessage, cancellationToken)
.ConfigureAwait(false);

response = HuggingFaceClient.DeserializeResponse<ChatCompletionResponse>(body);
}
catch (Exception ex)
{
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}

var response = HuggingFaceClient.DeserializeResponse<ChatCompletionResponse>(body);
var chatContents = GetChatMessageContentsFromResponse(response, modelId);

activity?.SetCompletionResponse(chatContents, response.Usage?.PromptTokens, response.Usage?.CompletionTokens);
this.LogChatCompletionUsage(executionSettings, response);

return chatContents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public HuggingFaceTextGenerationService(
Verify.NotNullOrWhiteSpace(model);

this.Client = new HuggingFaceClient(
modelId: model,
modelId: model,
endpoint: endpoint ?? httpClient?.BaseAddress,
apiKey: apiKey,
httpClient: HttpClientProvider.GetHttpClient(httpClient),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ internal AzureOpenAIClientCore(
var options = GetOpenAIClientOptions(httpClient);

this.DeploymentOrModelName = deploymentName;
this.Client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(apiKey), options);
this.Endpoint = new Uri(endpoint);
this.Client = new OpenAIClient(this.Endpoint, new AzureKeyCredential(apiKey), options);
}

/// <summary>
Expand All @@ -73,7 +74,8 @@ internal AzureOpenAIClientCore(
var options = GetOpenAIClientOptions(httpClient);

this.DeploymentOrModelName = deploymentName;
this.Client = new OpenAIClient(new Uri(endpoint), credential, options);
this.Endpoint = new Uri(endpoint);
this.Client = new OpenAIClient(this.Endpoint, credential, options);
}

/// <summary>
Expand Down
52 changes: 43 additions & 9 deletions dotnet/src/Connectors/Connectors.OpenAI/AzureSdk/ClientCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ internal ClientCore(ILogger? logger = null)
/// </summary>
internal abstract OpenAIClient Client { get; }

internal Uri? Endpoint { get; set; } = null;

/// <summary>
/// Logger instance
/// </summary>
Expand Down Expand Up @@ -132,15 +134,31 @@ internal async Task<IReadOnlyList<TextContent>> GetTextResultsAsync(

var options = CreateCompletionsOptions(text, textExecutionSettings, this.DeploymentOrModelName);

var responseData = (await RunRequestAsync(() => this.Client.GetCompletionsAsync(options, cancellationToken)).ConfigureAwait(false)).Value;
if (responseData.Choices.Count == 0)
Completions responseData;
IEnumerable<TextContent> responseContent;
using (var activity = ModelDiagnostics.StartCompletionActivity(this.Endpoint, this.DeploymentOrModelName, "OpenAI", text, executionSettings))
{
throw new KernelException("Text completions not found");
try
{
responseData = (await RunRequestAsync(() => this.Client.GetCompletionsAsync(options, cancellationToken)).ConfigureAwait(false)).Value;
if (responseData.Choices.Count == 0)
{
throw new KernelException("Text completions not found");
}
}
catch (Exception ex)
{
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}

responseContent = responseData.Choices.Select(choice => new TextContent(choice.Text, this.DeploymentOrModelName, choice, Encoding.UTF8, GetTextChoiceMetadata(responseData, choice)));
activity?.SetCompletionResponse(responseContent, responseData.Usage.PromptTokens, responseData.Usage.CompletionTokens);
}

this.CaptureUsageDetails(responseData.Usage);

return responseData.Choices.Select(choice => new TextContent(choice.Text, this.DeploymentOrModelName, choice, Encoding.UTF8, GetTextChoiceMetadata(responseData, choice))).ToList();
return responseContent.ToList();
}

internal async IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(
Expand Down Expand Up @@ -323,18 +341,34 @@ internal async Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsy
for (int requestIndex = 1; ; requestIndex++)
{
// Make the request.
var responseData = (await RunRequestAsync(() => this.Client.GetChatCompletionsAsync(chatOptions, cancellationToken)).ConfigureAwait(false)).Value;
this.CaptureUsageDetails(responseData.Usage);
if (responseData.Choices.Count == 0)
ChatCompletions responseData;
IEnumerable<OpenAIChatMessageContent> responseContent;
using (var activity = ModelDiagnostics.StartCompletionActivity(this.Endpoint, this.DeploymentOrModelName, "OpenAI", chat, executionSettings))
{
throw new KernelException("Chat completions not found");
try
{
responseData = (await RunRequestAsync(() => this.Client.GetChatCompletionsAsync(chatOptions, cancellationToken)).ConfigureAwait(false)).Value;
this.CaptureUsageDetails(responseData.Usage);
if (responseData.Choices.Count == 0)
{
throw new KernelException("Chat completions not found");
}
}
catch (Exception ex)
{
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
throw;
}

responseContent = responseData.Choices.Select(chatChoice => this.GetChatMessage(chatChoice, responseData));
activity?.SetCompletionResponse(responseContent, responseData.Usage.PromptTokens, responseData.Usage.CompletionTokens);
}

// If we don't want to attempt to invoke any functions, just return the result.
// Or if we are auto-invoking but we somehow end up with other than 1 choice even though only 1 was requested, similarly bail.
if (!autoInvoke || responseData.Choices.Count != 1)
{
return responseData.Choices.Select(chatChoice => this.GetChatMessage(chatChoice, responseData)).ToList();
return responseContent.ToList();
}

Debug.Assert(kernel is not null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ internal OpenAIClientCore(
if (providedEndpoint is null)
{
Verify.NotNullOrWhiteSpace(apiKey); // For Public OpenAI Endpoint a key must be provided.
this.Endpoint = new Uri("https://api.openai.com/v1");
}
else
{
options.AddPolicy(new CustomHostPipelinePolicy(providedEndpoint), Azure.Core.HttpPipelinePosition.PerRetry);
this.Endpoint = providedEndpoint;
}

this.Client = new OpenAIClient(apiKey ?? string.Empty, options);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Collections.Generic;
using System.Diagnostics;

namespace Microsoft.SemanticKernel.Diagnostics;

internal static class ActivityExtensions
{
public static Activity? StartActivityWithTags(this ActivitySource source, string name, List<KeyValuePair<string, object?>> tags)
{
return source.StartActivity(
name,
ActivityKind.Internal,
Activity.Current?.Context ?? new ActivityContext(),
tags);
}

public static Activity AddTags(this Activity activity, List<KeyValuePair<string, object?>> tags)
{
tags.ForEach(tag =>
{
activity.SetTag(tag.Key, tag.Value);
});

return activity;
}

public static Activity AttachSensitiveDataAsEvent(this Activity activity, string name, List<KeyValuePair<string, object?>> tags)
{
activity.AddEvent(new ActivityEvent(
name,
tags: new ActivityTagsCollection(tags)
));

return activity;
}
}
Loading