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.

FunctionTests.cs 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // FunctionTests.cs
  3. using System;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text.Json;
  7. using System.Text.Json.Serialization;
  8. using System.Threading.Tasks;
  9. using ApprovalTests;
  10. using ApprovalTests.Namers;
  11. using ApprovalTests.Reporters;
  12. using AutoGen.OpenAI.Extension;
  13. using FluentAssertions;
  14. using Microsoft.Extensions.AI;
  15. using Xunit;
  16. namespace AutoGen.Tests.Function;
  17. public class FunctionTests
  18. {
  19. private readonly JsonSerializerOptions _jsonSerializerOptions = new() { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull };
  20. [Description("get weather")]
  21. public string GetWeather(string city, string date = "today")
  22. {
  23. return $"The weather in {city} is sunny.";
  24. }
  25. [Description("get weather from static method")]
  26. [return: Description("weather information")]
  27. public static string GetWeatherStatic(string city, string[] date)
  28. {
  29. return $"The weather in {city} is sunny.";
  30. }
  31. [Description("get weather from async method")]
  32. public async Task<string> GetWeatherAsync(string city)
  33. {
  34. await Task.Delay(100);
  35. return $"The weather in {city} is sunny.";
  36. }
  37. [Description("get weather from async static method")]
  38. public static async Task<string> GetWeatherAsyncStatic(string city)
  39. {
  40. await Task.Delay(100);
  41. return $"The weather in {city} is sunny.";
  42. }
  43. [Fact]
  44. [UseReporter(typeof(DiffReporter))]
  45. [UseApprovalSubdirectory("ApprovalTests")]
  46. public async Task CreateGetWeatherFunctionFromAIFunctionFactoryAsync()
  47. {
  48. Delegate[] availableDelegates = [
  49. GetWeather,
  50. GetWeatherStatic,
  51. GetWeatherAsync,
  52. GetWeatherAsyncStatic,
  53. ];
  54. var functionContracts = availableDelegates.Select(function => (FunctionContract)AIFunctionFactory.Create(function).Metadata).ToList();
  55. // Verify the function contracts
  56. functionContracts.Should().HaveCount(4);
  57. var openAIToolContracts = functionContracts.Select(f =>
  58. {
  59. var tool = f.ToChatTool();
  60. return new
  61. {
  62. tool.Kind,
  63. tool.FunctionName,
  64. tool.FunctionDescription,
  65. FunctionParameters = tool.FunctionParameters.ToObjectFromJson<object>(),
  66. };
  67. });
  68. var json = JsonSerializer.Serialize(openAIToolContracts, _jsonSerializerOptions);
  69. Approvals.Verify(json);
  70. }
  71. }