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.

Create_Semantic_Kernel_Chat_Agent.cs 1.4 kB

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Create_Semantic_Kernel_Chat_Agent.cs
  3. using AutoGen.Core;
  4. using Microsoft.SemanticKernel;
  5. using Microsoft.SemanticKernel.Agents;
  6. namespace AutoGen.SemanticKernel.Sample;
  7. public class Create_Semantic_Kernel_Chat_Agent
  8. {
  9. public static async Task RunAsync()
  10. {
  11. var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
  12. var modelId = "gpt-3.5-turbo";
  13. var kernel = Kernel.CreateBuilder()
  14. .AddOpenAIChatCompletion(modelId: modelId, apiKey: openAIKey)
  15. .Build();
  16. // The built-in ChatCompletionAgent from semantic kernel.
  17. var chatAgent = new ChatCompletionAgent()
  18. {
  19. Kernel = kernel,
  20. Name = "assistant",
  21. Description = "You are a helpful AI assistant",
  22. };
  23. var messageConnector = new SemanticKernelChatMessageContentConnector();
  24. var skAgent = new SemanticKernelChatCompletionAgent(chatAgent)
  25. .RegisterMiddleware(messageConnector) // register message connector so it support AutoGen built-in message types like TextMessage.
  26. .RegisterPrintMessage(); // pretty print the message to the console
  27. await skAgent.SendAsync("Hey tell me a long tedious joke");
  28. }
  29. }