Skip to content

Commit df2ff40

Browse files
authored
Merge pull request #270 from jmickeyd/streaming-usage
Add token usage to last value produced by IChatClient.GetStreamingResponseAsync
2 parents 3c307ad + e5dd037 commit df2ff40

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/OllamaSharp/MicrosoftAi/AbstractionMapper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ private static Microsoft.Extensions.AI.ChatRole ToAbstractionRole(ChatRole? role
321321
/// <returns>A <see cref="ChatResponseUpdate"/> object containing the latest chat completion chunk.</returns>
322322
public static ChatResponseUpdate ToChatResponseUpdate(ChatResponseStream? response, string responseId)
323323
{
324+
if (response is ChatDoneResponseStream done)
325+
{
326+
return new ChatResponseUpdate(ToAbstractionRole(done.Message.Role), [new UsageContent(ParseOllamaChatResponseUsage(done))])
327+
{
328+
CreatedAt = done.CreatedAt,
329+
FinishReason = done.DoneReason is null ? null : new ChatFinishReason(done.DoneReason),
330+
RawRepresentation = response,
331+
ResponseId = responseId,
332+
ModelId = done.Model
333+
};
334+
}
335+
324336
var contents = response?.Message is null ? [new TextContent(string.Empty)] : GetAIContentsFromMessage(response.Message);
325337

326338
return new ChatResponseUpdate(ToAbstractionRole(response?.Message.Role), contents)
@@ -426,6 +438,9 @@ private static AdditionalPropertiesDictionary ParseOllamaEmbedResponseProps(Embe
426438
/// </summary>
427439
/// <param name="response">The response to parse.</param>
428440
/// <returns>A <see cref="UsageDetails"/> object containing the parsed usage details.</returns>
441+
#if NETSTANDARD2_1_OR_GREATER || NET8_0_OR_GREATER
442+
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("response")]
443+
#endif
429444
private static UsageDetails? ParseOllamaChatResponseUsage(ChatDoneResponseStream? response)
430445
{
431446
if (response is not null)

0 commit comments

Comments
 (0)