Skip to content

Commit

Permalink
.Net Agents - Fixed Assistant Function-Call Message Role (#9281)
Browse files Browse the repository at this point in the history
### Motivation and Context
<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

Fixes: #9245

### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

Assistant incorrectly assigning `Tool` role to function-call _and_
function-result. Function-call should be `Assistant`.

Added validation in integration tests.

### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->

- [X] The code builds clean without any errors or warnings
- [X] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄
  • Loading branch information
crickman authored Oct 15, 2024
1 parent e23d636 commit 60e49b1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ private static (FunctionName functionName, KernelArguments arguments) ParseFunct

private static ChatMessageContent GenerateFunctionCallContent(string agentName, IList<FunctionCallContent> functionCalls)
{
ChatMessageContent functionCallContent = new(AuthorRole.Tool, content: null)
ChatMessageContent functionCallContent = new(AuthorRole.Assistant, content: null)
{
AuthorName = agentName
};
Expand Down
29 changes: 27 additions & 2 deletions dotnet/src/IntegrationTests/Agents/MixedAgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.ClientModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Identity;
Expand Down Expand Up @@ -81,25 +82,28 @@ private async Task VerifyAgentExecutionAsync(
new OpenAIPromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() } :
new OpenAIPromptExecutionSettings() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };

// Configure chat agent with the plugin.
// Chat agent doesn't need plug-in since it has access to the shared function result.
ChatCompletionAgent chatAgent =
new()
{
Name = "Chat",
Kernel = chatCompletionKernel,
Instructions = "Answer questions about the menu.",
Arguments = new(executionSettings),
};
chatAgent.Kernel.Plugins.Add(plugin);

// Assistant doesn't need plug-in since it has access to the shared function result.
// Configure assistant agent with the plugin.
OpenAIAssistantAgent assistantAgent =
await OpenAIAssistantAgent.CreateAsync(
config,
new(modelName)
{
Name = "Assistant",
Instructions = "Answer questions about the menu."
},
new Kernel());
assistantAgent.Kernel.Plugins.Add(plugin);

// Act & Assert
try
Expand Down Expand Up @@ -127,6 +131,27 @@ private async Task AssertAgentInvocationAsync(AgentGroupChat chat, Agent agent,

// Assert
Assert.Contains(expected, builder.ToString(), StringComparison.OrdinalIgnoreCase);
await foreach (var message in chat.GetChatMessagesAsync())
{
AssertMessageValid(message);
}
}

private static void AssertMessageValid(ChatMessageContent message)
{
if (message.Items.OfType<FunctionResultContent>().Any())
{
Assert.Equal(AuthorRole.Tool, message.Role);
return;
}

if (message.Items.OfType<FunctionCallContent>().Any())
{
Assert.Equal(AuthorRole.Assistant, message.Role);
return;
}

Assert.Equal(string.IsNullOrEmpty(message.AuthorName) ? AuthorRole.User : AuthorRole.Assistant, message.Role);
}

private Kernel CreateChatCompletionKernel(AzureOpenAIConfiguration configuration)
Expand Down
21 changes: 21 additions & 0 deletions dotnet/src/IntegrationTests/Agents/OpenAIAssistantAgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ await OpenAIAssistantAgent.CreateAsync(

// Assert
Assert.Contains(expected, builder.ToString(), StringComparison.OrdinalIgnoreCase);
await foreach (var message in chat.GetChatMessagesAsync())
{
AssertMessageValid(message);
}
}
finally
{
Expand Down Expand Up @@ -179,6 +183,23 @@ await OpenAIAssistantAgent.CreateAsync(
Assert.Contains(expected, history.First().Content, StringComparison.OrdinalIgnoreCase);
}

private static void AssertMessageValid(ChatMessageContent message)
{
if (message.Items.OfType<FunctionResultContent>().Any())
{
Assert.Equal(AuthorRole.Tool, message.Role);
return;
}

if (message.Items.OfType<FunctionCallContent>().Any())
{
Assert.Equal(AuthorRole.Assistant, message.Role);
return;
}

Assert.Equal(string.IsNullOrEmpty(message.AuthorName) ? AuthorRole.User : AuthorRole.Assistant, message.Role);
}

public sealed class MenuPlugin
{
[KernelFunction, Description("Provides a list of specials from the menu.")]
Expand Down

0 comments on commit 60e49b1

Please sign in to comment.