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][Issue]: How to communicate with multi-agent through API #2622

Open
xuzeyu91 opened this issue May 8, 2024 · 3 comments
Open

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

xuzeyu91 opened this issue May 8, 2024 · 3 comments

Comments

@xuzeyu91
Copy link

xuzeyu91 commented May 8, 2024

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

@LittleLittleCloud
Copy link
Collaborator

LittleLittleCloud commented May 8, 2024

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!

@xuzeyu91 xuzeyu91 changed the title [.Net][Issue]: [.Net][Issue]: How to communicate with multi-agent through API May 9, 2024
@xuzeyu91
Copy link
Author

xuzeyu91 commented May 9, 2024

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

@LittleLittleCloud
Copy link
Collaborator

LittleLittleCloud commented May 9, 2024

@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);
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants