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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. [Trait("Category", "UnitV1")]
  18. public class FunctionTests
  19. {
  20. private readonly JsonSerializerOptions _jsonSerializerOptions = new() { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull };
  21. [Description("get weather")]
  22. public string GetWeather(string city, string date = "today")
  23. {
  24. return $"The weather in {city} is sunny.";
  25. }
  26. [Description("get weather from static method")]
  27. [return: Description("weather information")]
  28. public static string GetWeatherStatic(string city, string[] date)
  29. {
  30. return $"The weather in {city} is sunny.";
  31. }
  32. [Description("get weather from async method")]
  33. public async Task<string> GetWeatherAsync(string city)
  34. {
  35. await Task.Delay(100);
  36. return $"The weather in {city} is sunny.";
  37. }
  38. [Description("get weather from async static method")]
  39. public static async Task<string> GetWeatherAsyncStatic(string city)
  40. {
  41. await Task.Delay(100);
  42. return $"The weather in {city} is sunny.";
  43. }
  44. [Fact]
  45. [UseReporter(typeof(DiffReporter))]
  46. [UseApprovalSubdirectory("ApprovalTests")]
  47. public async Task CreateGetWeatherFunctionFromAIFunctionFactoryAsync()
  48. {
  49. Delegate[] availableDelegates = [
  50. GetWeather,
  51. GetWeatherStatic,
  52. GetWeatherAsync,
  53. GetWeatherAsyncStatic,
  54. ];
  55. var functionContracts = availableDelegates.Select(function => (FunctionContract)AIFunctionFactory.Create(function).Metadata).ToList();
  56. // Verify the function contracts
  57. functionContracts.Should().HaveCount(4);
  58. var openAIToolContracts = functionContracts.Select(f =>
  59. {
  60. var tool = f.ToChatTool();
  61. return new
  62. {
  63. tool.Kind,
  64. tool.FunctionName,
  65. tool.FunctionDescription,
  66. FunctionParameters = tool.FunctionParameters.ToObjectFromJson<object>(),
  67. };
  68. });
  69. var json = JsonSerializer.Serialize(openAIToolContracts, _jsonSerializerOptions);
  70. Approvals.Verify(json);
  71. }
  72. }