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

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