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.

KernelFunctionMiddlewareTests.cs 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // KernelFunctionMiddlewareTests.cs
  3. using AutoGen.Core;
  4. using AutoGen.OpenAI;
  5. using AutoGen.OpenAI.Extension;
  6. using AutoGen.Tests;
  7. using Azure.AI.OpenAI;
  8. using FluentAssertions;
  9. using Microsoft.SemanticKernel;
  10. namespace AutoGen.SemanticKernel.Tests;
  11. public class KernelFunctionMiddlewareTests
  12. {
  13. [ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_DEPLOY_NAME")]
  14. public async Task ItRegisterKernelFunctionMiddlewareFromTestPluginTests()
  15. {
  16. var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("Please set AZURE_OPENAI_ENDPOINT environment variable.");
  17. var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");
  18. var deployName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new Exception("Please set AZURE_OPENAI_DEPLOY_NAME environment variable.");
  19. var openaiClient = new OpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(key));
  20. var kernel = new Kernel();
  21. var plugin = kernel.ImportPluginFromType<TestPlugin>();
  22. var kernelFunctionMiddleware = new KernelPluginMiddleware(kernel, plugin);
  23. var agent = new OpenAIChatAgent(openaiClient, "assistant", modelName: deployName)
  24. .RegisterMessageConnector()
  25. .RegisterMiddleware(kernelFunctionMiddleware);
  26. var reply = await agent.SendAsync("what's the status of the light?");
  27. reply.GetContent().Should().Be("off");
  28. reply.Should().BeOfType<ToolCallAggregateMessage>();
  29. if (reply is ToolCallAggregateMessage aggregateMessage)
  30. {
  31. var toolCallMessage = aggregateMessage.Message1;
  32. toolCallMessage.ToolCalls.Should().HaveCount(1);
  33. toolCallMessage.ToolCalls[0].FunctionName.Should().Be("GetState");
  34. var toolCallResultMessage = aggregateMessage.Message2;
  35. toolCallResultMessage.ToolCalls.Should().HaveCount(1);
  36. toolCallResultMessage.ToolCalls[0].Result.Should().Be("off");
  37. }
  38. reply = await agent.SendAsync("change the status of the light to on");
  39. reply.GetContent().Should().Be("The status of the light is now on");
  40. reply.Should().BeOfType<ToolCallAggregateMessage>();
  41. if (reply is ToolCallAggregateMessage aggregateMessage1)
  42. {
  43. var toolCallMessage = aggregateMessage1.Message1;
  44. toolCallMessage.ToolCalls.Should().HaveCount(1);
  45. toolCallMessage.ToolCalls[0].FunctionName.Should().Be("ChangeState");
  46. var toolCallResultMessage = aggregateMessage1.Message2;
  47. toolCallResultMessage.ToolCalls.Should().HaveCount(1);
  48. }
  49. }
  50. [ApiKeyFact("AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_DEPLOY_NAME")]
  51. public async Task ItRegisterKernelFunctionMiddlewareFromMethodTests()
  52. {
  53. var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new Exception("Please set AZURE_OPENAI_ENDPOINT environment variable.");
  54. var key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new Exception("Please set AZURE_OPENAI_API_KEY environment variable.");
  55. var deployName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? throw new Exception("Please set AZURE_OPENAI_DEPLOY_NAME environment variable.");
  56. var openaiClient = new OpenAIClient(new Uri(endpoint), new Azure.AzureKeyCredential(key));
  57. var kernel = new Kernel();
  58. var getWeatherMethod = kernel.CreateFunctionFromMethod((string location) => $"The weather in {location} is sunny.", functionName: "GetWeather", description: "Get the weather for a location.");
  59. var createPersonObjectMethod = kernel.CreateFunctionFromMethod((string name, string email, int age) => new Person(name, email, age), functionName: "CreatePersonObject", description: "Creates a person object.");
  60. var plugin = kernel.ImportPluginFromFunctions("plugin", [getWeatherMethod, createPersonObjectMethod]);
  61. var kernelFunctionMiddleware = new KernelPluginMiddleware(kernel, plugin);
  62. var agent = new OpenAIChatAgent(openaiClient, "assistant", modelName: deployName)
  63. .RegisterMessageConnector()
  64. .RegisterMiddleware(kernelFunctionMiddleware);
  65. var reply = await agent.SendAsync("what's the weather in Seattle?");
  66. reply.GetContent().Should().Be("The weather in Seattle is sunny.");
  67. reply.Should().BeOfType<ToolCallAggregateMessage>();
  68. if (reply is ToolCallAggregateMessage getWeatherMessage)
  69. {
  70. var toolCallMessage = getWeatherMessage.Message1;
  71. toolCallMessage.ToolCalls.Should().HaveCount(1);
  72. toolCallMessage.ToolCalls[0].FunctionName.Should().Be("GetWeather");
  73. var toolCallResultMessage = getWeatherMessage.Message2;
  74. toolCallResultMessage.ToolCalls.Should().HaveCount(1);
  75. }
  76. reply = await agent.SendAsync("Create a person object with name: John, email: 12345@gmail.com, age: 30");
  77. reply.GetContent().Should().Be("Name: John, Email: 12345@gmail.com, Age: 30");
  78. reply.Should().BeOfType<ToolCallAggregateMessage>();
  79. if (reply is ToolCallAggregateMessage createPersonObjectMessage)
  80. {
  81. var toolCallMessage = createPersonObjectMessage.Message1;
  82. toolCallMessage.ToolCalls.Should().HaveCount(1);
  83. toolCallMessage.ToolCalls[0].FunctionName.Should().Be("CreatePersonObject");
  84. var toolCallResultMessage = createPersonObjectMessage.Message2;
  85. toolCallResultMessage.ToolCalls.Should().HaveCount(1);
  86. }
  87. }
  88. }
  89. public class Person
  90. {
  91. public Person(string name, string email, int age)
  92. {
  93. this.Name = name;
  94. this.Email = email;
  95. this.Age = age;
  96. }
  97. public string Name { get; set; }
  98. public string Email { get; set; }
  99. public int Age { get; set; }
  100. public override string ToString()
  101. {
  102. return $"Name: {this.Name}, Email: {this.Email}, Age: {this.Age}";
  103. }
  104. }