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.

Create_Anthropic_Agent_With_Tool.cs 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Create_Anthropic_Agent_With_Tool.cs
  3. using AutoGen.Anthropic.DTO;
  4. using AutoGen.Anthropic.Extensions;
  5. using AutoGen.Anthropic.Utils;
  6. using AutoGen.Core;
  7. using FluentAssertions;
  8. namespace AutoGen.Anthropic.Sample;
  9. #region WeatherFunction
  10. public partial class WeatherFunction
  11. {
  12. /// <summary>
  13. /// Gets the weather based on the location and the unit
  14. /// </summary>
  15. /// <param name="location"></param>
  16. /// <param name="unit"></param>
  17. /// <returns></returns>
  18. [Function]
  19. public async Task<string> GetWeather(string location, string unit)
  20. {
  21. // dummy implementation
  22. return $"The weather in {location} is currently sunny with a tempature of {unit} (s)";
  23. }
  24. }
  25. #endregion
  26. public class Create_Anthropic_Agent_With_Tool
  27. {
  28. public static async Task RunAsync()
  29. {
  30. #region define_tool
  31. var tool = new Tool
  32. {
  33. Name = "GetWeather",
  34. Description = "Get the current weather in a given location",
  35. InputSchema = new InputSchema
  36. {
  37. Type = "object",
  38. Properties = new Dictionary<string, SchemaProperty>
  39. {
  40. { "location", new SchemaProperty { Type = "string", Description = "The city and state, e.g. San Francisco, CA" } },
  41. { "unit", new SchemaProperty { Type = "string", Description = "The unit of temperature, either \"celsius\" or \"fahrenheit\"" } }
  42. },
  43. Required = new List<string> { "location" }
  44. }
  45. };
  46. var weatherFunction = new WeatherFunction();
  47. var functionMiddleware = new FunctionCallMiddleware(
  48. functions: [
  49. weatherFunction.GetWeatherFunctionContract,
  50. ],
  51. functionMap: new Dictionary<string, Func<string, Task<string>>>
  52. {
  53. { weatherFunction.GetWeatherFunctionContract.Name!, weatherFunction.GetWeatherWrapper },
  54. });
  55. #endregion
  56. #region create_anthropic_agent
  57. var apiKey = Environment.GetEnvironmentVariable("ANTHROPIC_API_KEY") ??
  58. throw new Exception("Missing ANTHROPIC_API_KEY environment variable.");
  59. var anthropicClient = new AnthropicClient(new HttpClient(), AnthropicConstants.Endpoint, apiKey);
  60. var agent = new AnthropicClientAgent(anthropicClient, "assistant", AnthropicConstants.Claude3Haiku,
  61. tools: [tool]); // Define tools for AnthropicClientAgent
  62. #endregion
  63. #region register_middleware
  64. var agentWithConnector = agent
  65. .RegisterMessageConnector()
  66. .RegisterPrintMessage()
  67. .RegisterStreamingMiddleware(functionMiddleware);
  68. #endregion register_middleware
  69. #region single_turn
  70. var question = new TextMessage(Role.Assistant,
  71. "What is the weather like in San Francisco?",
  72. from: "user");
  73. var functionCallReply = await agentWithConnector.SendAsync(question);
  74. #endregion
  75. #region Single_turn_verify_reply
  76. functionCallReply.Should().BeOfType<ToolCallAggregateMessage>();
  77. #endregion Single_turn_verify_reply
  78. #region Multi_turn
  79. var finalReply = await agentWithConnector.SendAsync(chatHistory: [question, functionCallReply]);
  80. #endregion Multi_turn
  81. #region Multi_turn_verify_reply
  82. finalReply.Should().BeOfType<TextMessage>();
  83. #endregion Multi_turn_verify_reply
  84. }
  85. }