Skip to content

Commit 6daaf7e

Browse files
committed
feat: include usage information in chat response updates
1 parent de86a08 commit 6daaf7e

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/GenerativeAI.Microsoft/Extensions/MicrosoftExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,18 @@ public static ChatResponseUpdate ToChatResponseUpdate(this GenerateContentRespon
405405

406406
if (response.Candidates != null)
407407
{
408+
var contents = response.Candidates.Select(s => s.Content).SelectMany(s => s?.Parts ?? new List<Part>()).ToList().ToAiContents(options);
409+
410+
// Add usage information as UsageContent so it gets aggregated by the framework
411+
var usage = ParseContentResponseUsage(response);
412+
if (usage != null)
413+
{
414+
contents.Add(new UsageContent(usage));
415+
}
416+
408417
return new ChatResponseUpdate
409418
{
410-
Contents = response.Candidates.Select(s => s.Content).SelectMany(s => s?.Parts ?? new List<Part>()).ToList().ToAiContents(options),
419+
Contents = contents,
411420
AdditionalProperties = null,
412421
FinishReason = response?.Candidates?.FirstOrDefault()?.FinishReason == FinishReason.OTHER
413422
? ChatFinishReason.Stop

0 commit comments

Comments
 (0)