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.

KernelFunctionExtensionTests.cs 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // KernelFunctionExtensionTests.cs
  3. using System.ComponentModel;
  4. using ApprovalTests;
  5. using ApprovalTests.Namers;
  6. using ApprovalTests.Reporters;
  7. using AutoGen.SemanticKernel.Extension;
  8. using FluentAssertions;
  9. using Microsoft.SemanticKernel;
  10. using Newtonsoft.Json;
  11. using Xunit;
  12. namespace AutoGen.SemanticKernel.Tests;
  13. [Trait("Category", "UnitV1")]
  14. public class TestPlugin
  15. {
  16. public bool IsOn { get; set; }
  17. [KernelFunction]
  18. [Description("Gets the state of the light.")]
  19. public string GetState() => this.IsOn ? "on" : "off";
  20. [KernelFunction]
  21. [Description("Changes the state of the light.'")]
  22. public string ChangeState(
  23. [Description("new state")] bool newState)
  24. {
  25. this.IsOn = newState;
  26. var state = this.GetState();
  27. // Print the state to the console
  28. Console.ForegroundColor = ConsoleColor.DarkBlue;
  29. Console.WriteLine($"[Light is now {state}]");
  30. Console.ResetColor();
  31. return $"The status of the light is now {state}";
  32. }
  33. }
  34. public class KernelFunctionExtensionTests
  35. {
  36. private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
  37. {
  38. Formatting = Formatting.Indented,
  39. NullValueHandling = NullValueHandling.Ignore,
  40. StringEscapeHandling = StringEscapeHandling.Default,
  41. };
  42. [Fact]
  43. [UseReporter(typeof(DiffReporter))]
  44. [UseApprovalSubdirectory("ApprovalTests")]
  45. public void ItCreateFunctionContractsFromTestPlugin()
  46. {
  47. var kernel = new Kernel();
  48. var plugin = kernel.ImportPluginFromType<TestPlugin>("test_plugin");
  49. var functionContracts = plugin.Select(f => f.Metadata.ToFunctionContract()).ToList();
  50. functionContracts.Count.Should().Be(2);
  51. var json = JsonConvert.SerializeObject(functionContracts, _serializerSettings);
  52. Approvals.Verify(json);
  53. }
  54. [Fact]
  55. [UseReporter(typeof(DiffReporter))]
  56. [UseApprovalSubdirectory("ApprovalTests")]
  57. public void ItCreateFunctionContractsFromMethod()
  58. {
  59. var kernel = new Kernel();
  60. var sayHelloFunction = KernelFunctionFactory.CreateFromMethod(() => "Hello, World!");
  61. var echoFunction = KernelFunctionFactory.CreateFromMethod((string message) => message);
  62. var functionContracts = new[]
  63. {
  64. sayHelloFunction.Metadata.ToFunctionContract(),
  65. echoFunction.Metadata.ToFunctionContract(),
  66. };
  67. var json = JsonConvert.SerializeObject(functionContracts, _serializerSettings);
  68. functionContracts.Length.Should().Be(2);
  69. Approvals.Verify(json);
  70. }
  71. [Fact]
  72. [UseReporter(typeof(DiffReporter))]
  73. [UseApprovalSubdirectory("ApprovalTests")]
  74. public void ItCreateFunctionContractsFromPrompt()
  75. {
  76. var kernel = new Kernel();
  77. var sayHelloFunction = KernelFunctionFactory.CreateFromPrompt("Say {{hello}}, World!", functionName: "sayHello");
  78. var functionContracts = new[]
  79. {
  80. sayHelloFunction.Metadata.ToFunctionContract(),
  81. };
  82. var json = JsonConvert.SerializeObject(functionContracts, _serializerSettings);
  83. functionContracts.Length.Should().Be(1);
  84. Approvals.Verify(json);
  85. }
  86. }