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 13 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 @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Http;
using Microsoft.SemanticKernel.Text;

Expand All @@ -21,6 +22,7 @@ namespace Microsoft.SemanticKernel.Connectors.Google.Core;
/// </summary>
internal sealed class GeminiChatCompletionClient : ClientBase
{
private const string ModelProvider = "google";
private readonly StreamJsonParser _streamJsonParser = new();
private readonly string _modelId;
private readonly Uri _chatGenerationEndpoint;
Expand Down Expand Up @@ -161,11 +163,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, ModelProvider, chatHistory, executionSettings))
{
try
{
geminiResponse = await this.SendRequestAndReturnValidGeminiResponseAsync(
this._chatGenerationEndpoint, state.GeminiRequest, cancellationToken)
.ConfigureAwait(false);
chatResponses = this.ProcessChatResponse(geminiResponse);
}
catch (Exception ex)
{
activity?.SetError(ex);
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 @@ -12,6 +12,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Http;
using Microsoft.SemanticKernel.Text;

Expand All @@ -21,6 +22,7 @@ internal sealed class HuggingFaceClient
{
private readonly HttpClient _httpClient;

internal string ModelProvider => "huggingface";
internal string ModelId { get; }
internal string? ApiKey { get; }
internal Uri Endpoint { get; }
Expand Down Expand Up @@ -136,14 +138,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 activity = ModelDiagnostics.StartCompletionActivity(endpoint, modelId, this.ModelProvider, prompt, executionSettings);
using var httpRequestMessage = this.CreatePost(request, endpoint, this.ApiKey);

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?.SetError(ex);
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 @@ -12,6 +12,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Http;
using Microsoft.SemanticKernel.Text;

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 activity = ModelDiagnostics.StartCompletionActivity(endpoint, modelId, this._clientCore.ModelProvider, chatHistory, executionSettings);
using var httpRequestMessage = this._clientCore.CreatePost(request, endpoint, this._clientCore.ApiKey);

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?.SetError(ex);
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
70 changes: 61 additions & 9 deletions dotnet/src/Connectors/Connectors.OpenAI/AzureSdk/ClientCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Http;

#pragma warning disable CA2208 // Instantiate argument exceptions correctly
Expand All @@ -29,6 +30,7 @@ namespace Microsoft.SemanticKernel.Connectors.OpenAI;
/// </summary>
internal abstract class ClientCore
{
private const string ModelProvider = "openai";
private const int MaxResultsPerPrompt = 128;

/// <summary>
Expand Down Expand Up @@ -70,6 +72,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 +136,39 @@ 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 = null;
List<TextContent> responseContent;
using (var activity = ModelDiagnostics.StartCompletionActivity(this.Endpoint, this.DeploymentOrModelName, ModelProvider, 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?.SetError(ex);
if (responseData != null)
{
// Capture available metadata even if the operation failed.
activity?
.SetResponseId(responseData.Id)
.SetPromptTokenUsage(responseData.Usage.PromptTokens)
.SetCompletionTokenUsage(responseData.Usage.CompletionTokens);
}
throw;
}

responseContent = responseData.Choices.Select(choice => new TextContent(choice.Text, this.DeploymentOrModelName, choice, Encoding.UTF8, GetTextChoiceMetadata(responseData, choice))).ToList();
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;
}

internal async IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(
Expand Down Expand Up @@ -323,18 +351,42 @@ 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 = null;
List<OpenAIChatMessageContent> responseContent;
using (var activity = ModelDiagnostics.StartCompletionActivity(this.Endpoint, this.DeploymentOrModelName, ModelProvider, 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?.SetError(ex);
if (responseData != null)
{
// Capture available metadata even if the operation failed.
activity?
.SetResponseId(responseData.Id)
.SetPromptTokenUsage(responseData.Usage.PromptTokens)
.SetCompletionTokenUsage(responseData.Usage.CompletionTokens);
}
throw;
}

responseContent = responseData.Choices.Select(chatChoice => this.GetChatMessage(chatChoice, responseData)).ToList();
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;
}

Debug.Assert(kernel is not null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Microsoft.SemanticKernel.Connectors.OpenAI;
/// </summary>
internal sealed class OpenAIClientCore : ClientCore
{
private const string DefaultPublicEndpoint = "https://api.openai.com/v1";

/// <summary>
/// Gets the attribute name used to store the organization in the <see cref="IAIService.Attributes"/> dictionary.
/// </summary>
Expand Down Expand Up @@ -59,11 +61,14 @@ internal OpenAIClientCore(
if (providedEndpoint is null)
{
Verify.NotNullOrWhiteSpace(apiKey); // For Public OpenAI Endpoint a key must be provided.
this.Endpoint = new Uri(DefaultPublicEndpoint);
}
else
{
options.AddPolicy(new CustomHostPipelinePolicy(providedEndpoint), Azure.Core.HttpPipelinePosition.PerRetry);
this.Endpoint = providedEndpoint;
}

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

Expand Down
Loading
Loading