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

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