AutoGen.SemanticKernel
provides built-in support for ChatCompletionAgent
via SemanticKernelChatCompletionAgent. By default the SemanticKernelChatCompletionAgent only supports the original ChatMessageContent
type via IMessage<ChatMessageContent>
. To support more AutoGen built-in message types like TextMessage, ImageMessage, MultiModalMessage, you can register the agent with SemanticKernelChatMessageContentConnector. The SemanticKernelChatMessageContentConnector will convert the message from AutoGen built-in message types to ChatMessageContent
and vice versa.
The following step-by-step example shows how to create an SemanticKernelChatCompletionAgent and chat with it:
Note
You can find the complete sample code here.
Step 1: add using statement
using AutoGen.Core;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
Step 2: create kernel
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();
Step 3: create ChatCompletionAgent
// The built-in ChatCompletionAgent from semantic kernel.
var chatAgent = new ChatCompletionAgent()
{
Kernel = kernel,
Name = "assistant",
Description = "You are a helpful AI assistant",
};
Step 4: create SemanticKernelChatCompletionAgent
In this step, we create an SemanticKernelChatCompletionAgent and register it with SemanticKernelChatMessageContentConnector. The SemanticKernelChatMessageContentConnector will convert the message from AutoGen built-in message types to ChatMessageContent
and vice versa.
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
Step 5: chat with SemanticKernelChatCompletionAgent
await skAgent.SendAsync("Hey tell me a long tedious joke");