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

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