-
Notifications
You must be signed in to change notification settings - Fork 7k
Optimize the selection NextSpeaker mechanism of RolePlayOrchestrator … #6688
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
base: main
Are you sure you want to change the base?
Changes from all commits
d3bd3c4
4ee44f7
8464123
ca2d4b8
8912db2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,10 @@ | |
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AutoGen.Core.Orchestrator; | ||
|
||
namespace AutoGen.Core; | ||
|
||
|
@@ -65,12 +67,20 @@ public RolePlayOrchestrator(IAgent admin, Graph? workflow = null) | |
var agentNames = candidates.Select(candidate => candidate.Name); | ||
var rolePlayMessage = new TextMessage(Role.User, | ||
content: $@"You are in a role play game. Carefully read the conversation history and carry on the conversation. | ||
The available roles are: | ||
{string.Join(",", agentNames)} | ||
Each message will start with 'From name:', e.g: | ||
From {agentNames.First()}: | ||
//your message//."); | ||
## Available Speaker Names | ||
{string.Join($"{Environment.NewLine}", agentNames.Select(z => $"- {z}"))} | ||
## Output Role | ||
Each message will use the strickly JSON format with a '//finish' suffix: | ||
{{""Speaker"":""<From Speaker Name>"", ""Message"":""<Chat Message>""}}//finish | ||
e,g: | ||
{{""Speaker"":""{agentNames.First()}"", ""Message"":""Hi, I'm {agentNames.First()}.""}}//finish | ||
Note: | ||
1. ""Speaker"" must be one of the most suitable names in ""Available Speaker names"". You cannot create it yourself, nor can you merge two names, it must be 100% exactly equal. | ||
2. You have to output clean JSON result, no other words are allowed."); | ||
|
||
var chatHistoryWithName = this.ProcessConversationsForRolePlay(context.ChatHistory); | ||
var messages = new IMessage[] { rolePlayMessage }.Concat(chatHistoryWithName); | ||
|
@@ -80,23 +90,101 @@ public RolePlayOrchestrator(IAgent admin, Graph? workflow = null) | |
options: new GenerateReplyOptions | ||
{ | ||
Temperature = 0, | ||
MaxToken = 128, | ||
StopSequence = [":"], | ||
MaxToken = 1024, | ||
StopSequence = ["//finish"], | ||
LittleLittleCloud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Functions = null, | ||
}, | ||
cancellationToken: cancellationToken); | ||
|
||
var name = response.GetContent() ?? throw new ArgumentException("No name is returned."); | ||
var responseMessageStr = response.GetContent() ?? throw new ArgumentException("No name is returned."); | ||
|
||
RolePlayOrchestratorResponse? responseMessage; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
|
||
try | ||
{ | ||
responseMessage = JsonSerializer.Deserialize<RolePlayOrchestratorResponse>(responseMessageStr); | ||
if (responseMessage == null) | ||
{ | ||
throw new Exception("Incorrect RolePlayOrchestratorResponse JSON format."); | ||
} | ||
} | ||
catch | ||
{ | ||
throw; | ||
} | ||
|
||
// remove From | ||
name = name!.Substring(5); | ||
var candidate = candidates.FirstOrDefault(x => x.Name!.ToLower() == name.ToLower()); | ||
var name = responseMessage.Speaker; | ||
var candidate = candidates.FirstOrDefault(x => x.Name!.ToUpper() == name!.ToUpper()); | ||
|
||
if (candidate != null) | ||
{ | ||
return candidate; | ||
} | ||
|
||
//Regain the correct name | ||
var regainMessage = new TextMessage(Role.User, | ||
content: @$"Choose a name that is closest to the meaning from ""Available Speaker Names"" by ""Input Name"". | ||
## Example | ||
### Available Speaker Names | ||
- Sales Manager | ||
- General Manager Assistant | ||
- Chief Financial Officer | ||
### Input Name | ||
CFO | ||
### Outout Name | ||
{{""Speaker"":""Chief Financial Officer"", ""Message"":""""}}//finish | ||
## Task | ||
Output Name must be one of the name in the following ""Available Speaker Names"" without any change. | ||
Note: | ||
1. ""Speaker"" must be one of the most suitable names in ""Available Speaker Names"". You cannot create it yourself, nor can you merge two names, it must be 100% exactly equal. | ||
2. You have to output clean JSON result,no other words are allowed. | ||
### Speaker List | ||
{string.Join($"{Environment.NewLine}", agentNames.Select(z => $"- {z}"))} | ||
### Input Name | ||
{name} | ||
### Output Name"); | ||
|
||
var regainResponse = await this.admin.GenerateReplyAsync( | ||
messages: new[] { regainMessage }, | ||
options: new GenerateReplyOptions | ||
{ | ||
Temperature = 0, | ||
MaxToken = 1024, | ||
StopSequence = ["//finish"], | ||
Functions = null, | ||
}, | ||
cancellationToken: cancellationToken); | ||
|
||
RolePlayOrchestratorResponse? regainResponseMessage; | ||
var regainNameStr = regainResponse.GetContent() ?? throw new ArgumentException("No name is returned."); | ||
try | ||
{ | ||
regainResponseMessage = JsonSerializer.Deserialize<RolePlayOrchestratorResponse>(regainNameStr); | ||
if (regainResponseMessage == null) | ||
{ | ||
throw new Exception("Incorrect RolePlayOrchestratorResponse JSON format."); | ||
} | ||
} | ||
catch | ||
{ | ||
throw; | ||
} | ||
|
||
var reaginCandidate = candidates.FirstOrDefault(x => x.Name!.ToUpper() == regainResponseMessage.Speaker!.ToUpper()); | ||
|
||
if (reaginCandidate != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
return reaginCandidate; | ||
} | ||
|
||
var errorMessage = $"The response from admin is {name}, which is either not in the candidates list or not in the correct format."; | ||
throw new ArgumentException(errorMessage); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// RolePlayOrchestratorResponse.cs | ||
|
||
namespace AutoGen.Core.Orchestrator; | ||
internal class RolePlayOrchestratorResponse | ||
Check failure on line 5 in dotnet/src/AutoGen.Core/Orchestrator/RolePlayOrchestratorResponse.cs
|
||
{ | ||
internal string? Speaker { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The access modifier of property doesn't have to be also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. public is also fine. |
||
internal string? Message { get; set; } | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe adding a class-level summary on top?