-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net Agents - Add Streaming support for
OpenAIAssistantAgent
and `A…
…gentChat` (#8175) ### 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. --> Add streaming support to direct invocation of `OpenAIAssistantAgent` and also `AgentChat`: Fixes: #4833 Fixes: #5643 Integrates with existing streaming support for `ChatCompletionAgent`. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Streaming returns the expected streaming content types as well as providing whole messages in the provided history. Support both agent invocation (no-chat) and via `AgentChat`. Introduced: - `StreamingAnnotationContent` - `StreamingFileReferenceContent` Added samples and 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 😄 --------- Co-authored-by: SergeyMenshykh <[email protected]> Co-authored-by: Roger Barreto <[email protected]> Co-authored-by: SergeyMenshykh <[email protected]> Co-authored-by: Roger Barreto <[email protected]> Co-authored-by: Dmytro Struk <[email protected]>
- Loading branch information
1 parent
e403734
commit 6426911
Showing
30 changed files
with
1,396 additions
and
219 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Agents; | ||
using Microsoft.SemanticKernel.Agents.Chat; | ||
using Microsoft.SemanticKernel.Agents.OpenAI; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
|
||
namespace Agents; | ||
|
||
/// <summary> | ||
/// Demonstrate consuming "streaming" message for <see cref="ChatCompletionAgent"/> and | ||
/// <see cref="OpenAIAssistantAgent"/> both participating in an <see cref="AgentChat"/>. | ||
/// </summary> | ||
public class MixedChat_Streaming(ITestOutputHelper output) : BaseAgentsTest(output) | ||
{ | ||
private const string ReviewerName = "ArtDirector"; | ||
private const string ReviewerInstructions = | ||
""" | ||
You are an art director who has opinions about copywriting born of a love for David Ogilvy. | ||
The goal is to determine is the given copy is acceptable to print. | ||
If so, state that it is approved. | ||
If not, provide insight on how to refine suggested copy without example. | ||
"""; | ||
|
||
private const string CopyWriterName = "CopyWriter"; | ||
private const string CopyWriterInstructions = | ||
""" | ||
You are a copywriter with ten years of experience and are known for brevity and a dry humor. | ||
The goal is to refine and decide on the single best copy as an expert in the field. | ||
Only provide a single proposal per response. | ||
You're laser focused on the goal at hand. | ||
Don't waste time with chit chat. | ||
Consider suggestions when refining an idea. | ||
"""; | ||
|
||
[Fact] | ||
public async Task UseStreamingAgentChatAsync() | ||
{ | ||
// Define the agents: one of each type | ||
ChatCompletionAgent agentReviewer = | ||
new() | ||
{ | ||
Instructions = ReviewerInstructions, | ||
Name = ReviewerName, | ||
Kernel = this.CreateKernelWithChatCompletion(), | ||
}; | ||
|
||
OpenAIAssistantAgent agentWriter = | ||
await OpenAIAssistantAgent.CreateAsync( | ||
kernel: new(), | ||
clientProvider: this.GetClientProvider(), | ||
definition: new(this.Model) | ||
{ | ||
Instructions = CopyWriterInstructions, | ||
Name = CopyWriterName, | ||
Metadata = AssistantSampleMetadata, | ||
}); | ||
|
||
// Create a chat for agent interaction. | ||
AgentGroupChat chat = | ||
new(agentWriter, agentReviewer) | ||
{ | ||
ExecutionSettings = | ||
new() | ||
{ | ||
// Here a TerminationStrategy subclass is used that will terminate when | ||
// an assistant message contains the term "approve". | ||
TerminationStrategy = | ||
new ApprovalTerminationStrategy() | ||
{ | ||
// Only the art-director may approve. | ||
Agents = [agentReviewer], | ||
// Limit total number of turns | ||
MaximumIterations = 10, | ||
} | ||
} | ||
}; | ||
|
||
// Invoke chat and display messages. | ||
ChatMessageContent input = new(AuthorRole.User, "concept: maps made out of egg cartons."); | ||
chat.AddChatMessage(input); | ||
this.WriteAgentChatMessage(input); | ||
|
||
string lastAgent = string.Empty; | ||
await foreach (StreamingChatMessageContent response in chat.InvokeStreamingAsync()) | ||
{ | ||
if (string.IsNullOrEmpty(response.Content)) | ||
{ | ||
continue; | ||
} | ||
|
||
if (!lastAgent.Equals(response.AuthorName, StringComparison.Ordinal)) | ||
{ | ||
Console.WriteLine($"\n# {response.Role} - {response.AuthorName ?? "*"}:"); | ||
lastAgent = response.AuthorName ?? string.Empty; | ||
} | ||
|
||
Console.WriteLine($"\t > streamed: '{response.Content}'"); | ||
} | ||
|
||
// Display the chat history. | ||
Console.WriteLine("================================"); | ||
Console.WriteLine("CHAT HISTORY"); | ||
Console.WriteLine("================================"); | ||
|
||
ChatMessageContent[] history = await chat.GetChatMessagesAsync().Reverse().ToArrayAsync(); | ||
|
||
for (int index = 0; index < history.Length; index++) | ||
{ | ||
this.WriteAgentChatMessage(history[index]); | ||
} | ||
|
||
Console.WriteLine($"\n[IS COMPLETED: {chat.IsComplete}]"); | ||
} | ||
|
||
private sealed class ApprovalTerminationStrategy : TerminationStrategy | ||
{ | ||
// Terminate when the final message contains the term "approve" | ||
protected override Task<bool> ShouldAgentTerminateAsync(Agent agent, IReadOnlyList<ChatMessageContent> history, CancellationToken cancellationToken) | ||
=> Task.FromResult(history[history.Count - 1].Content?.Contains("approve", StringComparison.OrdinalIgnoreCase) ?? false); | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
dotnet/samples/Concepts/Agents/OpenAIAssistant_Streaming.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,142 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
using System.ComponentModel; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Agents.OpenAI; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
|
||
namespace Agents; | ||
|
||
/// <summary> | ||
/// Demonstrate consuming "streaming" message for <see cref="OpenAIAssistantAgent"/>. | ||
/// </summary> | ||
public class OpenAIAssistant_Streaming(ITestOutputHelper output) : BaseAgentsTest(output) | ||
{ | ||
private const string ParrotName = "Parrot"; | ||
private const string ParrotInstructions = "Repeat the user message in the voice of a pirate and then end with a parrot sound."; | ||
|
||
[Fact] | ||
public async Task UseStreamingChatCompletionAgentAsync() | ||
{ | ||
// Define the agent | ||
OpenAIAssistantAgent agent = | ||
await OpenAIAssistantAgent.CreateAsync( | ||
kernel: new(), | ||
clientProvider: this.GetClientProvider(), | ||
new(this.Model) | ||
{ | ||
Instructions = ParrotInstructions, | ||
Name = ParrotName, | ||
Metadata = AssistantSampleMetadata, | ||
}); | ||
|
||
// Create a thread for the agent conversation. | ||
string threadId = await agent.CreateThreadAsync(new OpenAIThreadCreationOptions { Metadata = AssistantSampleMetadata }); | ||
|
||
// Respond to user input | ||
await InvokeAgentAsync(agent, threadId, "Fortune favors the bold."); | ||
await InvokeAgentAsync(agent, threadId, "I came, I saw, I conquered."); | ||
await InvokeAgentAsync(agent, threadId, "Practice makes perfect."); | ||
|
||
// Output the entire chat history | ||
await DisplayChatHistoryAsync(agent, threadId); | ||
} | ||
|
||
[Fact] | ||
public async Task UseStreamingChatCompletionAgentWithPluginAsync() | ||
{ | ||
const string MenuInstructions = "Answer questions about the menu."; | ||
|
||
// Define the agent | ||
OpenAIAssistantAgent agent = | ||
await OpenAIAssistantAgent.CreateAsync( | ||
kernel: new(), | ||
clientProvider: this.GetClientProvider(), | ||
new(this.Model) | ||
{ | ||
Instructions = MenuInstructions, | ||
Name = "Host", | ||
Metadata = AssistantSampleMetadata, | ||
}); | ||
|
||
// Initialize plugin and add to the agent's Kernel (same as direct Kernel usage). | ||
KernelPlugin plugin = KernelPluginFactory.CreateFromType<MenuPlugin>(); | ||
agent.Kernel.Plugins.Add(plugin); | ||
|
||
// Create a thread for the agent conversation. | ||
string threadId = await agent.CreateThreadAsync(new OpenAIThreadCreationOptions { Metadata = AssistantSampleMetadata }); | ||
|
||
// Respond to user input | ||
await InvokeAgentAsync(agent, threadId, "What is the special soup?"); | ||
await InvokeAgentAsync(agent, threadId, "What is the special drink?"); | ||
|
||
// Output the entire chat history | ||
await DisplayChatHistoryAsync(agent, threadId); | ||
} | ||
|
||
// Local function to invoke agent and display the conversation messages. | ||
private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, string threadId, string input) | ||
{ | ||
ChatMessageContent message = new(AuthorRole.User, input); | ||
await agent.AddChatMessageAsync(threadId, message); | ||
this.WriteAgentChatMessage(message); | ||
|
||
ChatHistory history = []; | ||
|
||
bool isFirst = false; | ||
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(threadId, history)) | ||
{ | ||
if (string.IsNullOrEmpty(response.Content)) | ||
{ | ||
continue; | ||
} | ||
|
||
if (!isFirst) | ||
{ | ||
Console.WriteLine($"\n# {response.Role} - {response.AuthorName ?? "*"}:"); | ||
isFirst = true; | ||
} | ||
|
||
Console.WriteLine($"\t > streamed: '{response.Content}'"); | ||
} | ||
|
||
foreach (ChatMessageContent content in history) | ||
{ | ||
this.WriteAgentChatMessage(content); | ||
} | ||
} | ||
|
||
private async Task DisplayChatHistoryAsync(OpenAIAssistantAgent agent, string threadId) | ||
{ | ||
Console.WriteLine("================================"); | ||
Console.WriteLine("CHAT HISTORY"); | ||
Console.WriteLine("================================"); | ||
|
||
ChatMessageContent[] messages = await agent.GetThreadMessagesAsync(threadId).ToArrayAsync(); | ||
for (int index = messages.Length - 1; index >= 0; --index) | ||
{ | ||
this.WriteAgentChatMessage(messages[index]); | ||
} | ||
} | ||
|
||
public sealed class MenuPlugin | ||
{ | ||
[KernelFunction, Description("Provides a list of specials from the menu.")] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1024:Use properties where appropriate", Justification = "Too smart")] | ||
public string GetSpecials() | ||
{ | ||
return @" | ||
Special Soup: Clam Chowder | ||
Special Salad: Cobb Salad | ||
Special Drink: Chai Tea | ||
"; | ||
} | ||
|
||
[KernelFunction, Description("Provides the price of the requested menu item.")] | ||
public string GetItemPrice( | ||
[Description("The name of the menu item.")] | ||
string menuItem) | ||
{ | ||
return "$9.99"; | ||
} | ||
} | ||
} |
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.