Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[.Net] only add the last message to chat history in GroupChatExtension.SendAsync #3272

Merged
merged 1 commit into from
Aug 2, 2024
Merged
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
5 changes: 4 additions & 1 deletion dotnet/src/AutoGen.Core/Extension/GroupChatExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public static async IAsyncEnumerable<IMessage> SendAsync(
yield break;
}

chatHistory = messages;
// messages will contain the complete chat history, include initalize messages
// but we only need to add the last message to the chat history
// fix #3268
LittleLittleCloud marked this conversation as resolved.
Show resolved Hide resolved
chatHistory = chatHistory.Append(lastMessage);
}
}

Expand Down
34 changes: 34 additions & 0 deletions dotnet/test/AutoGen.Tests/GroupChat/GroupChatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using Xunit;

namespace AutoGen.Tests;
Expand Down Expand Up @@ -51,4 +53,36 @@ public async Task ItTerminateConversationWhenAgentReturnTerminateKeyWord()
chatHistory.Count().Should().Be(3);
chatHistory.Last().From.Should().Be("Cathy");
}

[Fact]
public async Task ItSendAsyncDoesntAddDuplicateInitializeMessagesTest()
{
// fix #3268
var alice = new DefaultReplyAgent("Alice", "I am alice");
var bob = new DefaultReplyAgent("Bob", "I am bob");
var cathy = new DefaultReplyAgent("Cathy", $"I am cathy, {GroupChatExtension.TERMINATE}");

var roundRobinOrchestrator = new RoundRobinOrchestrator();
var orchestrator = Mock.Of<IOrchestrator>();
Mock.Get(orchestrator).Setup(x => x.GetNextSpeakerAsync(It.IsAny<OrchestrationContext>(), It.IsAny<CancellationToken>()))
.Returns((OrchestrationContext context, CancellationToken token) =>
{
// determine if initialize message is already sent and not added twice
context.ChatHistory.Where(x => x.From == alice.Name).Count().Should().Be(1);

return roundRobinOrchestrator.GetNextSpeakerAsync(context, token);
});

var groupChat = new GroupChat([alice, bob, cathy], orchestrator);
groupChat.AddInitializeMessage(new TextMessage(Role.User, "Hello", from: alice.Name));

var maxRound = 2;
var chatHistory = new List<IMessage>();
await foreach (var message in groupChat.SendAsync(chatHistory, maxRound))
{
chatHistory.Add(message);
}

chatHistory.Count().Should().Be(2);
}
}
Loading