Skip to content

Commit

Permalink
Use ToChatCompletion in OpenTelemetryChatClient (#5614)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Nov 11, 2024
1 parent 3f1f59c commit c163960
Showing 1 changed file with 1 addition and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,62 +201,12 @@ public override async IAsyncEnumerable<StreamingChatCompletionUpdate> CompleteSt
}
finally
{
TraceCompletion(activity, requestModelId, ComposeStreamingUpdatesIntoChatCompletion(trackedUpdates), error, stopwatch);
TraceCompletion(activity, requestModelId, trackedUpdates.ToChatCompletion(), error, stopwatch);

await responseEnumerator.DisposeAsync();
}
}

/// <summary>Creates a <see cref="ChatCompletion"/> from a collection of <see cref="StreamingChatCompletionUpdate"/> instances.</summary>
/// <remarks>
/// This only propagates information that's later used by the telemetry. If additional information from the <see cref="ChatCompletion"/>
/// is needed, this implementation should be updated to include it.
/// </remarks>
private static ChatCompletion ComposeStreamingUpdatesIntoChatCompletion(
List<StreamingChatCompletionUpdate> updates)
{
// Group updates by choice index.
Dictionary<int, List<StreamingChatCompletionUpdate>> choices = [];
foreach (var update in updates)
{
if (!choices.TryGetValue(update.ChoiceIndex, out var choiceContents))
{
choices[update.ChoiceIndex] = choiceContents = [];
}

choiceContents.Add(update);
}

// Add a ChatMessage for each choice.
string? id = null;
ChatFinishReason? finishReason = null;
string? modelId = null;
List<ChatMessage> messages = new(choices.Count);
foreach (var choice in choices.OrderBy(c => c.Key))
{
ChatRole? role = null;
List<AIContent> items = [];
foreach (var update in choice.Value)
{
id ??= update.CompletionId;
finishReason ??= update.FinishReason;
role ??= update.Role;
items.AddRange(update.Contents);
modelId ??= update.ModelId;
}

messages.Add(new ChatMessage(role ?? ChatRole.Assistant, items));
}

return new(messages)
{
CompletionId = id,
FinishReason = finishReason,
ModelId = modelId,
Usage = updates.SelectMany(c => c.Contents).OfType<UsageContent>().LastOrDefault()?.Details,
};
}

/// <summary>Creates an activity for a chat completion request, or returns null if not enabled.</summary>
private Activity? CreateAndConfigureActivity(ChatOptions? options)
{
Expand Down

0 comments on commit c163960

Please sign in to comment.