Skip to content

Commit

Permalink
.Net Agents - Add Streaming support for OpenAIAssistantAgent and `A…
Browse files Browse the repository at this point in the history
…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
6 people authored Sep 9, 2024
1 parent e403734 commit 6426911
Show file tree
Hide file tree
Showing 30 changed files with 1,396 additions and 219 deletions.
43 changes: 31 additions & 12 deletions dotnet/samples/Concepts/Agents/ChatCompletion_Streaming.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.
using System.ComponentModel;
using System.Text;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.ChatCompletion;
Expand All @@ -9,8 +8,7 @@
namespace Agents;

/// <summary>
/// Demonstrate creation of <see cref="ChatCompletionAgent"/> and
/// eliciting its response to three explicit user messages.
/// Demonstrate consuming "streaming" message for <see cref="ChatCompletionAgent"/>.
/// </summary>
public class ChatCompletion_Streaming(ITestOutputHelper output) : BaseAgentsTest(output)
{
Expand All @@ -35,6 +33,9 @@ public async Task UseStreamingChatCompletionAgentAsync()
await InvokeAgentAsync(agent, chat, "Fortune favors the bold.");
await InvokeAgentAsync(agent, chat, "I came, I saw, I conquered.");
await InvokeAgentAsync(agent, chat, "Practice makes perfect.");

// Output the entire chat history
DisplayChatHistory(chat);
}

[Fact]
Expand All @@ -61,6 +62,9 @@ public async Task UseStreamingChatCompletionAgentWithPluginAsync()
// Respond to user input
await InvokeAgentAsync(agent, chat, "What is the special soup?");
await InvokeAgentAsync(agent, chat, "What is the special drink?");

// Output the entire chat history
DisplayChatHistory(chat);
}

// Local function to invoke agent and display the conversation messages.
Expand All @@ -70,29 +74,44 @@ private async Task InvokeAgentAsync(ChatCompletionAgent agent, ChatHistory chat,
chat.Add(message);
this.WriteAgentChatMessage(message);

StringBuilder builder = new();
int historyCount = chat.Count;

bool isFirst = false;
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(chat))
{
if (string.IsNullOrEmpty(response.Content))
{
continue;
}

if (builder.Length == 0)
if (!isFirst)
{
Console.WriteLine($"# {response.Role} - {response.AuthorName ?? "*"}:");
Console.WriteLine($"\n# {response.Role} - {response.AuthorName ?? "*"}:");
isFirst = true;
}

Console.WriteLine($"\t > streamed: '{response.Content}'");
builder.Append(response.Content);
}

if (builder.Length > 0)
if (historyCount <= chat.Count)
{
for (int index = historyCount; index < chat.Count; index++)
{
this.WriteAgentChatMessage(chat[index]);
}
}
}

private void DisplayChatHistory(ChatHistory history)
{
// Display the chat history.
Console.WriteLine("================================");
Console.WriteLine("CHAT HISTORY");
Console.WriteLine("================================");

foreach (ChatMessageContent message in history)
{
// Display full response and capture in chat history
ChatMessageContent response = new(AuthorRole.Assistant, builder.ToString()) { AuthorName = agent.Name };
chat.Add(response);
this.WriteAgentChatMessage(response);
this.WriteAgentChatMessage(message);
}
}

Expand Down
122 changes: 122 additions & 0 deletions dotnet/samples/Concepts/Agents/MixedChat_Streaming.cs
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 dotnet/samples/Concepts/Agents/OpenAIAssistant_Streaming.cs
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";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task UseDependencyInjectionToCreateAgentAsync()
AgentClient agentClient = serviceProvider.GetRequiredService<AgentClient>();

// Execute the agent-client
await WriteAgentResponse("The sunset is very colorful.");
await WriteAgentResponse("The sunset is nice.");
await WriteAgentResponse("The sunset is setting over the mountains.");
await WriteAgentResponse("The sunset is setting over the mountains and filled the sky with a deep red flame, setting the clouds ablaze.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ await agent.CreateThreadAsync(
finally
{
await agent.DeleteThreadAsync(threadId);
await agent.DeleteAsync(CancellationToken.None);
await agent.DeleteAsync();
await vectorStoreClient.DeleteVectorStoreAsync(vectorStore);
await fileClient.DeleteFileAsync(fileInfo.Id);
}
Expand Down
Loading

0 comments on commit 6426911

Please sign in to comment.