-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net - Add support for Name property to ChatMessageContent (#5666)
### 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. --> Tracking identity is critical for multi-agent conversations. It is also supported as part of the core chat-completion api: https://platform.openai.com/docs/api-reference/chat/create ``` { "messages": [ { "content": "Write one paragraph in response to the user that rhymes", "name": "Echo", "role": "system" }, { "content": "Why is AI awesome", "name": "Ralph", "role": "user" } ], "temperature": 1, "top_p": 0.5, "n": 3, "presence_penalty": 0, "frequency_penalty": 0, "model": "gpt-4" } ``` ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Add support for `ChatMessageContent.Name` property with optional, non-breaking patterns. ### 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 😄 --------- Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
- Loading branch information
1 parent
67eda98
commit ca9e3ae
Showing
10 changed files
with
198 additions
and
42 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
dotnet/samples/KernelSyntaxExamples/Example37_CompletionIdentity.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,124 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Microsoft.SemanticKernel.Connectors.OpenAI; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Examples; | ||
|
||
// The following example shows how to use Semantic Kernel with identity associated with each chat message. | ||
public class Example37_CompletionIdentity : BaseTest | ||
{ | ||
/// <summary> | ||
/// Flag to force usage of OpenAI configuration if both <see cref="TestConfiguration.OpenAI"/> | ||
/// and <see cref="TestConfiguration.AzureOpenAI"/> are defined. | ||
/// If 'false', Azure takes precedence. | ||
/// </summary> | ||
/// <remarks> | ||
/// NOTE: Retrieval tools is not currently available on Azure. | ||
/// </remarks> | ||
private const bool ForceOpenAI = true; | ||
|
||
private static readonly OpenAIPromptExecutionSettings s_executionSettings = | ||
new() | ||
{ | ||
FrequencyPenalty = 0, | ||
PresencePenalty = 0, | ||
Temperature = 1, | ||
TopP = 0.5, | ||
}; | ||
|
||
[Theory] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
public async Task CompletionIdentityAsync(bool withName) | ||
{ | ||
WriteLine("======== Completion Identity ========"); | ||
|
||
IChatCompletionService chatService = CreateCompletionService(); | ||
|
||
ChatHistory chatHistory = CreateHistory(withName); | ||
|
||
WriteMessages(chatHistory); | ||
|
||
WriteMessages(await chatService.GetChatMessageContentsAsync(chatHistory, s_executionSettings), chatHistory); | ||
|
||
ValidateMessages(chatHistory, withName); | ||
} | ||
|
||
[Theory] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
public async Task StreamingIdentityAsync(bool withName) | ||
{ | ||
WriteLine("======== Completion Identity ========"); | ||
|
||
IChatCompletionService chatService = CreateCompletionService(); | ||
|
||
ChatHistory chatHistory = CreateHistory(withName); | ||
|
||
var content = await chatHistory.AddStreamingMessageAsync(chatService.GetStreamingChatMessageContentsAsync(chatHistory, s_executionSettings).Cast<OpenAIStreamingChatMessageContent>()).ToArrayAsync(); | ||
|
||
WriteMessages(chatHistory); | ||
|
||
ValidateMessages(chatHistory, withName); | ||
} | ||
|
||
private static ChatHistory CreateHistory(bool withName) | ||
{ | ||
return | ||
new ChatHistory() | ||
{ | ||
new ChatMessageContent(AuthorRole.System, "Write one paragraph in response to the user that rhymes") { AuthorName = withName ? "Echo" : null }, | ||
new ChatMessageContent(AuthorRole.User, "Why is AI awesome") { AuthorName = withName ? "Ralph" : null }, | ||
}; | ||
} | ||
|
||
private void ValidateMessages(ChatHistory chatHistory, bool expectName) | ||
{ | ||
foreach (var message in chatHistory) | ||
{ | ||
if (expectName && message.Role != AuthorRole.Assistant) | ||
{ | ||
Assert.NotNull(message.AuthorName); | ||
} | ||
else | ||
{ | ||
Assert.Null(message.AuthorName); | ||
} | ||
} | ||
} | ||
|
||
private void WriteMessages(IReadOnlyList<ChatMessageContent> messages, ChatHistory? history = null) | ||
{ | ||
foreach (var message in messages) | ||
{ | ||
WriteLine($"# {message.Role}:{message.AuthorName ?? "?"} - {message.Content ?? "-"}"); | ||
} | ||
|
||
history?.AddRange(messages); | ||
} | ||
|
||
private static IChatCompletionService CreateCompletionService() | ||
{ | ||
return | ||
ForceOpenAI || string.IsNullOrEmpty(TestConfiguration.AzureOpenAI.Endpoint) ? | ||
new OpenAIChatCompletionService( | ||
TestConfiguration.OpenAI.ChatModelId, | ||
TestConfiguration.OpenAI.ApiKey) : | ||
new AzureOpenAIChatCompletionService( | ||
deploymentName: TestConfiguration.AzureOpenAI.ChatDeploymentName, | ||
endpoint: TestConfiguration.AzureOpenAI.Endpoint, | ||
apiKey: TestConfiguration.AzureOpenAI.ApiKey, | ||
modelId: TestConfiguration.AzureOpenAI.ChatModelId); | ||
} | ||
|
||
public Example37_CompletionIdentity(ITestOutputHelper output) : base(output) | ||
{ | ||
} | ||
} |
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
Oops, something went wrong.