Skip to content

Commit c13bbb9

Browse files
Merge branch 'microsoft:main' into main
2 parents 2f52bd0 + 38d891e commit c13bbb9

File tree

45 files changed

+1895
-183
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1895
-183
lines changed

dotnet/samples/Concepts/Agents/AzureAIAgent_Streaming.cs

+24-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel;
33
using Azure.AI.Projects;
44
using Microsoft.SemanticKernel;
5+
using Microsoft.SemanticKernel.Agents;
56
using Microsoft.SemanticKernel.Agents.AzureAI;
67
using Microsoft.SemanticKernel.ChatCompletion;
78
using Agent = Azure.AI.Projects.Agent;
@@ -28,15 +29,15 @@ public async Task UseStreamingAgentAsync()
2829
AzureAIAgent agent = new(definition, this.AgentsClient);
2930

3031
// Create a thread for the agent conversation.
31-
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
32+
AzureAIAgentThread agentThread = new(this.AgentsClient, metadata: SampleMetadata);
3233

3334
// Respond to user input
34-
await InvokeAgentAsync(agent, thread.Id, "Fortune favors the bold.");
35-
await InvokeAgentAsync(agent, thread.Id, "I came, I saw, I conquered.");
36-
await InvokeAgentAsync(agent, thread.Id, "Practice makes perfect.");
35+
await InvokeAgentAsync(agent, agentThread, "Fortune favors the bold.");
36+
await InvokeAgentAsync(agent, agentThread, "I came, I saw, I conquered.");
37+
await InvokeAgentAsync(agent, agentThread, "Practice makes perfect.");
3738

3839
// Output the entire chat history
39-
await DisplayChatHistoryAsync(agent, thread.Id);
40+
await DisplayChatHistoryAsync(agentThread);
4041
}
4142

4243
[Fact]
@@ -58,14 +59,14 @@ public async Task UseStreamingAssistantAgentWithPluginAsync()
5859
agent.Kernel.Plugins.Add(plugin);
5960

6061
// Create a thread for the agent conversation.
61-
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
62+
AzureAIAgentThread agentThread = new(this.AgentsClient, metadata: SampleMetadata);
6263

6364
// Respond to user input
64-
await InvokeAgentAsync(agent, thread.Id, "What is the special soup and its price?");
65-
await InvokeAgentAsync(agent, thread.Id, "What is the special drink and its price?");
65+
await InvokeAgentAsync(agent, agentThread, "What is the special soup and its price?");
66+
await InvokeAgentAsync(agent, agentThread, "What is the special drink and its price?");
6667

6768
// Output the entire chat history
68-
await DisplayChatHistoryAsync(agent, thread.Id);
69+
await DisplayChatHistoryAsync(agentThread);
6970
}
7071

7172
[Fact]
@@ -84,28 +85,33 @@ public async Task UseStreamingAssistantWithCodeInterpreterAsync()
8485
AzureAIAgent agent = new(definition, this.AgentsClient);
8586

8687
// Create a thread for the agent conversation.
87-
AgentThread thread = await this.AgentsClient.CreateThreadAsync(metadata: SampleMetadata);
88+
AzureAIAgentThread agentThread = new(this.AgentsClient, metadata: SampleMetadata);
8889

8990
// Respond to user input
90-
await InvokeAgentAsync(agent, thread.Id, "Is 191 a prime number?");
91-
await InvokeAgentAsync(agent, thread.Id, "Determine the values in the Fibonacci sequence that that are less then the value of 101");
91+
await InvokeAgentAsync(agent, agentThread, "Is 191 a prime number?");
92+
await InvokeAgentAsync(agent, agentThread, "Determine the values in the Fibonacci sequence that that are less then the value of 101");
9293

9394
// Output the entire chat history
94-
await DisplayChatHistoryAsync(agent, thread.Id);
95+
await DisplayChatHistoryAsync(agentThread);
9596
}
9697

9798
// Local function to invoke agent and display the conversation messages.
98-
private async Task InvokeAgentAsync(AzureAIAgent agent, string threadId, string input)
99+
private async Task InvokeAgentAsync(AzureAIAgent agent, Microsoft.SemanticKernel.Agents.AgentThread agentThread, string input)
99100
{
100101
ChatMessageContent message = new(AuthorRole.User, input);
101-
await agent.AddChatMessageAsync(threadId, message);
102102
this.WriteAgentChatMessage(message);
103103

104+
// For this sample, also capture fully formed messages so we can display them later.
104105
ChatHistory history = [];
106+
Task OnNewMessage(ChatMessageContent message)
107+
{
108+
history.Add(message);
109+
return Task.CompletedTask;
110+
}
105111

106112
bool isFirst = false;
107113
bool isCode = false;
108-
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(threadId, messages: history))
114+
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(message, agentThread, new AgentInvokeOptions() { OnNewMessage = OnNewMessage }))
109115
{
110116
if (string.IsNullOrEmpty(response.Content))
111117
{
@@ -140,13 +146,13 @@ private async Task InvokeAgentAsync(AzureAIAgent agent, string threadId, string
140146
}
141147
}
142148

143-
private async Task DisplayChatHistoryAsync(AzureAIAgent agent, string threadId)
149+
private async Task DisplayChatHistoryAsync(AzureAIAgentThread agentThread)
144150
{
145151
Console.WriteLine("================================");
146152
Console.WriteLine("CHAT HISTORY");
147153
Console.WriteLine("================================");
148154

149-
ChatMessageContent[] messages = await agent.GetThreadMessagesAsync(threadId).ToArrayAsync();
155+
ChatMessageContent[] messages = await agentThread.GetMessagesAsync().ToArrayAsync();
150156
for (int index = messages.Length - 1; index >= 0; --index)
151157
{
152158
this.WriteAgentChatMessage(messages[index]);

dotnet/samples/Concepts/Agents/OpenAIAssistant_Streaming.cs

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
using System.ComponentModel;
33
using Microsoft.SemanticKernel;
4+
using Microsoft.SemanticKernel.Agents;
45
using Microsoft.SemanticKernel.Agents.OpenAI;
56
using Microsoft.SemanticKernel.ChatCompletion;
67
using OpenAI.Assistants;
@@ -27,15 +28,15 @@ await this.AssistantClient.CreateAssistantAsync(
2728
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);
2829

2930
// Create a thread for the agent conversation.
30-
string threadId = await this.AssistantClient.CreateThreadAsync(metadata: SampleMetadata);
31+
OpenAIAssistantAgentThread agentThread = new(this.AssistantClient, metadata: SampleMetadata);
3132

3233
// Respond to user input
33-
await InvokeAgentAsync(agent, threadId, "Fortune favors the bold.");
34-
await InvokeAgentAsync(agent, threadId, "I came, I saw, I conquered.");
35-
await InvokeAgentAsync(agent, threadId, "Practice makes perfect.");
34+
await InvokeAgentAsync(agent, agentThread, "Fortune favors the bold.");
35+
await InvokeAgentAsync(agent, agentThread, "I came, I saw, I conquered.");
36+
await InvokeAgentAsync(agent, agentThread, "Practice makes perfect.");
3637

3738
// Output the entire chat history
38-
await DisplayChatHistoryAsync(agent, threadId);
39+
await DisplayChatHistoryAsync(agentThread);
3940
}
4041

4142
[Fact]
@@ -54,14 +55,14 @@ await this.AssistantClient.CreateAssistantAsync(
5455
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient, [plugin]);
5556

5657
// Create a thread for the agent conversation.
57-
string threadId = await this.AssistantClient.CreateThreadAsync(metadata: SampleMetadata);
58+
OpenAIAssistantAgentThread agentThread = new(this.AssistantClient, metadata: SampleMetadata);
5859

5960
// Respond to user input
60-
await InvokeAgentAsync(agent, threadId, "What is the special soup and its price?");
61-
await InvokeAgentAsync(agent, threadId, "What is the special drink and its price?");
61+
await InvokeAgentAsync(agent, agentThread, "What is the special soup and its price?");
62+
await InvokeAgentAsync(agent, agentThread, "What is the special drink and its price?");
6263

6364
// Output the entire chat history
64-
await DisplayChatHistoryAsync(agent, threadId);
65+
await DisplayChatHistoryAsync(agentThread);
6566
}
6667

6768
[Fact]
@@ -80,28 +81,33 @@ await this.AssistantClient.CreateAssistantAsync(
8081
OpenAIAssistantAgent agent = new(assistant, this.AssistantClient);
8182

8283
// Create a thread for the agent conversation.
83-
string threadId = await this.AssistantClient.CreateThreadAsync(metadata: SampleMetadata);
84+
OpenAIAssistantAgentThread agentThread = new(this.AssistantClient, metadata: SampleMetadata);
8485

8586
// Respond to user input
86-
await InvokeAgentAsync(agent, threadId, "Is 191 a prime number?");
87-
await InvokeAgentAsync(agent, threadId, "Determine the values in the Fibonacci sequence that that are less then the value of 101");
87+
await InvokeAgentAsync(agent, agentThread, "Is 191 a prime number?");
88+
await InvokeAgentAsync(agent, agentThread, "Determine the values in the Fibonacci sequence that that are less then the value of 101");
8889

8990
// Output the entire chat history
90-
await DisplayChatHistoryAsync(agent, threadId);
91+
await DisplayChatHistoryAsync(agentThread);
9192
}
9293

9394
// Local function to invoke agent and display the conversation messages.
94-
private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, string threadId, string input)
95+
private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, AgentThread agentThread, string input)
9596
{
9697
ChatMessageContent message = new(AuthorRole.User, input);
97-
await agent.AddChatMessageAsync(threadId, message);
9898
this.WriteAgentChatMessage(message);
9999

100+
// For this sample, also capture fully formed messages so we can display them later.
100101
ChatHistory history = [];
102+
Task OnNewMessage(ChatMessageContent message)
103+
{
104+
history.Add(message);
105+
return Task.CompletedTask;
106+
}
101107

102108
bool isFirst = false;
103109
bool isCode = false;
104-
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(threadId, messages: history))
110+
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(message, agentThread, new() { OnNewMessage = OnNewMessage }))
105111
{
106112
if (string.IsNullOrEmpty(response.Content))
107113
{
@@ -136,13 +142,13 @@ private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, string threadId,
136142
}
137143
}
138144

139-
private async Task DisplayChatHistoryAsync(OpenAIAssistantAgent agent, string threadId)
145+
private async Task DisplayChatHistoryAsync(OpenAIAssistantAgentThread agentThread)
140146
{
141147
Console.WriteLine("================================");
142148
Console.WriteLine("CHAT HISTORY");
143149
Console.WriteLine("================================");
144150

145-
ChatMessageContent[] messages = await agent.GetThreadMessagesAsync(threadId).ToArrayAsync();
151+
ChatMessageContent[] messages = await agentThread.GetMessagesAsync().ToArrayAsync();
146152
for (int index = messages.Length - 1; index >= 0; --index)
147153
{
148154
this.WriteAgentChatMessage(messages[index]);

dotnet/samples/Demos/AgentFrameworkWithAspire/ChatWithAgent.ApiService/Controllers/AgentCompletionsController.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private async IAsyncEnumerable<ChatMessageContent> CompleteAsync(ChatHistory cha
7171
{
7272
var thread = new ChatHistoryAgentThread(chatHistory);
7373
IAsyncEnumerable<AgentResponseItem<ChatMessageContent>> content =
74-
this._agent.InvokeAsync([], thread, options: new() { KernelArguments = arguments }, cancellationToken: cancellationToken);
74+
this._agent.InvokeAsync(thread, options: new() { KernelArguments = arguments }, cancellationToken: cancellationToken);
7575

7676
await foreach (ChatMessageContent item in content.ConfigureAwait(false))
7777
{
@@ -90,7 +90,7 @@ private async IAsyncEnumerable<StreamingChatMessageContent> CompleteSteamingAsyn
9090
{
9191
var thread = new ChatHistoryAgentThread(chatHistory);
9292
IAsyncEnumerable<AgentResponseItem<StreamingChatMessageContent>> content =
93-
this._agent.InvokeStreamingAsync([], thread, options: new() { KernelArguments = arguments }, cancellationToken: cancellationToken);
93+
this._agent.InvokeStreamingAsync(thread, options: new() { KernelArguments = arguments }, cancellationToken: cancellationToken);
9494

9595
await foreach (StreamingChatMessageContent item in content.ConfigureAwait(false))
9696
{

dotnet/samples/GettingStartedWithAgents/AzureAIAgent/Step01_AzureAIAgent.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ await InvokeAgentAsync(
5858
// Local function to invoke agent and display the response.
5959
async Task InvokeAgentAsync(KernelArguments? arguments = null)
6060
{
61-
await foreach (ChatMessageContent response in agent.InvokeAsync([], thread, new() { KernelArguments = arguments }))
61+
await foreach (ChatMessageContent response in agent.InvokeAsync(thread, new() { KernelArguments = arguments }))
6262
{
6363
WriteAgentChatMessage(response);
6464
}

dotnet/samples/GettingStartedWithAgents/OpenAIAssistant/Step01_Assistant.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ await InvokeAgentAsync(
5858
// Local function to invoke agent and display the response.
5959
async Task InvokeAgentAsync(KernelArguments? arguments = null)
6060
{
61-
await foreach (ChatMessageContent response in agent.InvokeAsync([], thread, options: new() { KernelArguments = arguments }))
61+
await foreach (ChatMessageContent response in agent.InvokeAsync(thread, options: new() { KernelArguments = arguments }))
6262
{
6363
WriteAgentChatMessage(response);
6464
}

dotnet/samples/GettingStartedWithAgents/Step01_Agent.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ await InvokeAgentAsync(
173173
async Task InvokeAgentAsync(KernelArguments? arguments = null)
174174
{
175175
// Invoke the agent without any messages, since the agent has all that it needs via the template and arguments.
176-
await foreach (ChatMessageContent content in agent.InvokeAsync([], options: new() { KernelArguments = arguments }))
176+
await foreach (ChatMessageContent content in agent.InvokeAsync(options: new() { KernelArguments = arguments }))
177177
{
178178
WriteAgentChatMessage(content);
179179
}

0 commit comments

Comments
 (0)