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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Azure.AI.OpenAI;
  9. using Microsoft.SemanticKernel;
  10. namespace AutoGen.BasicSample;
  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-3.5-turbo";
  17. #region Create_Coder
  18. var openaiClient = new OpenAIClient(apiKey);
  19. var coder = new OpenAIChatAgent(
  20. openAIClient: openaiClient,
  21. name: "coder",
  22. modelName: model,
  23. systemMessage: "You are a C# coder, when writing csharp code, please put the code between ```csharp and ```")
  24. .RegisterMessageConnector() // convert OpenAI message to AutoGen message
  25. .RegisterPrintMessage(); // print the message content
  26. #endregion Create_Coder
  27. #region Create_Commenter
  28. var kernel = Kernel
  29. .CreateBuilder()
  30. .AddOpenAIChatCompletion(modelId: model, apiKey: apiKey)
  31. .Build();
  32. var commenter = new SemanticKernelAgent(
  33. kernel: kernel,
  34. name: "commenter",
  35. systemMessage: "You write inline comments for the code snippet and add unit tests if necessary")
  36. .RegisterMessageConnector() // register message connector so it support AutoGen built-in message types like TextMessage.
  37. .RegisterPrintMessage(); // pretty print the message to the console
  38. #endregion Create_Commenter
  39. #region Create_UserProxy
  40. var userProxy = new DefaultReplyAgent("user", defaultReply: "END")
  41. .RegisterPrintMessage(); // print the message content
  42. #endregion Create_UserProxy
  43. #region Create_Group
  44. var admin = new OpenAIChatAgent(
  45. openAIClient: openaiClient,
  46. name: "admin",
  47. modelName: model)
  48. .RegisterMessageConnector(); // convert OpenAI message to AutoGen message
  49. var group = new GroupChat(
  50. members: [coder, commenter, userProxy],
  51. admin: admin);
  52. #endregion Create_Group
  53. #region Chat_With_Group
  54. var workflowInstruction = new TextMessage(
  55. Role.User,
  56. """
  57. Here is the workflow of this group chat:
  58. User{Ask a question} -> Coder{Write code}
  59. Coder{Write code} -> Commenter{Add comments to the code}
  60. Commenter{Add comments to the code} -> User{END}
  61. """);
  62. var question = new TextMessage(Role.User, "How to calculate the 100th Fibonacci number?");
  63. var chatHistory = new List<IMessage> { workflowInstruction, question };
  64. while (true)
  65. {
  66. var replies = await group.CallAsync(chatHistory, maxRound: 1);
  67. var lastReply = replies.Last();
  68. chatHistory.Add(lastReply);
  69. if (lastReply.From == userProxy.Name)
  70. {
  71. break;
  72. }
  73. }
  74. #endregion Chat_With_Group
  75. #region Summarize_Chat_History
  76. var summary = await coder.SendAsync("summarize the conversation", chatHistory: chatHistory);
  77. #endregion Summarize_Chat_History
  78. }
  79. }