Skip to content

Commit e96bdb3

Browse files
LittleLittleCloudvictordibia
authored andcommitted
fix #2975 (#3012)
1 parent 337d728 commit e96bdb3

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

dotnet/src/AutoGen.Core/Message/ToolCallMessage.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override string ToString()
3636
}
3737
}
3838

39-
public class ToolCallMessage : IMessage, ICanGetToolCalls
39+
public class ToolCallMessage : IMessage, ICanGetToolCalls, ICanGetTextContent
4040
{
4141
public ToolCallMessage(IEnumerable<ToolCall> toolCalls, string? from = null)
4242
{
@@ -80,6 +80,12 @@ public void Update(ToolCallMessageUpdate update)
8080

8181
public string? From { get; set; }
8282

83+
/// <summary>
84+
/// Some LLMs might also include text content in a tool call response, like GPT.
85+
/// This field is used to store the text content in that case.
86+
/// </summary>
87+
public string? Content { get; set; }
88+
8389
public override string ToString()
8490
{
8591
var sb = new StringBuilder();
@@ -96,6 +102,11 @@ public IEnumerable<ToolCall> GetToolCalls()
96102
{
97103
return this.ToolCalls;
98104
}
105+
106+
public string? GetContent()
107+
{
108+
return this.Content;
109+
}
99110
}
100111

101112
public class ToolCallMessageUpdate : IStreamingMessage

dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs

+16-8
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,13 @@ private IMessage PostProcessChatCompletions(IMessage<ChatCompletions> message)
136136

137137
private IMessage PostProcessChatResponseMessage(ChatResponseMessage chatResponseMessage, string? from)
138138
{
139-
if (chatResponseMessage.Content is string content && !string.IsNullOrEmpty(content))
140-
{
141-
return new TextMessage(Role.Assistant, content, from);
142-
}
143-
139+
var textContent = chatResponseMessage.Content;
144140
if (chatResponseMessage.FunctionCall is FunctionCall functionCall)
145141
{
146-
return new ToolCallMessage(functionCall.Name, functionCall.Arguments, from);
142+
return new ToolCallMessage(functionCall.Name, functionCall.Arguments, from)
143+
{
144+
Content = textContent,
145+
};
147146
}
148147

149148
if (chatResponseMessage.ToolCalls.Where(tc => tc is ChatCompletionsFunctionToolCall).Any())
@@ -154,7 +153,15 @@ private IMessage PostProcessChatResponseMessage(ChatResponseMessage chatResponse
154153

155154
var toolCalls = functionToolCalls.Select(tc => new ToolCall(tc.Name, tc.Arguments) { ToolCallId = tc.Id });
156155

157-
return new ToolCallMessage(toolCalls, from);
156+
return new ToolCallMessage(toolCalls, from)
157+
{
158+
Content = textContent,
159+
};
160+
}
161+
162+
if (textContent is string content && !string.IsNullOrEmpty(content))
163+
{
164+
return new TextMessage(Role.Assistant, content, from);
158165
}
159166

160167
throw new InvalidOperationException("Invalid ChatResponseMessage");
@@ -327,7 +334,8 @@ private IEnumerable<ChatRequestMessage> ProcessToolCallMessage(IAgent agent, Too
327334
}
328335

329336
var toolCall = message.ToolCalls.Select((tc, i) => new ChatCompletionsFunctionToolCall(tc.ToolCallId ?? $"{tc.FunctionName}_{i}", tc.FunctionName, tc.FunctionArguments));
330-
var chatRequestMessage = new ChatRequestAssistantMessage(string.Empty) { Name = message.From };
337+
var textContent = message.GetContent() ?? string.Empty;
338+
var chatRequestMessage = new ChatRequestAssistantMessage(textContent) { Name = message.From };
331339
foreach (var tc in toolCall)
332340
{
333341
chatRequestMessage.ToolCalls.Add(tc);

dotnet/test/AutoGen.OpenAI.Tests/OpenAIMessageTests.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ public async Task ItProcessToolCallMessageAsync()
278278
var innerMessage = msgs.Last();
279279
innerMessage!.Should().BeOfType<MessageEnvelope<ChatRequestMessage>>();
280280
var chatRequestMessage = (ChatRequestAssistantMessage)((MessageEnvelope<ChatRequestMessage>)innerMessage!).Content;
281-
chatRequestMessage.Content.Should().BeNullOrEmpty();
282281
chatRequestMessage.Name.Should().Be("assistant");
283282
chatRequestMessage.ToolCalls.Count().Should().Be(1);
283+
chatRequestMessage.Content.Should().Be("textContent");
284284
chatRequestMessage.ToolCalls.First().Should().BeOfType<ChatCompletionsFunctionToolCall>();
285285
var functionToolCall = (ChatCompletionsFunctionToolCall)chatRequestMessage.ToolCalls.First();
286286
functionToolCall.Name.Should().Be("test");
@@ -291,7 +291,10 @@ public async Task ItProcessToolCallMessageAsync()
291291
.RegisterMiddleware(middleware);
292292

293293
// user message
294-
IMessage message = new ToolCallMessage("test", "test", "assistant");
294+
IMessage message = new ToolCallMessage("test", "test", "assistant")
295+
{
296+
Content = "textContent",
297+
};
295298
await agent.GenerateReplyAsync([message]);
296299
}
297300

@@ -526,13 +529,14 @@ public async Task ItConvertChatResponseMessageToToolCallMessageAsync()
526529
.RegisterMiddleware(middleware);
527530

528531
// tool call message
529-
var toolCallMessage = CreateInstance<ChatResponseMessage>(ChatRole.Assistant, "", new[] { new ChatCompletionsFunctionToolCall("test", "test", "test") }, new FunctionCall("test", "test"), CreateInstance<AzureChatExtensionsMessageContext>(), new Dictionary<string, BinaryData>());
532+
var toolCallMessage = CreateInstance<ChatResponseMessage>(ChatRole.Assistant, "textContent", new[] { new ChatCompletionsFunctionToolCall("test", "test", "test") }, new FunctionCall("test", "test"), CreateInstance<AzureChatExtensionsMessageContext>(), new Dictionary<string, BinaryData>());
530533
var chatRequestMessage = MessageEnvelope.Create(toolCallMessage);
531534
var message = await agent.GenerateReplyAsync([chatRequestMessage]);
532535
message.Should().BeOfType<ToolCallMessage>();
533536
message.GetToolCalls()!.Count().Should().Be(1);
534537
message.GetToolCalls()!.First().FunctionName.Should().Be("test");
535538
message.GetToolCalls()!.First().FunctionArguments.Should().Be("test");
539+
message.GetContent().Should().Be("textContent");
536540
}
537541

538542
[Fact]

0 commit comments

Comments
 (0)