Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 31 additions & 19 deletions src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
Expand Down Expand Up @@ -157,28 +158,38 @@ void IDisposable.Dispose()
}
else if (input.Role == ChatRole.Assistant)
{
AssistantChatMessage message = new(ToOpenAIChatContent(input.Contents))
var contentParts = ToOpenAIChatContent(input.Contents, ensureNotEmpty: false);

var toolCalls = input.Contents.OfType<FunctionCallContent>().Select(c =>
ChatToolCall.CreateFunctionToolCall(c.CallId, c.Name, new(JsonSerializer.SerializeToUtf8Bytes(
c.Arguments, options.GetTypeInfo(typeof(IDictionary<string, object?>)))))).ToArray();

AssistantChatMessage message;
if (contentParts.Count > 0)
{
message = new(contentParts);
foreach (var toolCall in toolCalls)
{
message.ToolCalls.Add(toolCall);
}
}
else if (toolCalls.Length > 0)
{
ParticipantName = input.AuthorName
};
message = new(toolCalls);
}
else
{
message = new(ChatMessageContentPart.CreateTextPart(string.Empty));
}

message.ParticipantName = input.AuthorName;

foreach (var content in input.Contents)
{
switch (content)
if (content is ErrorContent { ErrorCode: nameof(message.Refusal) } refusal)
{
case ErrorContent errorContent when errorContent.ErrorCode is nameof(message.Refusal):
message.Refusal = errorContent.Message;
break;

case FunctionCallContent callRequest:
message.ToolCalls.Add(
ChatToolCall.CreateFunctionToolCall(
callRequest.CallId,
callRequest.Name,
new(JsonSerializer.SerializeToUtf8Bytes(
callRequest.Arguments,
options.GetTypeInfo(typeof(IDictionary<string, object?>))))));
break;
message.Refusal = refusal.Message;
break;
}
}

Expand All @@ -188,7 +199,8 @@ void IDisposable.Dispose()
}

/// <summary>Converts a list of <see cref="AIContent"/> to a list of <see cref="ChatMessageContentPart"/>.</summary>
private static List<ChatMessageContentPart> ToOpenAIChatContent(IList<AIContent> contents)
private static List<ChatMessageContentPart> ToOpenAIChatContent(
IList<AIContent> contents, bool ensureNotEmpty = true)
{
List<ChatMessageContentPart> parts = [];
foreach (var content in contents)
Expand Down Expand Up @@ -226,7 +238,7 @@ private static List<ChatMessageContentPart> ToOpenAIChatContent(IList<AIContent>
}
}

if (parts.Count == 0)
if (ensureNotEmpty && parts.Count == 0)
{
parts.Add(ChatMessageContentPart.CreateTextPart(string.Empty));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public virtual async Task GetResponseAsync_WithEmptyMessage()

var response = await _chatClient.GetResponseAsync(
[
new(ChatRole.System, []),
new(ChatRole.User, []),
new(ChatRole.Assistant, []),
new(ChatRole.User, "What is 1 + 2? Reply with a single number."),
]);

Expand Down
Loading