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.1 kB

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