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.4 kB

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