mirror of https://github.com/microsoft/autogen.git
only add the last message to chat history in SendAsync (#3272)
This commit is contained in:
parent
56007d1164
commit
3186bb7610
|
@ -51,7 +51,10 @@ public static class GroupChatExtension
|
||||||
yield break;
|
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
|
||||||
|
chatHistory = chatHistory.Append(lastMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,10 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace AutoGen.Tests;
|
namespace AutoGen.Tests;
|
||||||
|
@ -51,4 +53,36 @@ public class GroupChatTests
|
||||||
chatHistory.Count().Should().Be(3);
|
chatHistory.Count().Should().Be(3);
|
||||||
chatHistory.Last().From.Should().Be("Cathy");
|
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…
Reference in New Issue