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.

Image_Chat_With_Agent.cs 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Image_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 Image_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-4o"; // The model needs to support multimodal inputs
  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. .RegisterPrintMessage();
  26. #endregion Create_Agent
  27. #region Prepare_Image_Input
  28. var backgoundImagePath = Path.Combine("resource", "images", "background.png");
  29. var imageBytes = File.ReadAllBytes(backgoundImagePath);
  30. var imageMessage = new ImageMessage(Role.User, BinaryData.FromBytes(imageBytes, "image/png"));
  31. #endregion Prepare_Image_Input
  32. #region Prepare_Multimodal_Input
  33. var textMessage = new TextMessage(Role.User, "what's in the picture");
  34. var multimodalMessage = new MultiModalMessage(Role.User, [textMessage, imageMessage]);
  35. #endregion Prepare_Multimodal_Input
  36. #region Chat_With_Agent
  37. var reply = await agent.SendAsync("what's in the picture", chatHistory: [imageMessage]);
  38. // or use multimodal message to generate reply
  39. reply = await agent.SendAsync(multimodalMessage);
  40. #endregion Chat_With_Agent
  41. #region verify_reply
  42. reply.Should().BeOfType<TextMessage>();
  43. #endregion verify_reply
  44. }
  45. }