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.

Dynamic_Group_Chat.cs 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Dynamic_Group_Chat.cs
  3. using AutoGen.Core;
  4. using AutoGen.OpenAI;
  5. using AutoGen.OpenAI.Extension;
  6. using AutoGen.SemanticKernel;
  7. using AutoGen.SemanticKernel.Extension;
  8. using Microsoft.SemanticKernel;
  9. using OpenAI;
  10. namespace AutoGen.Basic.Sample;
  11. public class Dynamic_Group_Chat
  12. {
  13. public static async Task RunAsync()
  14. {
  15. var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
  16. var model = "gpt-4o-mini";
  17. #region Create_Coder
  18. var openaiClient = new OpenAIClient(apiKey);
  19. var coder = new OpenAIChatAgent(
  20. chatClient: openaiClient.GetChatClient(model),
  21. name: "coder",
  22. systemMessage: "You are a C# coder, when writing csharp code, please put the code between ```csharp and ```")
  23. .RegisterMessageConnector() // convert OpenAI message to AutoGen message
  24. .RegisterPrintMessage(); // print the message content
  25. #endregion Create_Coder
  26. #region Create_Commenter
  27. var kernel = Kernel
  28. .CreateBuilder()
  29. .AddOpenAIChatCompletion(modelId: model, apiKey: apiKey)
  30. .Build();
  31. var commenter = new SemanticKernelAgent(
  32. kernel: kernel,
  33. name: "commenter",
  34. systemMessage: "You write inline comments for the code snippet and add unit tests if necessary")
  35. .RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage.
  36. .RegisterPrintMessage(); // pretty print the message to the console
  37. #endregion Create_Commenter
  38. #region Create_UserProxy
  39. var userProxy = new DefaultReplyAgent("user", defaultReply: "END")
  40. .RegisterPrintMessage(); // print the message content
  41. #endregion Create_UserProxy
  42. #region Create_Group
  43. var admin = new OpenAIChatAgent(
  44. chatClient: openaiClient.GetChatClient(model),
  45. name: "admin")
  46. .RegisterMessageConnector(); // convert OpenAI message to AutoGen message
  47. var group = new GroupChat(
  48. members: [coder, commenter, userProxy],
  49. admin: admin);
  50. #endregion Create_Group
  51. #region Chat_With_Group
  52. var workflowInstruction = new TextMessage(
  53. Role.User,
  54. """
  55. Here is the workflow of this group chat:
  56. User{Ask a question} -> Coder{Write code}
  57. Coder{Write code} -> Commenter{Add comments to the code}
  58. Commenter{Add comments to the code} -> User{END}
  59. """);
  60. var question = new TextMessage(Role.User, "How to calculate the 100th Fibonacci number?");
  61. var chatHistory = new List<IMessage> { workflowInstruction, question };
  62. while (true)
  63. {
  64. var replies = await group.CallAsync(chatHistory, maxRound: 1);
  65. var lastReply = replies.Last();
  66. chatHistory.Add(lastReply);
  67. if (lastReply.From == userProxy.Name)
  68. {
  69. break;
  70. }
  71. }
  72. #endregion Chat_With_Group
  73. #region Summarize_Chat_History
  74. var summary = await coder.SendAsync("summarize the conversation", chatHistory: chatHistory);
  75. #endregion Summarize_Chat_History
  76. }
  77. }