From 6b90cde99b337a023d7491dd372928db1f051129 Mon Sep 17 00:00:00 2001 From: Chris Rickman Date: Wed, 10 Jul 2024 18:27:05 -0700 Subject: [PATCH 1/4] Updated --- .../Concepts/Agents/ComplexChat_NestedShopper.cs | 4 ++-- dotnet/samples/Concepts/Agents/MixedChat_Agents.cs | 6 +++--- .../Concepts/Agents/OpenAIAssistant_ChartMaker.cs | 10 +++++++--- .../Concepts/Agents/OpenAIAssistant_CodeInterpreter.cs | 2 +- .../Agents/OpenAIAssistant_FileManipulation.cs | 6 +++--- .../Concepts/Agents/OpenAIAssistant_Retrieval.cs | 4 ++-- dotnet/samples/GettingStartedWithAgents/Step1_Agent.cs | 4 +++- .../samples/GettingStartedWithAgents/Step2_Plugins.cs | 6 ++++-- dotnet/samples/GettingStartedWithAgents/Step3_Chat.cs | 2 +- .../Step4_KernelFunctionStrategies.cs | 2 +- .../GettingStartedWithAgents/Step5_JsonResult.cs | 2 +- .../Step6_DependencyInjection.cs | 2 +- .../samples/GettingStartedWithAgents/Step7_Logging.cs | 2 +- .../GettingStartedWithAgents/Step8_OpenAIAssistant.cs | 4 ++-- 14 files changed, 32 insertions(+), 24 deletions(-) diff --git a/dotnet/samples/Concepts/Agents/ComplexChat_NestedShopper.cs b/dotnet/samples/Concepts/Agents/ComplexChat_NestedShopper.cs index 0802980422cd..aae984906ba3 100644 --- a/dotnet/samples/Concepts/Agents/ComplexChat_NestedShopper.cs +++ b/dotnet/samples/Concepts/Agents/ComplexChat_NestedShopper.cs @@ -154,7 +154,7 @@ public async Task NestedChatWithAggregatorAgentAsync() Console.WriteLine(">>>> AGGREGATED CHAT"); Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); - await foreach (var content in chat.GetChatMessagesAsync(personalShopperAgent).Reverse()) + await foreach (ChatMessageContent content in chat.GetChatMessagesAsync(personalShopperAgent).Reverse()) { Console.WriteLine($">>>> {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); } @@ -165,7 +165,7 @@ async Task InvokeChatAsync(string input) Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var content in chat.InvokeAsync(personalShopperAgent)) + await foreach (ChatMessageContent content in chat.InvokeAsync(personalShopperAgent)) { Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); } diff --git a/dotnet/samples/Concepts/Agents/MixedChat_Agents.cs b/dotnet/samples/Concepts/Agents/MixedChat_Agents.cs index 68052ef99cf2..d3a894dd6c8e 100644 --- a/dotnet/samples/Concepts/Agents/MixedChat_Agents.cs +++ b/dotnet/samples/Concepts/Agents/MixedChat_Agents.cs @@ -56,8 +56,8 @@ await OpenAIAssistantAgent.CreateAsync( }); // Create a chat for agent interaction. - var chat = - new AgentGroupChat(agentWriter, agentReviewer) + AgentGroupChat chat = + new(agentWriter, agentReviewer) { ExecutionSettings = new() @@ -80,7 +80,7 @@ await OpenAIAssistantAgent.CreateAsync( chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input)); Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var content in chat.InvokeAsync()) + await foreach (ChatMessageContent content in chat.InvokeAsync()) { Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); } diff --git a/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs b/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs index 5617784b780c..93d695cb8551 100644 --- a/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs +++ b/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs @@ -1,8 +1,10 @@ // Copyright (c) Microsoft. All rights reserved. +using System.Diagnostics; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Agents; using Microsoft.SemanticKernel.Agents.OpenAI; using Microsoft.SemanticKernel.ChatCompletion; +using Microsoft.SemanticKernel.Connectors.OpenAI; namespace Agents; @@ -23,6 +25,8 @@ public class OpenAIAssistant_ChartMaker(ITestOutputHelper output) : BaseTest(out [Fact] public async Task GenerateChartWithOpenAIAssistantAgentAsync() { + OpenAIFileService fileService = new(TestConfiguration.OpenAI.ApiKey); + // Define the agent OpenAIAssistantAgent agent = await OpenAIAssistantAgent.CreateAsync( @@ -37,7 +41,7 @@ await OpenAIAssistantAgent.CreateAsync( }); // Create a chat for agent interaction. - var chat = new AgentGroupChat(); + AgentGroupChat chat = new(); // Respond to user input try @@ -68,14 +72,14 @@ async Task InvokeAgentAsync(string input) Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var message in chat.InvokeAsync(agent)) + await foreach (ChatMessageContent message in chat.InvokeAsync(agent)) { if (!string.IsNullOrWhiteSpace(message.Content)) { Console.WriteLine($"# {message.Role} - {message.AuthorName ?? "*"}: '{message.Content}'"); } - foreach (var fileReference in message.Items.OfType()) + foreach (FileReferenceContent fileReference in message.Items.OfType()) { Console.WriteLine($"# {message.Role} - {message.AuthorName ?? "*"}: @{fileReference.FileId}"); } diff --git a/dotnet/samples/Concepts/Agents/OpenAIAssistant_CodeInterpreter.cs b/dotnet/samples/Concepts/Agents/OpenAIAssistant_CodeInterpreter.cs index 636f70636126..75b237489025 100644 --- a/dotnet/samples/Concepts/Agents/OpenAIAssistant_CodeInterpreter.cs +++ b/dotnet/samples/Concepts/Agents/OpenAIAssistant_CodeInterpreter.cs @@ -28,7 +28,7 @@ await OpenAIAssistantAgent.CreateAsync( }); // Create a chat for agent interaction. - var chat = new AgentGroupChat(); + AgentGroupChat chat = new(); // Respond to user input try diff --git a/dotnet/samples/Concepts/Agents/OpenAIAssistant_FileManipulation.cs b/dotnet/samples/Concepts/Agents/OpenAIAssistant_FileManipulation.cs index dbe9d17ba90a..8e64006ee9d3 100644 --- a/dotnet/samples/Concepts/Agents/OpenAIAssistant_FileManipulation.cs +++ b/dotnet/samples/Concepts/Agents/OpenAIAssistant_FileManipulation.cs @@ -44,7 +44,7 @@ await OpenAIAssistantAgent.CreateAsync( }); // Create a chat for agent interaction. - var chat = new AgentGroupChat(); + AgentGroupChat chat = new(); // Respond to user input try @@ -66,11 +66,11 @@ async Task InvokeAgentAsync(string input) Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var content in chat.InvokeAsync(agent)) + await foreach (ChatMessageContent content in chat.InvokeAsync(agent)) { Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); - foreach (var annotation in content.Items.OfType()) + foreach (AnnotationContent annotation in content.Items.OfType()) { Console.WriteLine($"\n* '{annotation.Quote}' => {annotation.FileId}"); BinaryContent fileContent = await fileService.GetFileContentAsync(annotation.FileId!); diff --git a/dotnet/samples/Concepts/Agents/OpenAIAssistant_Retrieval.cs b/dotnet/samples/Concepts/Agents/OpenAIAssistant_Retrieval.cs index 9c7c9bb46f43..6f30b6974ff7 100644 --- a/dotnet/samples/Concepts/Agents/OpenAIAssistant_Retrieval.cs +++ b/dotnet/samples/Concepts/Agents/OpenAIAssistant_Retrieval.cs @@ -40,7 +40,7 @@ await OpenAIAssistantAgent.CreateAsync( }); // Create a chat for agent interaction. - var chat = new AgentGroupChat(); + AgentGroupChat chat = new(); // Respond to user input try @@ -61,7 +61,7 @@ async Task InvokeAgentAsync(string input) Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var content in chat.InvokeAsync(agent)) + await foreach (ChatMessageContent content in chat.InvokeAsync(agent)) { Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); } diff --git a/dotnet/samples/GettingStartedWithAgents/Step1_Agent.cs b/dotnet/samples/GettingStartedWithAgents/Step1_Agent.cs index ddab79f032b0..d7d4a0471b01 100644 --- a/dotnet/samples/GettingStartedWithAgents/Step1_Agent.cs +++ b/dotnet/samples/GettingStartedWithAgents/Step1_Agent.cs @@ -26,7 +26,7 @@ public async Task UseSingleChatCompletionAgentAsync() Kernel = this.CreateKernelWithChatCompletion(), }; - /// Create a chat for agent interaction. For more, . + /// Create the chat history to capture the agent interaction. ChatHistory chat = []; // Respond to user input @@ -43,6 +43,8 @@ async Task InvokeAgentAsync(string input) await foreach (ChatMessageContent content in agent.InvokeAsync(chat)) { + chat.Add(content); + Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); } } diff --git a/dotnet/samples/GettingStartedWithAgents/Step2_Plugins.cs b/dotnet/samples/GettingStartedWithAgents/Step2_Plugins.cs index 61737de498be..38741bbb2e7c 100644 --- a/dotnet/samples/GettingStartedWithAgents/Step2_Plugins.cs +++ b/dotnet/samples/GettingStartedWithAgents/Step2_Plugins.cs @@ -33,7 +33,7 @@ public async Task UseChatCompletionWithPluginAgentAsync() KernelPlugin plugin = KernelPluginFactory.CreateFromType(); agent.Kernel.Plugins.Add(plugin); - /// Create a chat for agent interaction. For more, . + /// Create the chat history to capture the agent interaction. ChatHistory chat = []; // Respond to user input, invoking functions where appropriate. @@ -48,8 +48,10 @@ async Task InvokeAgentAsync(string input) chat.Add(new ChatMessageContent(AuthorRole.User, input)); Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var content in agent.InvokeAsync(chat)) + await foreach (ChatMessageContent content in agent.InvokeAsync(chat)) { + chat.Add(content); + Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); } } diff --git a/dotnet/samples/GettingStartedWithAgents/Step3_Chat.cs b/dotnet/samples/GettingStartedWithAgents/Step3_Chat.cs index 0c9c60f870a7..5d0c185f95f5 100644 --- a/dotnet/samples/GettingStartedWithAgents/Step3_Chat.cs +++ b/dotnet/samples/GettingStartedWithAgents/Step3_Chat.cs @@ -78,7 +78,7 @@ public async Task UseAgentGroupChatWithTwoAgentsAsync() chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input)); Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var content in chat.InvokeAsync()) + await foreach (ChatMessageContent content in chat.InvokeAsync()) { Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); } diff --git a/dotnet/samples/GettingStartedWithAgents/Step4_KernelFunctionStrategies.cs b/dotnet/samples/GettingStartedWithAgents/Step4_KernelFunctionStrategies.cs index cd99531ec27b..9cabe0193d3e 100644 --- a/dotnet/samples/GettingStartedWithAgents/Step4_KernelFunctionStrategies.cs +++ b/dotnet/samples/GettingStartedWithAgents/Step4_KernelFunctionStrategies.cs @@ -120,7 +120,7 @@ State only the name of the participant to take the next turn. chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input)); Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var content in chat.InvokeAsync()) + await foreach (ChatMessageContent content in chat.InvokeAsync()) { Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); } diff --git a/dotnet/samples/GettingStartedWithAgents/Step5_JsonResult.cs b/dotnet/samples/GettingStartedWithAgents/Step5_JsonResult.cs index b1e83a202505..20ad4c2096d4 100644 --- a/dotnet/samples/GettingStartedWithAgents/Step5_JsonResult.cs +++ b/dotnet/samples/GettingStartedWithAgents/Step5_JsonResult.cs @@ -64,7 +64,7 @@ async Task InvokeAgentAsync(string input) Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var content in chat.InvokeAsync(agent)) + await foreach (ChatMessageContent content in chat.InvokeAsync(agent)) { Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); Console.WriteLine($"# IS COMPLETE: {chat.IsComplete}"); diff --git a/dotnet/samples/GettingStartedWithAgents/Step6_DependencyInjection.cs b/dotnet/samples/GettingStartedWithAgents/Step6_DependencyInjection.cs index a7e3b9b41450..21af5db70dce 100644 --- a/dotnet/samples/GettingStartedWithAgents/Step6_DependencyInjection.cs +++ b/dotnet/samples/GettingStartedWithAgents/Step6_DependencyInjection.cs @@ -82,7 +82,7 @@ async Task WriteAgentResponse(string input) { Console.WriteLine($"# {AuthorRole.User}: {input}"); - await foreach (var content in agentClient.RunDemoAsync(input)) + await foreach (ChatMessageContent content in agentClient.RunDemoAsync(input)) { Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); } diff --git a/dotnet/samples/GettingStartedWithAgents/Step7_Logging.cs b/dotnet/samples/GettingStartedWithAgents/Step7_Logging.cs index 4372d71e37f8..1ab559e668fb 100644 --- a/dotnet/samples/GettingStartedWithAgents/Step7_Logging.cs +++ b/dotnet/samples/GettingStartedWithAgents/Step7_Logging.cs @@ -85,7 +85,7 @@ public async Task UseLoggerFactoryWithAgentGroupChatAsync() chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input)); Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var content in chat.InvokeAsync()) + await foreach (ChatMessageContent content in chat.InvokeAsync()) { Console.WriteLine($"# {content.Role} - {content.AuthorName ?? "*"}: '{content.Content}'"); } diff --git a/dotnet/samples/GettingStartedWithAgents/Step8_OpenAIAssistant.cs b/dotnet/samples/GettingStartedWithAgents/Step8_OpenAIAssistant.cs index 09afcfc44826..d9e9760e3fa6 100644 --- a/dotnet/samples/GettingStartedWithAgents/Step8_OpenAIAssistant.cs +++ b/dotnet/samples/GettingStartedWithAgents/Step8_OpenAIAssistant.cs @@ -36,7 +36,7 @@ await OpenAIAssistantAgent.CreateAsync( KernelPlugin plugin = KernelPluginFactory.CreateFromType(); agent.Kernel.Plugins.Add(plugin); - // Create a chat for agent interaction. + // Create a thread for the agent interaction. string threadId = await agent.CreateThreadAsync(); // Respond to user input @@ -60,7 +60,7 @@ async Task InvokeAgentAsync(string input) Console.WriteLine($"# {AuthorRole.User}: '{input}'"); - await foreach (var content in agent.InvokeAsync(threadId)) + await foreach (ChatMessageContent content in agent.InvokeAsync(threadId)) { if (content.Role != AuthorRole.Tool) { From 066d0094f759d26f311422270194750d6a30b344 Mon Sep 17 00:00:00 2001 From: Chris Rickman Date: Wed, 10 Jul 2024 18:32:23 -0700 Subject: [PATCH 2/4] Namespace --- dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs b/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs index 93d695cb8551..f984dccd8555 100644 --- a/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs +++ b/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs @@ -1,5 +1,4 @@ // Copyright (c) Microsoft. All rights reserved. -using System.Diagnostics; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Agents; using Microsoft.SemanticKernel.Agents.OpenAI; From 0c1515db72447e89dcd92ac3d335d5087cb274b2 Mon Sep 17 00:00:00 2001 From: Chris Rickman Date: Thu, 11 Jul 2024 07:15:06 -0700 Subject: [PATCH 3/4] Clean --- dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs b/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs index f984dccd8555..251bf33b5cbb 100644 --- a/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs +++ b/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs @@ -1,4 +1,5 @@ // Copyright (c) Microsoft. All rights reserved. +using System.Diagnostics; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Agents; using Microsoft.SemanticKernel.Agents.OpenAI; @@ -24,8 +25,6 @@ public class OpenAIAssistant_ChartMaker(ITestOutputHelper output) : BaseTest(out [Fact] public async Task GenerateChartWithOpenAIAssistantAgentAsync() { - OpenAIFileService fileService = new(TestConfiguration.OpenAI.ApiKey); - // Define the agent OpenAIAssistantAgent agent = await OpenAIAssistantAgent.CreateAsync( From bb5c9c73a94dfbf00e34b76849af4b11cd1164e9 Mon Sep 17 00:00:00 2001 From: Chris Rickman Date: Thu, 11 Jul 2024 07:15:35 -0700 Subject: [PATCH 4/4] Clean --- dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs b/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs index 251bf33b5cbb..ef5ba80154fa 100644 --- a/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs +++ b/dotnet/samples/Concepts/Agents/OpenAIAssistant_ChartMaker.cs @@ -1,10 +1,8 @@ // Copyright (c) Microsoft. All rights reserved. -using System.Diagnostics; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Agents; using Microsoft.SemanticKernel.Agents.OpenAI; using Microsoft.SemanticKernel.ChatCompletion; -using Microsoft.SemanticKernel.Connectors.OpenAI; namespace Agents;