-
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 - Support name based KernelFunction*Strategy (#9967)
### 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. --> Working with customer looking to minimize token usage. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> KernelFunction _selection_ and _termination_ strategies that evaluate name only can save tokens by not including message content. ### 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
8 changed files
with
107 additions
and
80 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
69 changes: 0 additions & 69 deletions
69
dotnet/samples/Concepts/Agents/OpenAIAssistant_FileService.cs
This file was deleted.
Oops, something went wrong.
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
82 changes: 82 additions & 0 deletions
82
dotnet/src/Agents/UnitTests/Core/Internal/ChatMessageForPromptTests.cs
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,82 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System.Text.Json; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Agents.Internal; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.Agents.UnitTests.Core.Internal; | ||
|
||
/// <summary> | ||
/// Unit testing of <see cref="ChatMessageForPrompt"/>. | ||
/// </summary> | ||
public class ChatMessageForPromptTests | ||
{ | ||
/// <summary> | ||
/// Verify <see cref="ChatMessageForPrompt"/> formats history for prompt. | ||
/// </summary> | ||
[Fact] | ||
public void VerifyFormatHistoryAsync() | ||
{ | ||
// Arrange & Act | ||
string history = ChatMessageForPrompt.Format([]); | ||
// Assert | ||
VerifyMessageCount<ChatMessageForTest>(history, 0); | ||
|
||
// Arrange & Act | ||
history = ChatMessageForPrompt.Format(CreatHistory()); | ||
// Assert | ||
ChatMessageForTest[] messages = VerifyMessageCount<ChatMessageForTest>(history, 4); | ||
Assert.Equal("test", messages[1].Name); | ||
Assert.Equal(string.Empty, messages[2].Name); | ||
Assert.Equal("test", messages[3].Name); | ||
} | ||
|
||
/// <summary> | ||
/// Verify <see cref="ChatMessageForPrompt"/> formats history using name only. | ||
/// </summary> | ||
[Fact] | ||
public void VerifyFormatNamesAsync() | ||
{ | ||
// Arrange & Act | ||
string history = ChatMessageForPrompt.Format([], useNameOnly: true); | ||
// Assert | ||
VerifyMessageCount<string>(history, 0); | ||
|
||
// Arrange & Act | ||
history = ChatMessageForPrompt.Format(CreatHistory(), useNameOnly: true); | ||
// Assert | ||
string[] names = VerifyMessageCount<string>(history, 4); | ||
Assert.Equal("test", names[1]); | ||
Assert.Equal(AuthorRole.Assistant.Label, names[2]); | ||
Assert.Equal("test", names[3]); | ||
} | ||
|
||
private static TResult[] VerifyMessageCount<TResult>(string history, int expectedLength) | ||
{ | ||
TResult[]? messages = JsonSerializer.Deserialize<TResult[]>(history); | ||
Assert.NotNull(messages); | ||
Assert.Equal(expectedLength, messages.Length); | ||
return messages; | ||
} | ||
|
||
private static ChatHistory CreatHistory() | ||
{ | ||
return | ||
[ | ||
new ChatMessageContent(AuthorRole.User, "content1"), | ||
new ChatMessageContent(AuthorRole.Assistant, "content1") { AuthorName = "test" }, | ||
new ChatMessageContent(AuthorRole.Assistant, "content1"), | ||
new ChatMessageContent(AuthorRole.Assistant, "content1") { AuthorName = "test" }, | ||
]; | ||
} | ||
|
||
private sealed class ChatMessageForTest | ||
{ | ||
public string Role { get; init; } = string.Empty; | ||
|
||
public string? Name { get; init; } = string.Empty; | ||
|
||
public string Content { get; init; } = string.Empty; | ||
} | ||
} |