A programming framework for agentic AI
[.Net] add AutoGen.SemanticKernel.Sample project (#2774)
Xiaoyun Zhang8d55334e4ded
diff --git a/dotnet/AutoGen.sln b/dotnet/AutoGen.slnindex 3d0848fb3..2f1413f43 100644--- a/dotnet/AutoGen.sln+++ b/dotnet/AutoGen.sln@@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.Ollama", "src\AutoGEndProjectProject("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoGen.Ollama.Tests", "test\AutoGen.Ollama.Tests\AutoGen.Ollama.Tests.csproj", "{03E31CAA-3728-48D3-B936-9F11CF6C18FE}"EndProject+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoGen.SemanticKernel.Sample", "sample\AutoGen.SemanticKernel.Sample\AutoGen.SemanticKernel.Sample.csproj", "{52958A60-3FF7-4243-9058-34A6E4F55C31}"+EndProjectGlobalGlobalSection(SolutionConfigurationPlatforms) = preSolutionDebug|Any CPU = Debug|Any CPU@@ -117,6 +119,10 @@ Global{03E31CAA-3728-48D3-B936-9F11CF6C18FE}.Debug|Any CPU.Build.0 = Debug|Any CPU{03E31CAA-3728-48D3-B936-9F11CF6C18FE}.Release|Any CPU.ActiveCfg = Release|Any CPU{03E31CAA-3728-48D3-B936-9F11CF6C18FE}.Release|Any CPU.Build.0 = Release|Any CPU+ {52958A60-3FF7-4243-9058-34A6E4F55C31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU+ {52958A60-3FF7-4243-9058-34A6E4F55C31}.Debug|Any CPU.Build.0 = Debug|Any CPU+ {52958A60-3FF7-4243-9058-34A6E4F55C31}.Release|Any CPU.ActiveCfg = Release|Any CPU+ {52958A60-3FF7-4243-9058-34A6E4F55C31}.Release|Any CPU.Build.0 = Release|Any CPUEndGlobalSectionGlobalSection(SolutionProperties) = preSolutionHideSolutionNode = FALSE@@ -139,6 +145,7 @@ Global{B61388CA-DC73-4B7F-A7B2-7B9A86C9229E} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}{9F9E6DED-3D92-4970-909A-70FC11F1A665} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}{03E31CAA-3728-48D3-B936-9F11CF6C18FE} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}+ {52958A60-3FF7-4243-9058-34A6E4F55C31} = {FBFEAD1F-29EB-4D99-A672-0CD8473E10B9}EndGlobalSectionGlobalSection(ExtensibilityGlobals) = postSolutionSolutionGuid = {93384647-528D-46C8-922C-8DB36A382F0B}diff --git a/dotnet/sample/AutoGen.BasicSamples/AutoGen.BasicSample.csproj b/dotnet/sample/AutoGen.BasicSamples/AutoGen.BasicSample.csprojindex 0cafff3c0..afc761649 100644--- a/dotnet/sample/AutoGen.BasicSamples/AutoGen.BasicSample.csproj+++ b/dotnet/sample/AutoGen.BasicSamples/AutoGen.BasicSample.csproj@@ -4,7 +4,6 @@<OutputType>Exe</OutputType><TargetFramework>$(TestTargetFramework)</TargetFramework><ImplicitUsings>enable</ImplicitUsings>- <Nullable>enable</Nullable><GenerateDocumentationFile>True</GenerateDocumentationFile><NoWarn>$(NoWarn);CS8981;CS8600;CS8602;CS8604;CS8618;CS0219;SKEXP0054;SKEXP0050;SKEXP0110</NoWarn></PropertyGroup>diff --git a/dotnet/sample/AutoGen.SemanticKernel.Sample/AutoGen.SemanticKernel.Sample.csproj b/dotnet/sample/AutoGen.SemanticKernel.Sample/AutoGen.SemanticKernel.Sample.csprojnew file mode 100644index 000000000..6c2266512--- /dev/null+++ b/dotnet/sample/AutoGen.SemanticKernel.Sample/AutoGen.SemanticKernel.Sample.csproj@@ -0,0 +1,17 @@+<Project Sdk="Microsoft.NET.Sdk">++ <PropertyGroup>+ <OutputType>Exe</OutputType>+ <TargetFramework>$(TestTargetFramework)</TargetFramework>+ <GenerateDocumentationFile>True</GenerateDocumentationFile>+ <NoWarn>$(NoWarn);CS8981;CS8600;CS8602;CS8604;CS8618;CS0219;SKEXP0054;SKEXP0050;SKEXP0110</NoWarn>+ <ImplicitUsings>enable</ImplicitUsings>+ </PropertyGroup>++ <ItemGroup>+ <ProjectReference Include="..\..\src\AutoGen.SourceGenerator\AutoGen.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />+ <ProjectReference Include="..\AutoGen.BasicSamples\AutoGen.BasicSample.csproj" />+ <PackageReference Include="Microsoft.SemanticKernel.Plugins.Web" Version="$(SemanticKernelExperimentalVersion)" />+ </ItemGroup>++</Project>diff --git a/dotnet/sample/AutoGen.SemanticKernel.Sample/Create_Semantic_Kernel_Agent.cs b/dotnet/sample/AutoGen.SemanticKernel.Sample/Create_Semantic_Kernel_Agent.csnew file mode 100644index 000000000..3333cdd9a--- /dev/null+++ b/dotnet/sample/AutoGen.SemanticKernel.Sample/Create_Semantic_Kernel_Agent.cs@@ -0,0 +1,29 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// Create_Semantic_Kernel_Agent.cs++using AutoGen.Core;+using AutoGen.SemanticKernel.Extension;+using Microsoft.SemanticKernel;++namespace AutoGen.SemanticKernel.Sample;++public class Create_Semantic_Kernel_Agent+{+ public static async Task RunAsync()+ {+ var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");+ var modelId = "gpt-3.5-turbo";+ var kernel = Kernel.CreateBuilder()+ .AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey)+ .Build();++ var skAgent = new SemanticKernelAgent(+ kernel: kernel,+ name: "assistant",+ systemMessage: "You are a helpful AI assistant")+ .RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage.+ .RegisterPrintMessage(); // pretty print the message to the console++ await skAgent.SendAsync("Hey tell me a long tedious joke");+ }+}diff --git a/dotnet/sample/AutoGen.SemanticKernel.Sample/Create_Semantic_Kernel_Chat_Agent.cs b/dotnet/sample/AutoGen.SemanticKernel.Sample/Create_Semantic_Kernel_Chat_Agent.csnew file mode 100644index 000000000..0caea6f03--- /dev/null+++ b/dotnet/sample/AutoGen.SemanticKernel.Sample/Create_Semantic_Kernel_Chat_Agent.cs@@ -0,0 +1,35 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// Create_Semantic_Kernel_Chat_Agent.cs++using AutoGen.Core;+using Microsoft.SemanticKernel;+using Microsoft.SemanticKernel.Agents;++namespace AutoGen.SemanticKernel.Sample;++public class Create_Semantic_Kernel_Chat_Agent+{+ public static async Task RunAsync()+ {+ var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");+ var modelId = "gpt-3.5-turbo";+ var kernel = Kernel.CreateBuilder()+ .AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey)+ .Build();++ // The built-in ChatCompletionAgent from semantic kernel.+ var chatAgent = new ChatCompletionAgent()+ {+ Kernel = kernel,+ Name = "assistant",+ Description = "You are a helpful AI assistant",+ };++ var messageConnector = new SemanticKernelChatMessageContentConnector();+ var skAgent = new SemanticKernelChatCompletionAgent(chatAgent)+ .RegisterMiddleware(messageConnector) // register message connector so it support AutoGen built-in message types like TextMessage.+ .RegisterPrintMessage(); // pretty print the message to the console++ await skAgent.SendAsync("Hey tell me a long tedious joke");+ }+}diff --git a/dotnet/sample/AutoGen.SemanticKernel.Sample/Program.cs b/dotnet/sample/AutoGen.SemanticKernel.Sample/Program.csnew file mode 100644index 000000000..5032f2d43--- /dev/null+++ b/dotnet/sample/AutoGen.SemanticKernel.Sample/Program.cs@@ -0,0 +1,6 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// Program.cs++using AutoGen.SemanticKernel.Sample;++await Use_Kernel_Functions_With_Other_Agent.RunAsync();diff --git a/dotnet/sample/AutoGen.SemanticKernel.Sample/Use_Bing_Search_With_Semantic_Kernel_Agent.cs b/dotnet/sample/AutoGen.SemanticKernel.Sample/Use_Bing_Search_With_Semantic_Kernel_Agent.csnew file mode 100644index 000000000..4cebc8829--- /dev/null+++ b/dotnet/sample/AutoGen.SemanticKernel.Sample/Use_Bing_Search_With_Semantic_Kernel_Agent.cs@@ -0,0 +1,37 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// Use_Bing_Search_With_Semantic_Kernel_Agent.cs++using AutoGen.Core;+using AutoGen.SemanticKernel.Extension;+using Microsoft.SemanticKernel;+using Microsoft.SemanticKernel.Plugins.Web;+using Microsoft.SemanticKernel.Plugins.Web.Bing;++namespace AutoGen.SemanticKernel.Sample;++public class Use_Bing_Search_With_Semantic_Kernel_Agent+{+ public static async Task RunAsync()+ {+ var bingApiKey = Environment.GetEnvironmentVariable("BING_API_KEY") ?? throw new Exception("BING_API_KEY environment variable is not set");+ var bingSearch = new BingConnector(bingApiKey);+ var webSearchPlugin = new WebSearchEnginePlugin(bingSearch);++ var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");+ var modelId = "gpt-3.5-turbo";+ var kernelBuilder = Kernel.CreateBuilder()+ .AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey);+ kernelBuilder.Plugins.AddFromObject(webSearchPlugin);++ var kernel = kernelBuilder.Build();++ var skAgent = new SemanticKernelAgent(+ kernel: kernel,+ name: "assistant",+ systemMessage: "You are a helpful AI assistant")+ .RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage.+ .RegisterPrintMessage(); // pretty print the message to the console++ await skAgent.SendAsync("Tell me more about gpt-4-o");+ }+}diff --git a/dotnet/sample/AutoGen.SemanticKernel.Sample/Use_Kernel_Functions_With_Other_Agent.cs b/dotnet/sample/AutoGen.SemanticKernel.Sample/Use_Kernel_Functions_With_Other_Agent.csnew file mode 100644index 000000000..d91d72766--- /dev/null+++ b/dotnet/sample/AutoGen.SemanticKernel.Sample/Use_Kernel_Functions_With_Other_Agent.cs@@ -0,0 +1,44 @@+// Copyright (c) Microsoft Corporation. All rights reserved.+// Use_Kernel_Functions_With_Other_Agent.cs++using AutoGen.Core;+using AutoGen.OpenAI;+using AutoGen.OpenAI.Extension;+using Azure.AI.OpenAI;+using Microsoft.SemanticKernel;++namespace AutoGen.SemanticKernel.Sample;++public class Use_Kernel_Functions_With_Other_Agent+{+ public static async Task RunAsync()+ {+ var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");+ var modelId = "gpt-3.5-turbo";+ var kernelBuilder = Kernel.CreateBuilder();+ var kernel = kernelBuilder.Build();+ var getWeatherFunction = KernelFunctionFactory.CreateFromMethod(+ method: (string location) => $"The weather in {location} is 75 degrees Fahrenheit.",+ functionName: "GetWeather",+ description: "Get the weather for a location.");+ var plugin = kernel.CreatePluginFromFunctions("my_plugin", [getWeatherFunction]);++ // Create a middleware to handle the plugin functions+ var kernelPluginMiddleware = new KernelPluginMiddleware(kernel, plugin);++ var openAIClient = new OpenAIClient(openAIKey);+ var openAIAgent = new OpenAIChatAgent(+ openAIClient: openAIClient,+ name: "assistant",+ modelName: modelId)+ .RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage.+ .RegisterMiddleware(kernelPluginMiddleware) // register the middleware to handle the plugin functions+ .RegisterPrintMessage(); // pretty print the message to the console++ var toolAggregateMessage = await openAIAgent.SendAsync("Tell me the weather in Seattle");++ // The aggregate message will be converted to [ToolCallMessage, ToolCallResultMessage] when flowing into the agent+ // send the aggregated message to llm to generate the final response+ var finalReply = await openAIAgent.SendAsync(toolAggregateMessage);+ }+}