| 
									
										
										
										
											2024-09-30 16:32:48 -07:00
										 |  |  | // Copyright (c) Microsoft Corporation. All rights reserved. | 
					
						
							| 
									
										
										
										
											2024-04-26 09:21:46 -07:00
										 |  |  | // Example10_SemanticKernel.cs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using System.ComponentModel; | 
					
						
							|  |  |  | using AutoGen.Core; | 
					
						
							|  |  |  | using AutoGen.SemanticKernel.Extension; | 
					
						
							|  |  |  | using FluentAssertions; | 
					
						
							|  |  |  | using Microsoft.SemanticKernel; | 
					
						
							|  |  |  | using Microsoft.SemanticKernel.ChatCompletion; | 
					
						
							|  |  |  | using Microsoft.SemanticKernel.Connectors.OpenAI; | 
					
						
							| 
									
										
										
										
											2025-01-28 17:13:36 -05:00
										 |  |  | namespace AutoGen.Basic.Sample; | 
					
						
							| 
									
										
										
										
											2024-04-26 09:21:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | public class LightPlugin | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-09-30 16:32:48 -07:00
										 |  |  |     public bool IsOn { get; set; } | 
					
						
							| 
									
										
										
										
											2024-04-26 09:21:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     [KernelFunction] | 
					
						
							|  |  |  |     [Description("Gets the state of the light.")] | 
					
						
							|  |  |  |     public string GetState() => this.IsOn ? "on" : "off"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     [KernelFunction] | 
					
						
							|  |  |  |     [Description("Changes the state of the light.'")] | 
					
						
							|  |  |  |     public string ChangeState(bool newState) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         this.IsOn = newState; | 
					
						
							|  |  |  |         var state = this.GetState(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Print the state to the console | 
					
						
							|  |  |  |         Console.ForegroundColor = ConsoleColor.DarkBlue; | 
					
						
							|  |  |  |         Console.WriteLine($"[Light is now {state}]"); | 
					
						
							|  |  |  |         Console.ResetColor(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return state; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public class Example10_SemanticKernel | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public static async Task RunAsync() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable."); | 
					
						
							| 
									
										
										
										
											2024-08-27 14:37:47 -07:00
										 |  |  |         var modelId = "gpt-4o-mini"; | 
					
						
							| 
									
										
										
										
											2024-04-26 09:21:46 -07:00
										 |  |  |         var builder = Kernel.CreateBuilder() | 
					
						
							|  |  |  |             .AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey); | 
					
						
							|  |  |  |         var kernel = builder.Build(); | 
					
						
							|  |  |  |         var settings = new OpenAIPromptExecutionSettings | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         kernel.Plugins.AddFromObject(new LightPlugin()); | 
					
						
							|  |  |  |         var skAgent = kernel | 
					
						
							|  |  |  |             .ToSemanticKernelAgent(name: "assistant", systemMessage: "You control the light", settings); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Send a message to the skAgent, the skAgent supports the following message types: | 
					
						
							|  |  |  |         // - IMessage<ChatMessageContent> | 
					
						
							|  |  |  |         // - (streaming) IMessage<StreamingChatMessageContent> | 
					
						
							|  |  |  |         // You can create an IMessage<ChatMessageContent> using MessageEnvelope.Create | 
					
						
							|  |  |  |         var chatMessageContent = MessageEnvelope.Create(new ChatMessageContent(AuthorRole.User, "Toggle the light")); | 
					
						
							|  |  |  |         var reply = await skAgent.SendAsync(chatMessageContent); | 
					
						
							|  |  |  |         reply.Should().BeOfType<MessageEnvelope<ChatMessageContent>>(); | 
					
						
							|  |  |  |         Console.WriteLine((reply as IMessage<ChatMessageContent>).Content.Items[0].As<TextContent>().Text); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var skAgentWithMiddleware = skAgent | 
					
						
							| 
									
										
										
										
											2024-05-05 07:51:00 -07:00
										 |  |  |             .RegisterMessageConnector() // Register the message connector to support more AutoGen built-in message types | 
					
						
							| 
									
										
										
										
											2024-04-26 09:21:46 -07:00
										 |  |  |             .RegisterPrintMessage(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Now the skAgentWithMiddleware supports more IMessage types like TextMessage, ImageMessage or MultiModalMessage | 
					
						
							|  |  |  |         // It also register a print format message hook to print the message in a human readable format to the console | 
					
						
							|  |  |  |         await skAgent.SendAsync(chatMessageContent); | 
					
						
							|  |  |  |         await skAgentWithMiddleware.SendAsync(new TextMessage(Role.User, "Toggle the light")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // The more message type an agent support, the more flexible it is to be used in different scenarios | 
					
						
							|  |  |  |         // For example, since the TextMessage is supported, the skAgentWithMiddleware can be used with user proxy. | 
					
						
							|  |  |  |         var userProxy = new UserProxyAgent("user"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await skAgentWithMiddleware.InitiateChatAsync(userProxy, "how can I help you today"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |