Skip to content

Commit ab0a70e

Browse files
.Net: Add Usage Metadata for ChatClientChatCompletionService Adapter (#12412)
### Motivation and Context As part of Agents code-base is commong practice now to acquire `Usage` from the ChatMessageContent.Metadata, although is different how this is done with the new `Microsoft.Extensions.AI` relying on the `UsageDetails` or `UsageContent` to avoid any disruption when migrating `Agent`s to use the `IChatClient`. This change was made, so old code can still function. Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
1 parent d2120d4 commit ab0a70e

File tree

5 files changed

+473
-3
lines changed

5 files changed

+473
-3
lines changed

dotnet/src/InternalUtilities/samples/AgentUtilities/BaseAgentsTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
using Microsoft.SemanticKernel.ChatCompletion;
1010
using OpenAI.Assistants;
1111
using OpenAI.Files;
12-
1312
using ChatTokenUsage = OpenAI.Chat.ChatTokenUsage;
13+
using UsageDetails = Microsoft.Extensions.AI.UsageDetails;
1414

1515
/// <summary>
1616
/// Base class for samples that demonstrate the usage of host agents
@@ -125,6 +125,10 @@ protected void WriteAgentChatMessage(ChatMessageContent message)
125125
{
126126
WriteUsage(chatUsage.TotalTokenCount, chatUsage.InputTokenCount, chatUsage.OutputTokenCount);
127127
}
128+
else if (usage is UsageDetails usageDetails)
129+
{
130+
WriteUsage(usageDetails.TotalTokenCount ?? 0, usageDetails.InputTokenCount ?? 0, usageDetails.OutputTokenCount ?? 0);
131+
}
128132
}
129133

130134
string FormatAuthor() => message.AuthorName is not null ? $" - {message.AuthorName ?? " * "}" : string.Empty;

dotnet/src/SemanticKernel.Abstractions/AI/ChatClient/ChatMessageExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static ChatMessageContent ToChatMessageContent(this ChatMessage message
1515
ModelId = response?.ModelId,
1616
AuthorName = message.AuthorName,
1717
InnerContent = response?.RawRepresentation ?? message.RawRepresentation,
18-
Metadata = message.AdditionalProperties,
18+
Metadata = new AdditionalPropertiesDictionary(message.AdditionalProperties ?? []) { ["Usage"] = response?.Usage },
1919
Role = new AuthorRole(message.Role.Value),
2020
};
2121

dotnet/src/SemanticKernel.Abstractions/AI/ChatClient/ChatResponseUpdateExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using System.Collections.Generic;
34
using System.Text.Json;
45
using Microsoft.SemanticKernel;
56
using Microsoft.SemanticKernel.ChatCompletion;
@@ -34,9 +35,18 @@ item is Microsoft.Extensions.AI.FunctionCallContent fcc ?
3435

3536
if (resultContent is not null)
3637
{
38+
resultContent.InnerContent = item.RawRepresentation;
3739
resultContent.ModelId = update.ModelId;
3840
content.Items.Add(resultContent);
3941
}
42+
43+
if (item is Microsoft.Extensions.AI.UsageContent uc)
44+
{
45+
content.Metadata = new Dictionary<string, object?>(update.AdditionalProperties ?? [])
46+
{
47+
["Usage"] = uc
48+
};
49+
}
4050
}
4151

4252
return content;

0 commit comments

Comments
 (0)