Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
32 changes: 17 additions & 15 deletions src/OllamaSharp/MicrosoftAi/AbstractionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@ internal static class AbstractionMapper
/// <returns>A <see cref="ChatResponse"/> object containing the mapped data.</returns>
public static ChatResponse? ToChatResponse(ChatDoneResponseStream? stream, string? usedModel)
{
if (stream is null)
return null;

var chatMessage = ToChatMessage(stream.Message);
chatMessage.CreatedAt = stream.CreatedAt;

return new ChatResponse(chatMessage)
{
if (stream is null)
return null;

var responseId = stream.CreatedAtString ?? Guid.NewGuid().ToString("N");
var chatMessage = ToChatMessage(stream.Message);
chatMessage.CreatedAt = stream.CreatedAt;
chatMessage.MessageId = responseId;

return new ChatResponse(chatMessage)
{
FinishReason = ToFinishReason(stream.DoneReason),
AdditionalProperties = ParseOllamaChatResponseProps(stream),
CreatedAt = stream.CreatedAt,
ModelId = usedModel ?? stream.Model,
RawRepresentation = stream,
ResponseId = stream.CreatedAtString ?? Guid.NewGuid().ToString("N"),
Usage = ParseOllamaChatResponseUsage(stream)
};
}
CreatedAt = stream.CreatedAt,
ModelId = usedModel ?? stream.Model,
RawRepresentation = stream,
ResponseId = responseId,
Usage = ParseOllamaChatResponseUsage(stream)
};
}

/// <summary>
/// Converts Microsoft.Extensions.AI <see cref="ChatMessage"/> objects and
Expand Down
9 changes: 5 additions & 4 deletions test/AbstractionMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,10 @@ public void Maps_Known_Properties()
response.AdditionalProperties[Application.PromptEvalDuration].ShouldBe(TimeSpan.FromSeconds(5.555555555));
response.CreatedAt.ShouldBe(new DateTimeOffset(2023, 08, 04, 08, 52, 19, 385, 406, TimeSpan.FromHours(-7)));
response.FinishReason.ShouldBe(ChatFinishReason.Stop);
response.Messages[0].AuthorName.ShouldBeNull();
response.Messages[0].RawRepresentation.ShouldBe(stream.Message);
response.Messages[0].Role.ShouldBe(Microsoft.Extensions.AI.ChatRole.Assistant);
response.Messages[0].AuthorName.ShouldBeNull();
response.Messages[0].MessageId.ShouldBe(ollamaCreatedStamp);
response.Messages[0].RawRepresentation.ShouldBe(stream.Message);
response.Messages[0].Role.ShouldBe(Microsoft.Extensions.AI.ChatRole.Assistant);
response.Messages[0].Text.ShouldBe("Hi.");
response.Messages[0].Contents.Count.ShouldBe(1);
((TextContent)response.Messages[0].Contents[0]).Text.ShouldBe("Hi.");
Expand Down Expand Up @@ -1111,4 +1112,4 @@ public void Maps_Response()
}

#pragma warning restore CS8602 // Dereference of a possibly null reference.
#pragma warning restore CS8604 // Possible null reference argument.
#pragma warning restore CS8604 // Possible null reference argument.
Loading