2024-10-02 11:42:27 -07:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2024-05-23 18:53:22 -07:00
|
|
|
// Use_Kernel_Functions_With_Other_Agent.cs
|
|
|
|
|
2024-05-28 14:55:40 -07:00
|
|
|
#region Using
|
2024-05-23 18:53:22 -07:00
|
|
|
using AutoGen.Core;
|
2024-08-27 14:37:47 -07:00
|
|
|
using AutoGen.OpenAI;
|
|
|
|
using AutoGen.OpenAI.Extension;
|
2024-05-23 18:53:22 -07:00
|
|
|
using Microsoft.SemanticKernel;
|
2024-08-27 14:37:47 -07:00
|
|
|
using OpenAI;
|
2024-05-28 14:55:40 -07:00
|
|
|
#endregion Using
|
2024-05-23 18:53:22 -07:00
|
|
|
|
|
|
|
namespace AutoGen.SemanticKernel.Sample;
|
|
|
|
|
|
|
|
public class Use_Kernel_Functions_With_Other_Agent
|
|
|
|
{
|
|
|
|
public static async Task RunAsync()
|
|
|
|
{
|
2024-05-28 14:55:40 -07:00
|
|
|
#region Create_plugin
|
2024-05-23 18:53:22 -07:00
|
|
|
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-05-23 18:53:22 -07:00
|
|
|
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]);
|
2024-05-28 14:55:40 -07:00
|
|
|
#endregion Create_plugin
|
2024-05-23 18:53:22 -07:00
|
|
|
|
2024-05-28 14:55:40 -07:00
|
|
|
#region Use_plugin
|
2024-05-23 18:53:22 -07:00
|
|
|
// Create a middleware to handle the plugin functions
|
|
|
|
var kernelPluginMiddleware = new KernelPluginMiddleware(kernel, plugin);
|
|
|
|
|
|
|
|
var openAIClient = new OpenAIClient(openAIKey);
|
|
|
|
var openAIAgent = new OpenAIChatAgent(
|
2024-08-27 14:37:47 -07:00
|
|
|
chatClient: openAIClient.GetChatClient(modelId),
|
|
|
|
name: "assistant")
|
2024-05-23 18:53:22 -07:00
|
|
|
.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
|
2024-05-28 14:55:40 -07:00
|
|
|
#endregion Use_plugin
|
2024-05-23 18:53:22 -07:00
|
|
|
|
2024-05-28 14:55:40 -07:00
|
|
|
#region Send_message
|
2024-05-23 18:53:22 -07:00
|
|
|
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);
|
2024-05-28 14:55:40 -07:00
|
|
|
#endregion Send_message
|
2024-05-23 18:53:22 -07:00
|
|
|
}
|
|
|
|
}
|