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.

Use_Tools_With_Agent.cs 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Use_Tools_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. using OpenAI;
  10. namespace AutoGen.Basic.Sample;
  11. #region Tools
  12. public partial class Tools
  13. {
  14. /// <summary>
  15. /// Get the weather of the city.
  16. /// </summary>
  17. /// <param name="city"></param>
  18. [Function]
  19. public async Task<string> GetWeather(string city)
  20. {
  21. return $"The weather in {city} is sunny.";
  22. }
  23. }
  24. #endregion Tools
  25. public class Use_Tools_With_Agent
  26. {
  27. public static async Task RunAsync()
  28. {
  29. #region Create_tools
  30. var tools = new Tools();
  31. #endregion Create_tools
  32. #region Create_auto_invoke_middleware
  33. var autoInvokeMiddleware = new FunctionCallMiddleware(
  34. functions: [tools.GetWeatherFunctionContract],
  35. functionMap: new Dictionary<string, Func<string, Task<string>>>()
  36. {
  37. { tools.GetWeatherFunctionContract.Name!, tools.GetWeatherWrapper },
  38. });
  39. #endregion Create_auto_invoke_middleware
  40. #region Create_no_invoke_middleware
  41. var noInvokeMiddleware = new FunctionCallMiddleware(
  42. functions: [tools.GetWeatherFunctionContract]);
  43. #endregion Create_no_invoke_middleware
  44. #region Create_Agent
  45. var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");
  46. var model = "gpt-4o-mini";
  47. var openaiClient = new OpenAIClient(apiKey);
  48. var agent = new OpenAIChatAgent(
  49. chatClient: openaiClient.GetChatClient(model),
  50. name: "agent",
  51. systemMessage: "You are a helpful AI assistant")
  52. .RegisterMessageConnector(); // convert OpenAI message to AutoGen message
  53. #endregion Create_Agent
  54. #region Single_Turn_Auto_Invoke
  55. var autoInvokeAgent = agent
  56. .RegisterMiddleware(autoInvokeMiddleware) // pass function definition to agent.
  57. .RegisterPrintMessage(); // print the message content
  58. var question = new TextMessage(Role.User, "What is the weather in Seattle?");
  59. var reply = await autoInvokeAgent.SendAsync(question);
  60. reply.Should().BeOfType<ToolCallAggregateMessage>();
  61. #endregion Single_Turn_Auto_Invoke
  62. #region Single_Turn_No_Invoke
  63. var noInvokeAgent = agent
  64. .RegisterMiddleware(noInvokeMiddleware) // pass function definition to agent.
  65. .RegisterPrintMessage(); // print the message content
  66. question = new TextMessage(Role.User, "What is the weather in Seattle?");
  67. reply = await noInvokeAgent.SendAsync(question);
  68. reply.Should().BeOfType<ToolCallMessage>();
  69. #endregion Single_Turn_No_Invoke
  70. #region Multi_Turn_Tool_Call
  71. var finalReply = await agent.SendAsync(chatHistory: [question, reply]);
  72. #endregion Multi_Turn_Tool_Call
  73. #region verify_reply
  74. finalReply.Should().BeOfType<TextMessage>();
  75. #endregion verify_reply
  76. #region parallel_tool_call
  77. question = new TextMessage(Role.User, "What is the weather in Seattle, New York and Vancouver");
  78. reply = await agent.SendAsync(question);
  79. #endregion parallel_tool_call
  80. #region verify_parallel_tool_call_reply
  81. reply.Should().BeOfType<ToolCallAggregateMessage>();
  82. (reply as ToolCallAggregateMessage)!.Message1.ToolCalls.Count.Should().Be(3);
  83. #endregion verify_parallel_tool_call_reply
  84. #region Multi_Turn_Parallel_Tool_Call
  85. finalReply = await agent.SendAsync(chatHistory: [question, reply]);
  86. finalReply.Should().BeOfType<ToolCallAggregateMessage>();
  87. (finalReply as ToolCallAggregateMessage)!.Message1.ToolCalls.Count.Should().Be(3);
  88. #endregion Multi_Turn_Parallel_Tool_Call
  89. }
  90. }