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.

Chat_With_Agent.cs 1.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Chat_With_Agent.cs
  3. #region Using
  4. using AutoGen.Core;
  5. using AutoGen.OpenAI;
  6. using AutoGen.OpenAI.Extension;
  7. #endregion Using
  8. using FluentAssertions;
  9. namespace AutoGen.Basic.Sample;
  10. public class Chat_With_Agent
  11. {
  12. public static async Task RunAsync()
  13. {
  14. #region Create_Agent
  15. var gpt4o = LLMConfiguration.GetOpenAIGPT4o_mini();
  16. var agent = new OpenAIChatAgent(
  17. chatClient: gpt4o,
  18. name: "agent",
  19. systemMessage: "You are a helpful AI assistant")
  20. .RegisterMessageConnector(); // convert OpenAI message to AutoGen message
  21. #endregion Create_Agent
  22. #region Chat_With_Agent
  23. var reply = await agent.SendAsync("Tell me a joke");
  24. reply.Should().BeOfType<TextMessage>();
  25. if (reply is TextMessage textMessage)
  26. {
  27. Console.WriteLine(textMessage.Content);
  28. }
  29. #endregion Chat_With_Agent
  30. #region Chat_With_History
  31. reply = await agent.SendAsync("summarize the conversation", chatHistory: [reply]);
  32. #endregion Chat_With_History
  33. #region Streaming_Chat
  34. var question = new TextMessage(Role.User, "Tell me a long joke");
  35. await foreach (var streamingReply in agent.GenerateStreamingReplyAsync([question]))
  36. {
  37. if (streamingReply is TextMessageUpdate textMessageUpdate)
  38. {
  39. Console.WriteLine(textMessageUpdate.Content);
  40. }
  41. }
  42. #endregion Streaming_Chat
  43. #region verify_reply
  44. reply.Should().BeOfType<TextMessage>();
  45. #endregion verify_reply
  46. }
  47. }