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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. using Azure.AI.OpenAI;
  8. #endregion Using
  9. using FluentAssertions;
  10. namespace AutoGen.BasicSample;
  11. public class Chat_With_Agent
  12. {
  13. public static async Task RunAsync()
  14. {
  15. #region Create_Agent
  16. var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
  17. var model = "gpt-3.5-turbo";
  18. var openaiClient = new OpenAIClient(apiKey);
  19. var agent = new OpenAIChatAgent(
  20. openAIClient: openaiClient,
  21. name: "agent",
  22. modelName: model,
  23. systemMessage: "You are a helpful AI assistant")
  24. .RegisterMessageConnector(); // convert OpenAI message to AutoGen message
  25. #endregion Create_Agent
  26. #region Chat_With_Agent
  27. var reply = await agent.SendAsync("Tell me a joke");
  28. reply.Should().BeOfType<TextMessage>();
  29. if (reply is TextMessage textMessage)
  30. {
  31. Console.WriteLine(textMessage.Content);
  32. }
  33. #endregion Chat_With_Agent
  34. #region Chat_With_History
  35. reply = await agent.SendAsync("summarize the conversation", chatHistory: [reply]);
  36. #endregion Chat_With_History
  37. #region Streaming_Chat
  38. var question = new TextMessage(Role.User, "Tell me a long joke");
  39. await foreach (var streamingReply in agent.GenerateStreamingReplyAsync([question]))
  40. {
  41. if (streamingReply is TextMessageUpdate textMessageUpdate)
  42. {
  43. Console.WriteLine(textMessageUpdate.Content);
  44. }
  45. }
  46. #endregion Streaming_Chat
  47. #region verify_reply
  48. reply.Should().BeOfType<TextMessage>();
  49. #endregion verify_reply
  50. }
  51. }