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 5.8 kB

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