From 395b64e3b6dd52552e4b2a080b717c743e72d67f Mon Sep 17 00:00:00 2001 From: XiaoYun Zhang Date: Thu, 11 Jul 2024 15:09:50 -0700 Subject: [PATCH] update autogen to 0.0.16 --- .../ChatRoom.Common/ChatPlatformClient.cs | 13 ++++ .../Controller/ChatRoomClientController.cs | 4 +- .../Orleans/AutoGenOrchestratorObserver.cs | 33 +++++++++ Directory.Packages.props | 10 +-- template/chatroom/chatroom_all_in_one.json | 69 +++++++++---------- .../ChatRoomClientCommandTests.cs | 32 +++++++++ test/ChatRoom.Tests/ChannelGrainTests.cs | 2 + 7 files changed, 119 insertions(+), 44 deletions(-) create mode 100644 ChatRoom/ChatRoom.Common/Orleans/AutoGenOrchestratorObserver.cs diff --git a/ChatRoom/ChatRoom.Common/ChatPlatformClient.cs b/ChatRoom/ChatRoom.Common/ChatPlatformClient.cs index ec6dcca..d745c80 100644 --- a/ChatRoom/ChatRoom.Common/ChatPlatformClient.cs +++ b/ChatRoom/ChatRoom.Common/ChatPlatformClient.cs @@ -3,6 +3,7 @@ using Orleans; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using ChatRoom.SDK.Orleans; namespace ChatRoom.SDK; @@ -56,6 +57,12 @@ public async Task RegisterAgentAsync(string name, string description, bool isHum } } + public async Task RegisterAutoGenOrchestratorAsync(string name, AutoGen.Core.IOrchestrator orchestrator) + { + var observer = new AutoGenOrchestratorObserver(orchestrator); + await this.RegisterOrchestratorAsync(name, observer); + } + public async Task RegisterOrchestratorAsync(string name, IOrchestrator orchestrator) { var observer = new OrchestratorObserver(orchestrator); @@ -122,6 +129,12 @@ public async Task ClearChannelHistory(string channelName) await channel.ClearHistory(); } + public async Task SendMessageToChannel(string channelName, ChatMsg msg) + { + var channel = _client.GetGrain(channelName); + await channel.SendMessage(msg); + } + public async Task GenerateNextReply(string channelName, string[]? candidates = null, ChatMsg[]? chatMsgs = null, string? orchestrator = null) { var room = _client.GetGrain(_room); diff --git a/ChatRoom/ChatRoom.Common/Controller/ChatRoomClientController.cs b/ChatRoom/ChatRoom.Common/Controller/ChatRoomClientController.cs index b2051f5..1b027ae 100644 --- a/ChatRoom/ChatRoom.Common/Controller/ChatRoomClientController.cs +++ b/ChatRoom/ChatRoom.Common/Controller/ChatRoomClientController.cs @@ -75,9 +75,7 @@ public async Task SendTextMessageToChannel( return new BadRequestObjectResult("You are not authorized to send message to this channel"); } - var channelGrain = _clusterClient.GetGrain(channel); - - await channelGrain.SendMessage(message); + await _chatPlatformClient.SendMessageToChannel(channel, message); return new OkResult(); } diff --git a/ChatRoom/ChatRoom.Common/Orleans/AutoGenOrchestratorObserver.cs b/ChatRoom/ChatRoom.Common/Orleans/AutoGenOrchestratorObserver.cs new file mode 100644 index 0000000..497d6a7 --- /dev/null +++ b/ChatRoom/ChatRoom.Common/Orleans/AutoGenOrchestratorObserver.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AutoGen.Core; + +namespace ChatRoom.SDK.Orleans; + +internal class AutoGenOrchestratorObserver : IOrchestratorObserver +{ + private readonly AutoGen.Core.IOrchestrator _orchestrator; + public AutoGenOrchestratorObserver(AutoGen.Core.IOrchestrator orchestrator) + { + _orchestrator = orchestrator; + } + + public async Task GetNextSpeaker(AgentInfo[] members, ChatMsg[] messages) + { + var autogenMessages = messages.Select(msg => msg.ToAutoGenMessage()).ToArray(); + var dummyAgents = members.Select(x => new DummyAgent(x)).ToArray(); + + var context = new OrchestrationContext() + { + Candidates = dummyAgents, + ChatHistory = autogenMessages, + }; + + var nextAgent = await _orchestrator.GetNextSpeakerAsync(context); + + return nextAgent?.Name; + } +} diff --git a/Directory.Packages.props b/Directory.Packages.props index 45e6175..58078f2 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -17,11 +17,11 @@ - - - - - + + + + + diff --git a/template/chatroom/chatroom_all_in_one.json b/template/chatroom/chatroom_all_in_one.json index db0087d..bf4845f 100644 --- a/template/chatroom/chatroom_all_in_one.json +++ b/template/chatroom/chatroom_all_in_one.json @@ -2,32 +2,34 @@ "$schema": "chatroom_configuration_schema.json", "channel_config": { "openai_config": { - "llm_type": "OpenAI", - "openai_api_key": "", - "openai_model_id": "gpt-3.5-turbo" + "llm_type": "AOAI", + "azure_openai_deploy_name": "gpt-4o", + "azure_openai_endpoint": "https://xiaoyun-openai-canada-east.openai.azure.com/", + "azure_openai_key": "53547b13b52046709973aac63084e390" } }, "server_config": { "urls": "http://localhost:51234;https://localhost:51235" }, "chatroom_github_configuration": { - "github_repo_name": "", - "github_repo_owner": "", - "github_token": "", + "github_repo_name": "Agent-Chatroom", + "github_repo_owner": "LittleLittleCloud", "issue_helper": { "openai_config": { - "llm_type": "OpenAI", - "openai_api_key": "", - "openai_model_id": "gpt-3.5-turbo" + "llm_type": "AOAI", + "azure_openai_deploy_name": "gpt-4o", + "azure_openai_endpoint": "https://xiaoyun-openai-canada-east.openai.azure.com/", + "azure_openai_key": "53547b13b52046709973aac63084e390" } } }, "chatroom_planner_configuration": { "react_planner_configuration": { "openai_config": { - "llm_type": "OpenAI", - "openai_api_key": "", - "openai_model_id": "gpt-4o" + "llm_type": "AOAI", + "azure_openai_deploy_name": "gpt-4o", + "azure_openai_endpoint": "https://xiaoyun-openai-canada-east.openai.azure.com/", + "azure_openai_key": "53547b13b52046709973aac63084e390" } } }, @@ -43,20 +45,13 @@ } }, { - "name": "gpt4", + "name": "gpt4o", "system_message": "You are a helpful AI assistant", "llm_config": { - "llm_type": "OpenAI", - "openai_api_key": "", - "openai_model_id": "gpt-4-turbo" - } - }, - { - "name": "llama3", - "llm_config": { - "llm_type": "ThirdParty", - "third_party_llm_endpoint": "http://localhost:51234", - "third_party_llm_model_id": "llama3:latest" + "llm_type": "AOAI", + "azure_openai_deploy_name": "gpt-4o", + "azure_openai_endpoint": "https://xiaoyun-openai-canada-east.openai.azure.com/", + "azure_openai_key": "53547b13b52046709973aac63084e390" } } ] @@ -65,9 +60,10 @@ "gpt": { "CurrentWorkingDirectory": ".", "openai_config": { - "llm_type": "OpenAI", - "openai_api_key": "", - "openai_model_id": "gpt-3.5-turbo" + "llm_type": "AOAI", + "azure_openai_deploy_name": "gpt-4o", + "azure_openai_endpoint": "https://xiaoyun-openai-canada-east.openai.azure.com/", + "azure_openai_key": "53547b13b52046709973aac63084e390" } }, "runner": { @@ -76,20 +72,21 @@ }, "chatroom_websearch_configuration": { "bing_search_config": { - "bing_api_key": "", "openai_config": { - "llm_type": "OpenAI", - "openai_api_key": "", - "openai_model_id": "gpt-3.5-turbo" + "llm_type": "AOAI", + "azure_openai_deploy_name": "gpt-4o", + "azure_openai_endpoint": "https://xiaoyun-openai-canada-east.openai.azure.com/", + "azure_openai_key": "53547b13b52046709973aac63084e390" } }, "google_search_config": { - "google_api_key": "", - "google_custom_search_engine_id": "", + "google_api_key": "AIzaSyBh1y6OGoUv4AWM-Rzx6Jpe2BPNDbW-T28", + "google_custom_search_engine_id": "862e329b3cc8246d5", "openai_config": { - "llm_type": "OpenAI", - "openai_api_key": "", - "openai_model_id": "gpt-3.5-turbo" + "llm_type": "AOAI", + "azure_openai_deploy_name": "gpt-4o", + "azure_openai_endpoint": "https://xiaoyun-openai-canada-east.openai.azure.com/", + "azure_openai_key": "53547b13b52046709973aac63084e390" } } } diff --git a/test/ChatRoom.Client.Tests/ChatRoomClientCommandTests.cs b/test/ChatRoom.Client.Tests/ChatRoomClientCommandTests.cs index 04d3322..59b7b1a 100644 --- a/test/ChatRoom.Client.Tests/ChatRoomClientCommandTests.cs +++ b/test/ChatRoom.Client.Tests/ChatRoomClientCommandTests.cs @@ -16,6 +16,8 @@ using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.Hosting; using Newtonsoft.Json.Linq; +using AutoGen.Core; +using Moq; namespace ChatRoom.Client.Tests; @@ -97,4 +99,34 @@ public async Task ItCreateAndRemoveChannelTestAsync() channels = await _client.GetChannels(); channels.Count().Should().Be(0); } + + + [Fact] + public async Task ItUseAutoGenOrchstratorTest() + { + var orchestrator = new RoundRobinOrchestrator(); + await _client.RegisterAutoGenOrchestratorAsync("autogen-round-robin", orchestrator); + + var availableOrchestrators = await _client.GetOrchestrators(); + availableOrchestrators.Should().Contain("autogen-round-robin"); + + // create a channel with User, gpt3.5, and autogen-round-robin orchestrator + await _client.CreateChannel("autogen-channel", ["User", "gpt3.5"], orchestrators: ["autogen-round-robin"]); + + // send a dummy user message to the channel + var userMessage = new ChatMsg("User", "Hello, I am a user."); + await _client.SendMessageToChannel("autogen-channel", userMessage); + + // generate a reply + var reply = await _client.GenerateNextReply("autogen-channel", chatMsgs: [userMessage], candidates: ["User", "gpt3.5"], orchestrator: "autogen-round-robin"); + + reply!.From.Should().Be("gpt3.5"); + + // clean up + await _client.DeleteChannel("autogen-channel"); + await _client.UnregisterOrchestratorAsync("autogen-round-robin"); + + availableOrchestrators = await _client.GetOrchestrators(); + availableOrchestrators.Should().NotContain("autogen-round-robin"); + } } diff --git a/test/ChatRoom.Tests/ChannelGrainTests.cs b/test/ChatRoom.Tests/ChannelGrainTests.cs index eed32a5..73894ce 100644 --- a/test/ChatRoom.Tests/ChannelGrainTests.cs +++ b/test/ChatRoom.Tests/ChannelGrainTests.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using AutoGen.Core; using ChatRoom.SDK; using FluentAssertions; using Moq; @@ -69,4 +70,5 @@ public async Task ItUseOrchstratorTest() var chatHistory = await channel.ReadHistory(1000); chatHistory.Count().Should().Be(2); } + }