Skip to content

Commit d3641c5

Browse files
authored
Merge pull request #206 from jozkee/feature/fix-tools-with-streaming
Account for ToolCalls on GetStreamingResponseAsync
2 parents 477baa9 + 6518e26 commit d3641c5

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/OllamaSharp/MicrosoftAi/AbstractionMapper.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,11 @@ private static Microsoft.Extensions.AI.ChatRole ToAbstractionRole(ChatRole? role
309309
/// <returns>A <see cref="ChatResponseUpdate"/> object containing the latest chat completion chunk.</returns>
310310
public static ChatResponseUpdate ToChatResponseUpdate(ChatResponseStream? response, string responseId)
311311
{
312-
// TODO: Check if "Message" can ever actually be null. If not, remove the null-coalescing operator
313-
return new(ToAbstractionRole(response?.Message?.Role), response?.Message?.Content ?? string.Empty)
312+
// TODO: Check if "Message" can ever actually be null.
313+
List<AIContent> contents = response?.Message is null ?
314+
[new TextContent(string.Empty)] : GetAIContentsFromMessage(response.Message);
315+
316+
return new ChatResponseUpdate(ToAbstractionRole(response?.Message.Role), contents)
314317
{
315318
// no need to set "Contents" as we set the text
316319
CreatedAt = response?.CreatedAt,
@@ -327,6 +330,11 @@ public static ChatResponseUpdate ToChatResponseUpdate(ChatResponseStream? respon
327330
/// <param name="message">The message to convert.</param>
328331
/// <returns>A <see cref="ChatMessage"/> object containing the converted data.</returns>
329332
public static ChatMessage ToChatMessage(Message message)
333+
{
334+
return new ChatMessage(ToAbstractionRole(message.Role), GetAIContentsFromMessage(message)) { RawRepresentation = message };
335+
}
336+
337+
private static List<AIContent> GetAIContentsFromMessage(Message message)
330338
{
331339
var contents = new List<AIContent>();
332340

@@ -347,7 +355,7 @@ public static ChatMessage ToChatMessage(Message message)
347355
if (message.Content?.Length > 0 || contents.Count == 0)
348356
contents.Insert(0, new TextContent(message.Content));
349357

350-
return new ChatMessage(ToAbstractionRole(message.Role), contents) { RawRepresentation = message };
358+
return contents;
351359
}
352360

353361
/// <summary>

0 commit comments

Comments
 (0)