Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net Agents - Add Streaming support for OpenAIAssistantAgent and AgentChat #8175

Merged
merged 300 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
300 commits
Select commit Hold shift + click to select a range
9e9f364
Typo
crickman Jul 7, 2024
7b21ee8
.Net: Migrate AzureOpenAIAudioToTextService to Azure.AI.OpenAI SDK v2…
SergeyMenshykh Jul 8, 2024
6fd7c7f
Exclude V1 sample
crickman Jul 8, 2024
d230cce
.Net: OpenAI V2 - Audio to Text - Response Format as Enum conversion …
RogerBarreto Jul 8, 2024
9238604
.Net: AzureOpenAI services cleanup (#7140)
SergeyMenshykh Jul 8, 2024
ec0aa8c
Project clean-up
crickman Jul 8, 2024
13a9318
.Net: Copy OpenAI file service to the Connectors.AzureOpenAI project …
SergeyMenshykh Jul 8, 2024
59d632b
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Jul 8, 2024
b2c1726
Dependency clean-up: IntegrationTests
crickman Jul 8, 2024
7e4cd39
Fix path case for unbuntu
crickman Jul 8, 2024
e0a33c0
Comments and orgid clean-up
crickman Jul 8, 2024
a042a85
Comments
crickman Jul 8, 2024
3081083
More clean-up
crickman Jul 8, 2024
d3b5528
Coverage
crickman Jul 8, 2024
0ae6739
Typo
crickman Jul 8, 2024
18f6ad2
More test coverage
crickman Jul 8, 2024
2f459f0
More coverage
crickman Jul 8, 2024
4d2914b
Partial arc optimization
crickman Jul 8, 2024
f5b9bdc
.Net: OpenAI V2 Connector - ChatCompletion + FC - Phase 06 (#7138)
RogerBarreto Jul 9, 2024
f7e7e29
.Net: OpenAI V2 - FileService Obsolescence (#7184)
RogerBarreto Jul 10, 2024
3325773
.Net: OpenAI V2 - Reverting all avoidable Breaking Changes - Phase 08…
RogerBarreto Jul 11, 2024
bd4dde0
.Net: Remove AzureOpenAIFileService (#7195)
SergeyMenshykh Jul 11, 2024
64120d3
.Net: OpenAI V2 Removing LogActivity Extra Implementation (#7205)
RogerBarreto Jul 11, 2024
a10e9f2
.Net: Align metadata names with underlying library ones (#7207)
SergeyMenshykh Jul 11, 2024
5dadd59
Merge branch 'main' into feature-connectors-openai
crickman Jul 12, 2024
89c2096
Merge branch 'main' into feature-connectors-openai
crickman Jul 12, 2024
80c8518
Merge branch 'feature-connectors-openai' of https://github.com/micros…
crickman Jul 12, 2024
ec7129f
Resolve merge from main
crickman Jul 12, 2024
f0b2757
.Net: Remove time stamp granularities (#7214)
SergeyMenshykh Jul 12, 2024
49ff10f
.Net: Rollback unnecessary breaking change (#7222)
SergeyMenshykh Jul 12, 2024
a3145a2
.Net: Preparing grounds for Concepts OpenAI V2 migration (#7229)
RogerBarreto Jul 12, 2024
5b30e33
.Net: Remove unnecessary breaking changes (#7235)
SergeyMenshykh Jul 12, 2024
5eba985
Resolve merge from parent
crickman Jul 12, 2024
66a9f24
Merge branch 'feature-connectors-openai' of https://github.com/micros…
crickman Jul 12, 2024
9e46068
Resolve merge from main
crickman Jul 12, 2024
696652d
Resolve breaking changes (first pass)
crickman Jul 12, 2024
bb98e44
Remove ConceptsV2 shim
crickman Jul 12, 2024
4992cd3
Namespace
crickman Jul 12, 2024
daed2f8
Whitespace
crickman Jul 12, 2024
6bd518c
Resolve OpenAIV2 dependency
crickman Jul 12, 2024
51f80c5
Remove file-service usage
crickman Jul 12, 2024
9e7e2dc
Fix concepts resources
crickman Jul 12, 2024
37363fd
Fix sample migration
crickman Jul 12, 2024
1c986cf
Restore skipped samples
crickman Jul 12, 2024
448af97
/sigh
crickman Jul 12, 2024
9fae258
.Net Remove Azure* redundant function calling classes (#7236)
SergeyMenshykh Jul 15, 2024
3851576
.Net: Remove unnecessary azure chat message content classes (#7259)
SergeyMenshykh Jul 15, 2024
4c6b99b
.Net: Minimize *prompt execution settings duplication (#7265)
SergeyMenshykh Jul 16, 2024
44f27a2
.Net: Cleanup (#7266)
SergeyMenshykh Jul 16, 2024
c425b78
.Net: OpenAI V2 - Concepts Migration - Phase 2.0 (#7233)
RogerBarreto Jul 16, 2024
f356b9d
.Net: Chat history serialization test + bug fix (#7305)
SergeyMenshykh Jul 17, 2024
fd79d99
Merge branch 'feature-connectors-openai' of https://github.com/micros…
crickman Jul 17, 2024
6f7b89b
Resolve merge from parent
crickman Jul 17, 2024
c92f87f
Fix merge
crickman Jul 17, 2024
7f98d4f
Resolve merge from main
crickman Jul 17, 2024
8797fc9
Fix merge (exclude new concept sample and demo)
crickman Jul 17, 2024
1a918f8
Resolve merge from parent
crickman Jul 17, 2024
9205cec
Fix build
crickman Jul 17, 2024
712c826
Fix merge
crickman Jul 17, 2024
3b8e54f
.Net: Refactor samples to use new {Azure}OpenAI connectors (#7334)
SergeyMenshykh Jul 17, 2024
d436430
.Net: Test execution settings compatibility (#7337)
SergeyMenshykh Jul 18, 2024
c03cc7f
.Net: Migrate remaining samples to new {Azure}OpenAI services (#7353)
SergeyMenshykh Jul 18, 2024
362e187
Resolve merge from parent
crickman Jul 18, 2024
974dc99
.Net: OpenAI V2 - Demos Migration (#7384)
RogerBarreto Jul 22, 2024
58d01e7
Definition property update
crickman Jul 22, 2024
22946a8
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Jul 22, 2024
080d21e
UT update
crickman Jul 22, 2024
31e3051
More UT
crickman Jul 22, 2024
a33dc5e
Rename
crickman Jul 22, 2024
84c0a1f
Rename and UT stubs
crickman Jul 22, 2024
e09013b
Namespace
crickman Jul 22, 2024
a48aa12
Checkpoint
crickman Jul 22, 2024
991dd59
Blank line
crickman Jul 22, 2024
6bb0613
Checkpoint
crickman Jul 22, 2024
1709575
Checkpoint
crickman Jul 23, 2024
f6e8c7b
More testing
crickman Jul 23, 2024
117066c
Clean-up
crickman Jul 23, 2024
6044fbb
Merge branch 'main' into feature-connectors-openai
crickman Jul 23, 2024
05b48b6
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Jul 23, 2024
dd0e4fa
Merge branch 'main' into feature-connectors-openai
RogerBarreto Jul 23, 2024
ecd3fee
.Net: OpenAI V2 Optional Settings (#7409)
RogerBarreto Jul 23, 2024
10c4afb
Sync with bug-fix
crickman Jul 23, 2024
89773be
Merge branch 'main' into feature-connectors-openai
RogerBarreto Jul 23, 2024
497f225
.Net: Net: OpenAI v2 Reusability (#7427)
RogerBarreto Jul 24, 2024
3396076
Checkpoint
crickman Jul 24, 2024
2f4785e
Merge branch 'main' into feature-connectors-openai
crickman Jul 24, 2024
499fbbd
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Jul 24, 2024
511e6ca
Test Complete
crickman Jul 24, 2024
8ca0a9d
More tests + typo fix
crickman Jul 25, 2024
bd56cd9
Blank line
crickman Jul 25, 2024
51ffe3a
Typo
crickman Jul 25, 2024
3117d3c
.Net: OpenAI V2 Migration - Decomission V1 Phase 01 (#7446)
RogerBarreto Jul 25, 2024
61328ec
Merge branch 'main' into feature-connectors-openai
crickman Jul 25, 2024
d0509b3
Resolve merge from parent
crickman Jul 25, 2024
ef92c71
Update based on merge and PR input
crickman Jul 25, 2024
12925ab
Merge branch 'main' into feature-connectors-openai
crickman Jul 25, 2024
21a905f
Merge new agent samples
crickman Jul 25, 2024
a4567b7
Resolve merge from parent
crickman Jul 25, 2024
afda061
Migrate new samples
crickman Jul 25, 2024
ea1fa92
Legacy fix
crickman Jul 25, 2024
259b769
Exception message
crickman Jul 25, 2024
6c6bc5c
.Net: OpenAI V2 IntegrationTests Merge - Phase 02 (#7453)
RogerBarreto Jul 25, 2024
91d5142
Resolve merge from parent
crickman Jul 25, 2024
20c7057
Clean-up
crickman Jul 25, 2024
718505f
.Net: OpenAI V2 -> OpenAI Renaming - Phase 03 (#7454)
RogerBarreto Jul 26, 2024
8f26ee7
Merge branch 'main' into feature-connectors-openai
RogerBarreto Jul 26, 2024
799cf63
Resolve merge from parent
crickman Jul 26, 2024
93bfab4
.Net: OpenAI V2 Migration - Apply recommendations (#7471)
RogerBarreto Jul 27, 2024
8dbba0f
Merge branch 'main' into feature-connectors-openai
crickman Jul 29, 2024
f83751a
Resolve merge from parent
crickman Jul 29, 2024
4135c51
Merge branch 'main' into feature-connectors-openai
RogerBarreto Jul 29, 2024
13a88e9
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Jul 30, 2024
719cce3
.Net: OpenAI V2 Migration - Small fixes (#7532)
RogerBarreto Jul 31, 2024
f0c97df
Merge branch 'main' into feature-connectors-openai
crickman Jul 31, 2024
da75903
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Jul 31, 2024
f419ac2
Clean-up
crickman Jul 31, 2024
cd24a45
Legacy clean-up
crickman Jul 31, 2024
b4dfd7a
Type fix
crickman Jul 31, 2024
e2d3408
Remove friend relationship to concepts
crickman Jul 31, 2024
d68b567
Update CharMaker sample to download and view images
crickman Jul 31, 2024
475a9f6
Namespace
crickman Jul 31, 2024
844cef9
Param comment
crickman Jul 31, 2024
6aa0d26
Merge branch 'main' into feature-connectors-openai
crickman Aug 1, 2024
d5ab830
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Aug 1, 2024
dac78fd
PR comments
crickman Aug 1, 2024
03f2456
Simplify
crickman Aug 1, 2024
197a7e3
Update dotnet/src/Agents/UnitTests/OpenAI/Internal/AssistantMessageFa…
crickman Aug 2, 2024
a184818
Update dotnet/src/Agents/OpenAI/Extensions/OpenAIServiceConfiguration…
crickman Aug 2, 2024
042ab64
Cosmetic: Maximize single line code complexity and eliminate potentia…
crickman Aug 2, 2024
2a4baec
Namespace
crickman Aug 2, 2024
ae47cbf
Remove silent failure
crickman Aug 2, 2024
bd55f03
Braces
crickman Aug 2, 2024
f3c8af1
Fix logic level
crickman Aug 2, 2024
a7e8c94
Merge branch 'main' into feature-connectors-openai
crickman Aug 5, 2024
b78806a
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Aug 5, 2024
0c11eb3
Port update
crickman Aug 5, 2024
c31269f
Merge branch 'main' into feature-connectors-openai
crickman Aug 5, 2024
db0e752
Resolve merge from parent
crickman Aug 5, 2024
65bfdf8
Merge branch 'main' into feature-connectors-openai
RogerBarreto Aug 6, 2024
8faccc8
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Aug 6, 2024
d492d84
.Net: [Feature branch] Added release candidate suffix for production …
dmytrostruk Aug 6, 2024
9d573b5
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Aug 6, 2024
406c3d9
Checkpoint
crickman Aug 6, 2024
9fd6b92
Unit-tests
crickman Aug 6, 2024
5b54de4
Serialization tests and attributes
crickman Aug 7, 2024
d029bd4
Image DataUrl support
crickman Aug 7, 2024
a7cdba5
Remove duplicate sample
crickman Aug 7, 2024
13f1986
Update concept samples
crickman Aug 7, 2024
7dc67fd
Whitespace
crickman Aug 7, 2024
e74aa19
Namespace
crickman Aug 7, 2024
092992b
Merge branch 'main' of https://github.com/microsoft/semantic-kernel i…
RogerBarreto Aug 7, 2024
84aece3
Fix Azure namespace
RogerBarreto Aug 7, 2024
77fefb9
Fix namespace order
RogerBarreto Aug 7, 2024
41482ba
"Breaking Glass" Checkpoint
crickman Aug 7, 2024
a372a67
Comments
crickman Aug 7, 2024
2a6227e
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Aug 7, 2024
bdaa4b6
One more comment
crickman Aug 7, 2024
2b138e5
Include agent-base-test in solution
crickman Aug 7, 2024
6855dbb
Re-link samples/internalutilities to .sln
crickman Aug 7, 2024
f4d2113
Re-add demo project to sln (merge weirdness?)
crickman Aug 7, 2024
eeef023
Merge branch 'main' into feature-connectors-openai
crickman Aug 7, 2024
4fe9a1f
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Aug 7, 2024
e8ace92
.Net: Allow chat history mutation from auto-function invocation filte…
SergeyMenshykh Aug 8, 2024
a18953f
.Net: Enable code coverage for OpenAi connectors (#7970)
SergeyMenshykh Aug 8, 2024
64c4932
Update package versions
crickman Aug 8, 2024
196cadd
Update base package version
crickman Aug 8, 2024
85d803e
Merge branch 'feature-connectors-openai' into feature_agents_openai
crickman Aug 8, 2024
0d082e1
Fix channel-keys generation
crickman Aug 8, 2024
b8f1d02
Allocate key-enumeration
crickman Aug 8, 2024
73319b1
Merge branch 'main' into feature-connectors-openai
crickman Aug 8, 2024
a1d3e4f
Merge to all recent agent updates
crickman Aug 8, 2024
fe3b835
Final fixes
crickman Aug 8, 2024
7cdfba6
Test enhancement
crickman Aug 8, 2024
5c62656
Up test coverage
crickman Aug 8, 2024
5077465
Typo
crickman Aug 8, 2024
bf3825e
Namespace + core coverage
crickman Aug 8, 2024
590fbf3
Service-selection coverage
crickman Aug 8, 2024
e46a93c
Even deeper coverage
crickman Aug 8, 2024
8be28e1
.Net: OpenAI V2 - Small fix (#8015)
RogerBarreto Aug 9, 2024
9c19d59
Update dotnet/src/Agents/OpenAI/OpenAIAssistantAgent.cs
crickman Aug 9, 2024
176f372
Update dotnet/src/Agents/OpenAI/OpenAIAssistantExecutionOptions.cs
crickman Aug 9, 2024
71bd060
Update dotnet/src/Agents/OpenAI/OpenAIAssistantExecutionOptions.cs
crickman Aug 9, 2024
a24d151
Update dotnet/src/Agents/UnitTests/OpenAI/Internal/AssistantMessageFa…
crickman Aug 9, 2024
fd0d81e
Update dotnet/src/Agents/UnitTests/OpenAI/Internal/AssistantMessageFa…
crickman Aug 9, 2024
7162e34
Update dotnet/src/Agents/OpenAI/OpenAIAssistantAgent.cs
crickman Aug 9, 2024
96c3ac0
Update dotnet/src/Agents/OpenAI/Internal/AssistantThreadActions.cs
crickman Aug 9, 2024
58ce240
Update dotnet/src/Agents/OpenAI/Internal/AssistantThreadActions.cs
crickman Aug 9, 2024
56f0fea
Indenting
crickman Aug 9, 2024
f80ec5d
Merge branch 'feature_agents_openai' of https://github.com/microsoft/…
crickman Aug 9, 2024
c45c1c8
Update dotnet/src/Agents/OpenAI/Internal/AssistantThreadActions.cs
crickman Aug 9, 2024
b0b35e7
Updated based on comments
crickman Aug 9, 2024
ff40c20
Merge branch 'feature_agents_openai' of https://github.com/microsoft/…
crickman Aug 9, 2024
631eded
Update unit-test comments
crickman Aug 9, 2024
5463d1b
Summary reducer optimization
crickman Aug 9, 2024
2cfadc7
Checkpoint
crickman Aug 10, 2024
9e59698
.Net Agents - Assistant V2 Migration (#7126)
crickman Aug 12, 2024
45169b9
.Net: AzureOpenAI - Enable package validation (#8097)
RogerBarreto Aug 13, 2024
e4ad4e7
Feature Complete
crickman Aug 14, 2024
4c97f2b
Typo
crickman Aug 14, 2024
56eed66
namespace
crickman Aug 14, 2024
ba93fe1
Namespace
crickman Aug 14, 2024
d62b0ea
Namespace
crickman Aug 14, 2024
019b2cb
Whitespace
crickman Aug 14, 2024
2bf6ccd
Suppress
crickman Aug 14, 2024
7803a0f
Test coverage
crickman Aug 14, 2024
1ca5373
One more sample
crickman Aug 15, 2024
76fdb4c
Namespace
crickman Aug 15, 2024
f673b55
Merge branch 'main' into feature-connectors-openai
RogerBarreto Aug 19, 2024
86e1df6
.Net: OpenAI V2 - Prompty UT Fix (#8277)
RogerBarreto Aug 19, 2024
ac038d2
Resolve sync with main
crickman Aug 19, 2024
3b95272
Merge branch 'main' into feature-connectors-openai
crickman Aug 19, 2024
5774c73
Sync new sample
crickman Aug 19, 2024
c93a56a
Namespace in sample
crickman Aug 19, 2024
5f44859
Resolve merge from parent
crickman Aug 19, 2024
c262d99
Resolve merge from main
crickman Aug 20, 2024
3458110
Resolve merge from parent
crickman Aug 21, 2024
47e6f94
Resolve merge from main
crickman Sep 4, 2024
c80aa0f
Checkpoint
crickman Sep 4, 2024
d00a322
Merge branch 'main' into feature_agents_streaming
crickman Sep 4, 2024
21082e4
Merge branch 'main' into feature_agents_streaming
crickman Sep 4, 2024
230fc79
Checkpoint
crickman Sep 4, 2024
7fa17ac
Merge branch 'feature_agents_streaming' of https://github.com/microso…
crickman Sep 4, 2024
2b4e0b8
Tune samples
crickman Sep 4, 2024
9cb5ecf
Namespace
crickman Sep 4, 2024
f4c5609
whitespace
crickman Sep 4, 2024
9d7ef22
namespace
crickman Sep 4, 2024
534ad3d
Merge branch 'main' into feature_agents_streaming
crickman Sep 4, 2024
445c840
Merge branch 'main' into feature_agents_streaming
crickman Sep 5, 2024
d6bf539
Remove demo
crickman Sep 5, 2024
d58bfa1
Merge branch 'main' into feature_agents_streaming
crickman Sep 5, 2024
9708058
Merge branch 'main' into feature_agents_streaming
crickman Sep 5, 2024
57850e2
Update dotnet/src/Agents/Abstractions/Logging/AgentChatLogMessages.cs
crickman Sep 5, 2024
995ace9
Update dotnet/src/SemanticKernel.Abstractions/Contents/StreamingAnnot…
crickman Sep 5, 2024
647f7c3
Update dotnet/src/SemanticKernel.Abstractions/Contents/StreamingFileR…
crickman Sep 5, 2024
3c4e516
Update dotnet/src/SemanticKernel.Abstractions/Contents/StreamingFileR…
crickman Sep 5, 2024
9ae1dc6
Update dotnet/src/Agents/OpenAI/Internal/AssistantThreadActions.cs
crickman Sep 5, 2024
ab8f9c1
Update dotnet/src/SemanticKernel.UnitTests/Contents/StreamingAnnotati…
crickman Sep 5, 2024
87a8fa6
Update dotnet/src/SemanticKernel.UnitTests/Contents/StreamingFileRefe…
crickman Sep 5, 2024
f29cb68
Update dotnet/src/Agents/Core/AgentGroupChat.cs
crickman Sep 5, 2024
6473623
Sample comments
crickman Sep 5, 2024
afe87ae
Updated from PR comments
crickman Sep 5, 2024
9adf479
Whitespace
crickman Sep 5, 2024
e71e2ea
Update dotnet/src/SemanticKernel.Abstractions/Contents/StreamingAnnot…
crickman Sep 5, 2024
8967a1e
Update dotnet/src/Agents/OpenAI/Internal/AssistantThreadActions.cs
crickman Sep 5, 2024
a6e6865
Update dotnet/src/Agents/OpenAI/Internal/AssistantThreadActions.cs
crickman Sep 5, 2024
9ca049a
Whitespace
crickman Sep 5, 2024
39579d3
Merge branch 'main' into feature_agents_streaming
crickman Sep 6, 2024
fdb9682
Merge branch 'main' into feature_agents_streaming
crickman Sep 6, 2024
adb7845
Merge branch 'main' into feature_agents_streaming
crickman Sep 9, 2024
2124391
Merge branch 'main' into feature_agents_streaming
crickman Sep 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
crickman marked this conversation as resolved.
Show resolved Hide resolved
{
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
Loading