You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Use_Kernel_Functions_With_Other_Agent.cs 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Use_Kernel_Functions_With_Other_Agent.cs
  3. #region Using
  4. using AutoGen.Core;
  5. using AutoGen.OpenAI;
  6. using AutoGen.OpenAI.Extension;
  7. using Microsoft.SemanticKernel;
  8. using OpenAI;
  9. #endregion Using
  10. namespace AutoGen.SemanticKernel.Sample;
  11. public class Use_Kernel_Functions_With_Other_Agent
  12. {
  13. public static async Task RunAsync()
  14. {
  15. #region Create_plugin
  16. var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
  17. var modelId = "gpt-4o-mini";
  18. var kernelBuilder = Kernel.CreateBuilder();
  19. var kernel = kernelBuilder.Build();
  20. var getWeatherFunction = KernelFunctionFactory.CreateFromMethod(
  21. method: (string location) => $"The weather in {location} is 75 degrees Fahrenheit.",
  22. functionName: "GetWeather",
  23. description: "Get the weather for a location.");
  24. var plugin = kernel.CreatePluginFromFunctions("my_plugin", [getWeatherFunction]);
  25. #endregion Create_plugin
  26. #region Use_plugin
  27. // Create a middleware to handle the plugin functions
  28. var kernelPluginMiddleware = new KernelPluginMiddleware(kernel, plugin);
  29. var openAIClient = new OpenAIClient(openAIKey);
  30. var openAIAgent = new OpenAIChatAgent(
  31. chatClient: openAIClient.GetChatClient(modelId),
  32. name: "assistant")
  33. .RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage.
  34. .RegisterMiddleware(kernelPluginMiddleware) // register the middleware to handle the plugin functions
  35. .RegisterPrintMessage(); // pretty print the message to the console
  36. #endregion Use_plugin
  37. #region Send_message
  38. var toolAggregateMessage = await openAIAgent.SendAsync("Tell me the weather in Seattle");
  39. // The aggregate message will be converted to [ToolCallMessage, ToolCallResultMessage] when flowing into the agent
  40. // send the aggregated message to llm to generate the final response
  41. var finalReply = await openAIAgent.SendAsync(toolAggregateMessage);
  42. #endregion Send_message
  43. }
  44. }