In AgentGroupChat only One Agent is Participating out of three? My Code is in C# and uses Agentic Framework #10148
Answered
by
crickman
lovedeepatsgit
asked this question in
Q&A
-
In the Below Code Only One Agent (Agent_One) responds and the conversation stops, I have tried this code many times but still getting the same issue, If someone have some insights or solutions then please reply to this query.using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Agents.Chat;
using Microsoft.SemanticKernel.Agents.History;
using System;
string AZURE_OPEN_AI_ENDPOINT = "ABC";
string AZURE_OPEN_AI_KEY = "XYZ";
string DEPLOYMENT_MODEL = "gpt-4o";
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(
DEPLOYMENT_MODEL,
AZURE_OPEN_AI_ENDPOINT,
AZURE_OPEN_AI_KEY);
Kernel kernel = builder.Build();
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
ChatCompletionAgent agent_teacher =
new()
{
Name = "Agent_One",
Instructions = "You are an Helpful Ai Teacher, who helps students to Learn Statistics.",
Kernel = kernel
};
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
ChatCompletionAgent beta_agent =
new()
{
Name = "Agent_Two",
Instructions = "Your are Assessment Teacher, Your task is listen to the conversation and ask questions to test the knowledge of students.",
Kernel = kernel
};
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
ChatCompletionAgent gamma_agent =
new()
{
Name = "Agent_Three",
Instructions = "You are an expert in Statistics, Your task is to provide the correct answers to the questions",
Kernel = kernel
};
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
//Defining the agent names for use in functions
#pragma warning disable CS0219 // Variable is assigned but its value is never used
const string Agent_One = "agent_teacher";
const string Agent_Two = "beta_agent";
const string Agent_Three = "gamma_agent";
#pragma warning restore CS0219 // Variable is assigned but its value is never used
// Defining a Kernel Function for Selection Strategy of Agents
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
KernelFunction selectionFunction =
AgentGroupChat.CreatePromptFunctionForStrategy(
$$$"""
Determine which participant to take the next turn in a conversation.
Choose only from these participants:
- {{Agent_One}}
- {{Agent_Two}}
- {{Agent_Three}}
Always follow these rules when selecting the next participant:
- After {{Agent_One}} takes a turn, {{Agent_Two}} should take the next turn.
- After {{Agent_Two}} takes a turn, {{Agent_Three}} should take the next turn.
History:
{{$history}}
""",
safeParameterNames: "history");
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
// Defining Selection staretgy for the agent group chat
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
KernelFunctionSelectionStrategy selectionStrategy =
new(selectionFunction, kernel)
{
// Always start with teacher_agent (agent_one)
InitialAgent = agent_teacher,
//Parse the function response.
ResultParser = (result) => result.GetValue<string>() ?? Agent_One,
// The prompt variable name for the history argument.
HistoryVariableName = "history",
};
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
// Defining a kernel function for the termination strategy of the agent group chat.
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
KernelFunction terminationFunction =
AgentGroupChat.CreatePromptFunctionForStrategy(
$$$"""
Determine if the conversation should be terminated.
if the gamma_agent has spoken type 'terminate'.
History:
{{$history}}
""",
safeParameterNames: "history");
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
//Define the termination strategy for the agent group chat.
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
KernelFunctionTerminationStrategy terminationStrategy =
new(terminationFunction, kernel)
{
//Agents can approve.
Agents =[agent_teacher,beta_agent,gamma_agent],
//Parse the function response.
ResultParser = (result) =>
result.GetValue<string>()?.Contains("terminate", StringComparison.OrdinalIgnoreCase) ?? false,
//The prompt variable name for the history argument.
HistoryVariableName = "history",
//Limit total number of turns to 10.
MaximumIterations = 10,
};
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
AgentGroupChat agentGroupChat = new(agent_teacher, beta_agent,gamma_agent)
{
ExecutionSettings = new AgentGroupChatSettings
{
SelectionStrategy = selectionStrategy,
TerminationStrategy = terminationStrategy,
}
};
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
agentGroupChat.AddChatMessage(new ChatMessageContent(AuthorRole.User, "What is the use statistics in real life?"));
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
await foreach (ChatMessageContent response in agentGroupChat.InvokeAsync())
{
Console.WriteLine("**************************************************");
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
Console.WriteLine(value: $"[{response.AuthorName}] {response.Content}");
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
}
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
ChatMessageContent[] history = await agentGroupChat.GetChatMessagesAsync().ToArrayAsync();
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
foreach (var message in history)
{
Console.WriteLine(message.ToString());
} |
Beta Was this translation helpful? Give feedback.
Answered by
crickman
Jan 10, 2025
Replies: 2 comments 3 replies
-
Tagging @crickman for visibility. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks for your question and the clear code sample, @lovedeepatsgit. A couple observations:
Please try running this version to make progress on your starting point: //Defining the agent names for use in functions
const string Agent_One = "agent_teacher";
const string Agent_Two = "beta_agent";
const string Agent_Three = "gamma_agent";
ChatCompletionAgent agent_teacher =
new()
{
//Name = "Agent_One",
Name = Agent_One,
Instructions = "You are an Helpful Ai Teacher, who helps students to Learn Statistics.",
Kernel = kernel
};
ChatCompletionAgent beta_agent =
new()
{
//Name = "Agent_Two",
Name = Agent_Two,
Instructions = "Your are Assessment Teacher, Your task is listen to the conversation and ask questions to test the knowledge of students.",
Kernel = kernel
};
ChatCompletionAgent gamma_agent =
new()
{
//Name = "Agent_Three",
Name = Agent_Three,
Instructions = "You are an expert in Statistics, Your task is to provide the correct answers to the questions",
Kernel = kernel
};
// Defining a Kernel Function for Selection Strategy of Agents
KernelFunction selectionFunction =
AgentGroupChat.CreatePromptFunctionForStrategy(
$$$"""
Determine which participant to take the next turn in a conversation.
Choose only from these participants:
- {{{Agent_One}}}
- {{{Agent_Two}}}
- {{{Agent_Three}}}
Always follow these rules when selecting the next participant:
- After {{{Agent_One}}} takes a turn, {{{Agent_Two}}} should take the next turn.
- After {{{Agent_Two}}} takes a turn, {{{Agent_Three}}} should take the next turn.
Reply only with the agent name without explanation or formatting.
History:
{{$history}}
""",
safeParameterNames: "history");
// Defining Selection staretgy for the agent group chat
KernelFunctionSelectionStrategy selectionStrategy =
new(selectionFunction, kernel)
{
// Always start with teacher_agent (agent_one)
InitialAgent = agent_teacher,
//Parse the function response.
ResultParser = (result) =>
{
Console.WriteLine("###: " + result);
return result.GetValue<string>() ?? Agent_One;
},
// The prompt variable name for the history argument.
HistoryVariableName = "history",
};
// Defining a kernel function for the termination strategy of the agent group chat.
KernelFunction terminationFunction =
AgentGroupChat.CreatePromptFunctionForStrategy(
$$$"""
if {{{Agent_Three}}} has spoken respond with the single word: terminate.
otherwise respond with the single word: continue.
History:
{{$history}}
""",
safeParameterNames: "history");
//Define the termination strategy for the agent group chat.
KernelFunctionTerminationStrategy terminationStrategy =
new(terminationFunction, kernel)
{
//Agents can approve.
Agents = [agent_teacher, beta_agent, gamma_agent],
//Parse the function response.
ResultParser = (result) =>
{
Console.WriteLine("$$$: " + result);
return result.GetValue<string>()?.Contains("terminate", StringComparison.OrdinalIgnoreCase) ?? false;
},
//The prompt variable name for the history argument.
HistoryVariableName = "history",
//Limit total number of turns to 10.
MaximumIterations = 10,
};
AgentGroupChat agentGroupChat = new(agent_teacher, beta_agent, gamma_agent)
{
ExecutionSettings = new AgentGroupChatSettings
{
SelectionStrategy = selectionStrategy,
TerminationStrategy = terminationStrategy,
}
};
agentGroupChat.AddChatMessage(new ChatMessageContent(AuthorRole.User, "What is the use statistics in real life?"));
await foreach (ChatMessageContent response in agentGroupChat.InvokeAsync())
{
Console.WriteLine("**************************************************");
Console.WriteLine(value: $"[{response.AuthorName}] {response.Content}");
}
Console.WriteLine(agentGroupChat.IsComplete); |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
lovedeepatsgit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your question and the clear code sample, @lovedeepatsgit.
A couple observations:
NoWarn
property in your .csproj instead of placing#pragma
statements in code:https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/GettingStartedWithAgents/GettingStartedWithAgents.csproj#L12
true
for the following statement:The conversation should not be terminated, as gamma_agent has not spoken.