forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net Agents - Update KernelFunction Based Strategies for `AgentGroupC…
…hat` (microsoft#8913) ### 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. --> Refine handling of history for `KernelFunctionSelectionStrategy` and `KernelFunctionTerminationStrategy` based on customer input. Fixes: microsoft#8898 Fixes: microsoft#8914 ``` Determine which participant takes the next turn in a conversation based on the the most recent participant. State only the name of the participant to take the next turn. No participant should take more than one turn in a row. Choose only from these participants: - ArtDirector - CopyWriter Always follow these rules when selecting the next participant: - After CopyWriter, it is ArtDirector's turn. - After ArtDirector, it is CopyWriter's turn. History: [ { "Role": "user", "Content": "concept: maps made out of egg cartons." }, { "Role": "Assistant", "Name": "CopyWriter", "Content": "Navigate your world, one carton at a time." } { "Role": "Assistant", "Name": "ArtDirector", "Content": "Approved. The copy effectively conveys the concept with a clever and concise tagline." } ] ``` ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> - Introduce convenience method (static) to create a `KernelFunction` from prompt-template and specifiy "safe" parameters. - Internally utilize `ChatMessageForPrompt` wrapper for `ChatMessageContent` to eliminate the inclusion of extraneous metadata in the strategy prompt. - Update `KernelFunctionSelectionStrategy` and `KernelFunctionTerminationStrategy` to call `ChatMessageForPrompt.Format` to format history for strategy prompt. - Add optional `HistoryReducer` property to `KernelFunctionSelectionStrategy` and `KernelFunctionTerminationStrategy` to limit how much history is included in the strategy prompt. - Updated sample ### 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
7 changed files
with
138 additions
and
21 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
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,44 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Microsoft.SemanticKernel.Agents.Internal; | ||
|
||
/// <summary> | ||
/// Present a <see cref="ChatMessageForPrompt"/> for serialization without metadata. | ||
/// </summary> | ||
/// <param name="message">The referenced message</param> | ||
internal sealed class ChatMessageForPrompt(ChatMessageContent message) | ||
{ | ||
private static readonly JsonSerializerOptions s_jsonOptions = new() { WriteIndented = true }; | ||
|
||
/// <summary> | ||
/// The string representation of the <see cref="ChatMessageContent.Role"/> property. | ||
/// </summary> | ||
public string Role => message.Role.Label; | ||
|
||
/// <summary> | ||
/// The referenced <see cref="ChatMessageContent.AuthorName"/> property. | ||
/// </summary> | ||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] | ||
public string? Name => message.AuthorName; | ||
|
||
/// <summary> | ||
/// The referenced <see cref="ChatMessageContent.Content"/> property. | ||
/// </summary> | ||
public string Content => message.Content ?? string.Empty; | ||
|
||
/// <summary> | ||
/// Convenience method to reference a set of messages. | ||
/// </summary> | ||
public static IEnumerable<ChatMessageForPrompt> Prepare(IEnumerable<ChatMessageContent> messages) => | ||
messages.Where(m => !string.IsNullOrWhiteSpace(m.Content)).Select(m => new ChatMessageForPrompt(m)); | ||
|
||
/// <summary> | ||
/// Convenience method to format a set of messages for use in a prompt. | ||
/// </summary> | ||
public static string Format(IEnumerable<ChatMessageContent> messages) => | ||
JsonSerializer.Serialize(Prepare(messages).ToArray(), s_jsonOptions); | ||
} |