-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net Agents - Align with expectations when arguments override specifi…
…ed. (#9096) ### 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. --> When default `KernelArguments` are specified for `KernelAgent.Arguments`, it is likely expected that when override `arguments` parameter is provided that this override doesn't necessarily invalidate the default `KernelArguments.ExecutionSettings`. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Provide performant merge logic for _default_ and _override_ `KernelArguments` for all invocations. This merge preserves any _default_ values not specifically included in the _override_...including both `ExecutionSettings` and parameters. Solves this (common) pattern: ```c# ChatCompletionAgent agent = new() { Name = "SampleAssistantAgent", Instructions = """ Something something. Something dynamic: {{$topic}} The current date and time is: {{$now}}. """, Kernel = kernel, Arguments = new KernelArguments(new AzureOpenAIPromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }) { { "topic", "default topic" } } }; KernelArguments arguments = new() { { "now", $"{now.ToShortDateString()} {now.ToShortTimeString()}" } }; await foreach (ChatMessageContent response in agent.InvokeAsync(history, arguments)) { ... } ``` ### 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
Showing
6 changed files
with
179 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System.Linq; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Agents; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.Agents.UnitTests; | ||
|
||
/// <summary> | ||
/// Verify behavior of <see cref="KernelAgent"/> base class. | ||
/// </summary> | ||
public class KernelAgentTests | ||
{ | ||
/// <summary> | ||
/// Verify ability to merge null <see cref="KernelArguments"/>. | ||
/// </summary> | ||
[Fact] | ||
public void VerifyNullArgumentMerge() | ||
{ | ||
// Arrange | ||
MockAgent agentWithNullArguments = new(); | ||
// Act | ||
KernelArguments? arguments = agentWithNullArguments.MergeArguments(null); | ||
// Assert | ||
Assert.Null(arguments); | ||
|
||
// Arrange | ||
KernelArguments overrideArguments = []; | ||
// Act | ||
arguments = agentWithNullArguments.MergeArguments(overrideArguments); | ||
// Assert | ||
Assert.NotNull(arguments); | ||
Assert.StrictEqual(overrideArguments, arguments); | ||
|
||
// Arrange | ||
MockAgent agentWithEmptyArguments = new() { Arguments = new() }; | ||
// Act | ||
arguments = agentWithEmptyArguments.MergeArguments(null); | ||
// Assert | ||
Assert.NotNull(arguments); | ||
Assert.StrictEqual(agentWithEmptyArguments.Arguments, arguments); | ||
} | ||
|
||
/// <summary> | ||
/// Verify ability to merge <see cref="KernelArguments"/> parameters. | ||
/// </summary> | ||
[Fact] | ||
public void VerifyArgumentParameterMerge() | ||
{ | ||
// Arrange | ||
MockAgent agentWithArguments = new() { Arguments = new() { { "a", 1 } } }; | ||
KernelArguments overrideArguments = new() { { "b", 2 } }; | ||
|
||
// Act | ||
KernelArguments? arguments = agentWithArguments.MergeArguments(overrideArguments); | ||
|
||
// Assert | ||
Assert.NotNull(arguments); | ||
Assert.Equal(2, arguments.Count); | ||
Assert.Equal(1, arguments["a"]); | ||
Assert.Equal(2, arguments["b"]); | ||
|
||
// Arrange | ||
overrideArguments["a"] = 11; | ||
overrideArguments["c"] = 3; | ||
|
||
// Act | ||
arguments = agentWithArguments.MergeArguments(overrideArguments); | ||
|
||
// Assert | ||
Assert.NotNull(arguments); | ||
Assert.Equal(3, arguments.Count); | ||
Assert.Equal(11, arguments["a"]); | ||
Assert.Equal(2, arguments["b"]); | ||
Assert.Equal(3, arguments["c"]); | ||
} | ||
|
||
/// <summary> | ||
/// Verify ability to merge <see cref="KernelArguments.ExecutionSettings"/>. | ||
/// </summary> | ||
[Fact] | ||
public void VerifyArgumentSettingsMerge() | ||
{ | ||
// Arrange | ||
FunctionChoiceBehavior autoInvoke = FunctionChoiceBehavior.Auto(); | ||
MockAgent agentWithSettings = new() { Arguments = new(new PromptExecutionSettings() { FunctionChoiceBehavior = autoInvoke }) }; | ||
KernelArguments overrideArgumentsNoSettings = new(); | ||
|
||
// Act | ||
KernelArguments? arguments = agentWithSettings.MergeArguments(overrideArgumentsNoSettings); | ||
|
||
// Assert | ||
Assert.NotNull(arguments); | ||
Assert.NotNull(arguments.ExecutionSettings); | ||
Assert.Single(arguments.ExecutionSettings); | ||
Assert.StrictEqual(autoInvoke, arguments.ExecutionSettings.First().Value.FunctionChoiceBehavior); | ||
|
||
// Arrange | ||
FunctionChoiceBehavior noInvoke = FunctionChoiceBehavior.None(); | ||
KernelArguments overrideArgumentsWithSettings = new(new PromptExecutionSettings() { FunctionChoiceBehavior = noInvoke }); | ||
|
||
// Act | ||
arguments = agentWithSettings.MergeArguments(overrideArgumentsWithSettings); | ||
|
||
// Assert | ||
Assert.NotNull(arguments); | ||
Assert.NotNull(arguments.ExecutionSettings); | ||
Assert.Single(arguments.ExecutionSettings); | ||
Assert.StrictEqual(noInvoke, arguments.ExecutionSettings.First().Value.FunctionChoiceBehavior); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters