autogen icon indicating copy to clipboard operation
autogen copied to clipboard

[.Net][Issue]: How to communicate with multi-agent through API

Open xuzeyu91 opened this issue 9 months ago • 3 comments

Describe the issue

Viewing demos using console output How should I obtain messages from multiple agents when using GroupAgent? Is there an example, as well as the input to the console? If I want to use the interface for interaction, how can I implement it

Steps to reproduce

       var userProxyAgent = new UserProxyAgent(
             name: "user",
             humanInputMode: HumanInputMode.ALWAYS)
           .RegisterPrintMessage();

Does RegisterPrintMessage have methods such as events to retrieve messages

Screenshots and logs

No response

Additional Information

No response

xuzeyu91 avatar May 08 '24 15:05 xuzeyu91

Yes, RegisterPrintMessage does all the trick for getting reply from an agent and print it to console. It also streaming the output if the agent is an IStreamingAgent.

The code for RegisterPrintMessage is here

Also, can you put a title in the issue, thanks!

LittleLittleCloud avatar May 08 '24 18:05 LittleLittleCloud

Yes, RegisterPrintMessage does all the trick for getting reply from an agent and print it to console. It also streaming the output if the agent is an IStreamingAgent.

The code for RegisterPrintMessage is here

Also, can you put a title in the issue, thanks!

This is not what I want. After testing the console, I hope to develop an API to interact with the user UI. How should I correctly receive messages and pass them to the UI layer, and how should users send messages to the agent through the UI? I am currently using RegisterMiddleware to receive messages and display them to the UI, but I am not sure how users should send messages in the UserProxyAgent. I have a simple implementation now, but I don't think it's a good method

https://github.com/xuzeyu91/EasyAgent/blob/main/src/EasyAgent/Pages/Chat/Chat.razor.cs

This is my code. I am currently waiting in the RegisterMiddleware of the UserProxyAgent until the user enters text before continuing. This is not very good, and I hope there is a better way to handle it

xuzeyu91 avatar May 09 '24 14:05 xuzeyu91

@xuzeyu91 Thanks for your reply.

In your OnSendAsync function, you don't return the result until the group chat is end. So you need to collect user input in the middle of a group chat and that's where the waiting in the RegisterMiddleware comes from. My comment on this is it's not too bad to collect user input in your code's manner because that's also what will happen in UserProxyAgent when it's waiting for input from console. Even if you wrap the blocking function in a nicer way, under the hood the intrinsic is still the same.

My suggestion here though is to update your OnSendAsync to exit group chat either when it's terminated, or user proxy agent is selected as next agent to speak. And resume the conversation the next time the OnSendAsync is called. In that situation you no longer need to wait for input from user in the middle of group chat conversation.

Here is the pesude code.

// in OnSendAsync function
ChatMessage[] chatHistory;
GroupChat groupChat;
// in userProxyAgent, set input mode to never and a default reply message so it always return "wait for user input" when it's selected.
IAgent userProxyAgent = new UserProxyAgent(humanInputMode: HumanInputMode.NEVER, defaultReply: "wait for user input")
string userInputContent; // suppose this is the latest user message
while (true)
{
    // add latest user input to chatHistory
   chatHistory.Add(userInputContent);
   // continue group chat
   // set maxTurn to 1 so it stop after one round
   var newMessages = await groupChat.SendAsync(chatHistory: chatHistory, maxTurn: 1);
   // check if the newMessages[0] comes from user
   if (newMessages[0].From == "user")
   {
      // exit to wait for user input
     return;
   }
   else
   {
      // carry on the conversation by add new messages to history and continue
      chatHistory.AddRange(newMessages);
   }
}

LittleLittleCloud avatar May 09 '24 18:05 LittleLittleCloud

thank you

xuzeyu91 avatar May 23 '24 16:05 xuzeyu91