经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
旁门左道:借助 HttpClientHandler 拦截请求,体验 Semantic Kernel 插件
来源:cnblogs  作者:dudu  时间:2024/2/21 9:28:26  对本文有异议

前天尝试通过 one-api + dashscope(阿里云灵积) + qwen(通义千问)运行 Semantic Kernel 插件(Plugin) ,结果尝试失败,详见前天的博文

今天换一种方式尝试,选择了一个旁门左道走走看,看能不能在不使用大模型的情况下让 Semantic Kernel 插件运行起来,这个旁门左道就是从 Stephen Toub 那偷学到的一招 —— 借助 DelegatingHandler(new HttpClientHandler()) 拦截 HttpClient 请求,直接以模拟数据进行响应。

先创建一个 .NET 控制台项目

  1. dotnet new console
  2. dotnet add package Microsoft.SemanticKernel
  3. dotnet add package Microsoft.Extensions.Http

参照 Semantic Kernel 源码中的示例代码创建一个非常简单的插件 LightPlugin

  1. public class LightPlugin
  2. {
  3. public bool IsOn { get; set; } = false;
  4. [KernelFunction]
  5. [Description("帮看一下灯是开是关")]
  6. public string GetState() => IsOn ? "on" : "off";
  7. [KernelFunction]
  8. [Description("开灯或者关灯")]
  9. public string ChangeState(bool newState)
  10. {
  11. IsOn = newState;
  12. var state = GetState();
  13. Console.WriteLine(state == "on" ? $"[开灯啦]" : "[关灯咯]");
  14. return state;
  15. }
  16. }

接着创建旁门左道 BackdoorHandler,先实现一个最简单的功能,打印 HttpClient 请求内容

  1. public class BypassHandler() : DelegatingHandler(new HttpClientHandler())
  2. {
  3. protected override async Task<HttpResponseMessage> SendAsync(
  4. HttpRequestMessage request, CancellationToken cancellationToken)
  5. {
  6. Console.WriteLine(await request.Content!.ReadAsStringAsync());
  7. // return await base.SendAsync(request, cancellationToken);
  8. return new HttpResponseMessage(HttpStatusCode.OK);
  9. }
  10. }

然后携 LightPluginBypassHandler 创建 Semantic Kernel 的 Kernel

  1. var builder = Kernel.CreateBuilder();
  2. builder.Services.AddOpenAIChatCompletion("qwen-max", "sk-xxxxxx");
  3. builder.Services.ConfigureHttpClientDefaults(b =>
  4. b.ConfigurePrimaryHttpMessageHandler(() => new BypassHandler()));
  5. builder.Plugins.AddFromType<LightPlugin>();
  6. Kernel kernel = builder.Build();

再然后,发送携带 prompt 的请求并获取响应内容

  1. var history = new ChatHistory();
  2. history.AddUserMessage("请开灯");
  3. Console.WriteLine("User > " + history[0].Content);
  4. var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
  5. // Enable auto function calling
  6. OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
  7. {
  8. ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions
  9. };
  10. var result = await chatCompletionService.GetChatMessageContentAsync(
  11. history,
  12. executionSettings: openAIPromptExecutionSettings,
  13. kernel: kernel);
  14. Console.WriteLine("Assistant > " + result);

运行控制台程序,BypassHandler 就会在控制台输出请求的 json 内容(为了阅读方便对json进行了格式化):

点击查看 json
  1. {
  2. "messages": [
  3. {
  4. "content": "Assistant is a large language model.",
  5. "role": "system"
  6. },
  7. {
  8. "content": "\u8BF7\u5F00\u706F",
  9. "role": "user"
  10. }
  11. ],
  12. "temperature": 1,
  13. "top_p": 1,
  14. "n": 1,
  15. "presence_penalty": 0,
  16. "frequency_penalty": 0,
  17. "model": "qwen-max",
  18. "tools": [
  19. {
  20. "function": {
  21. "name": "LightPlugin-GetState",
  22. "description": "\u5E2E\u770B\u4E00\u4E0B\u706F\u662F\u5F00\u662F\u5173",
  23. "parameters": {
  24. "type": "object",
  25. "required": [],
  26. "properties": {}
  27. }
  28. },
  29. "type": "function"
  30. },
  31. {
  32. "function": {
  33. "name": "LightPlugin-ChangeState",
  34. "description": "\u5F00\u706F\u6216\u8005\u5173\u706F",
  35. "parameters": {
  36. "type": "object",
  37. "required": [
  38. "newState"
  39. ],
  40. "properties": {
  41. "newState": {
  42. "type": "boolean"
  43. }
  44. }
  45. }
  46. },
  47. "type": "function"
  48. }
  49. ],
  50. "tool_choice": "auto"
  51. }

为了能反序列化这个 json ,我们需要定义一个类型 ChatCompletionRequest,Sermantic Kernel 中没有现成可以使用的,实现代码如下:

点击查看 ChatCompletionRequest
  1. public class ChatCompletionRequest
  2. {
  3. [JsonPropertyName("messages")]
  4. public IReadOnlyList<RequestMessage>? Messages { get; set; }
  5. [JsonPropertyName("temperature")]
  6. public double Temperature { get; set; } = 1;
  7. [JsonPropertyName("top_p")]
  8. public double TopP { get; set; } = 1;
  9. [JsonPropertyName("n")]
  10. public int? N { get; set; } = 1;
  11. [JsonPropertyName("presence_penalty")]
  12. public double PresencePenalty { get; set; } = 0;
  13. [JsonPropertyName("frequency_penalty")]
  14. public double FrequencyPenalty { get; set; } = 0;
  15. [JsonPropertyName("model")]
  16. public required string Model { get; set; }
  17. [JsonPropertyName("tools")]
  18. public IReadOnlyList<Tool>? Tools { get; set; }
  19. [JsonPropertyName("tool_choice")]
  20. public string? ToolChoice { get; set; }
  21. }
  22. public class RequestMessage
  23. {
  24. [JsonPropertyName("role")]
  25. public string? Role { get; set; }
  26. [JsonPropertyName("name")]
  27. public string? Name { get; set; }
  28. [JsonPropertyName("content")]
  29. public string? Content { get; set; }
  30. }
  31. public class Tool
  32. {
  33. [JsonPropertyName("function")]
  34. public FunctionDefinition? Function { get; set; }
  35. [JsonPropertyName("type")]
  36. public string? Type { get; set; }
  37. }
  38. public class FunctionDefinition
  39. {
  40. [JsonPropertyName("name")]
  41. public string? Name { get; set; }
  42. [JsonPropertyName("description")]
  43. public string? Description { get; set; }
  44. [JsonPropertyName("parameters")]
  45. public ParameterDefinition Parameters { get; set; }
  46. public struct ParameterDefinition
  47. {
  48. [JsonPropertyName("type")]
  49. public required string Type { get; set; }
  50. [JsonPropertyName("description")]
  51. public string? Description { get; set; }
  52. [JsonPropertyName("required")]
  53. public string[]? Required { get; set; }
  54. [JsonPropertyName("properties")]
  55. public Dictionary<string, PropertyDefinition>? Properties { get; set; }
  56. public struct PropertyDefinition
  57. {
  58. [JsonPropertyName("type")]
  59. public required PropertyType Type { get; set; }
  60. }
  61. [JsonConverter(typeof(JsonStringEnumConverter))]
  62. public enum PropertyType
  63. {
  64. Number,
  65. String,
  66. Boolean
  67. }
  68. }
  69. }

有了这个类,我们就可以从请求中获取对应 Plugin 的 function 信息,比如下面的代码:

  1. var function = chatCompletionRequest?.Tools.FirstOrDefault(x => x.Function.Description.Contains("开灯"))?.Function;
  2. var functionName = function.Name;
  3. var parameterName = function.Parameters.Properties.FirstOrDefault(x => x.Value.Type == PropertyType.Boolean).Key;

接下来就是旁门左道的关键,直接在 BypassHandler 中响应 Semantic Kernel 通过 OpenAI.ClientCore 发出的 http 请求。

首先创建用于 json 序列化的类 ChatCompletionResponse

点击查看 ChatCompletionResponse
  1. public class ChatCompletionResponse
  2. {
  3. [JsonPropertyName("id")]
  4. public string? Id { get; set; }
  5. [JsonPropertyName("object")]
  6. public string? Object { get; set; }
  7. [JsonPropertyName("created")]
  8. public long Created { get; set; }
  9. [JsonPropertyName("model")]
  10. public string? Model { get; set; }
  11. [JsonPropertyName("usage"), JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
  12. public Usage? Usage { get; set; }
  13. [JsonPropertyName("choices")]
  14. public List<Choice>? Choices { get; set; }
  15. }
  16. public class Choice
  17. {
  18. [JsonPropertyName("message")]
  19. public ResponseMessage? Message { get; set; }
  20. /// <summary>
  21. /// The message in this response (when streaming a response).
  22. /// </summary>
  23. [JsonPropertyName("delta"), JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
  24. public ResponseMessage? Delta { get; set; }
  25. [JsonPropertyName("finish_reason")]
  26. public string? FinishReason { get; set; }
  27. /// <summary>
  28. /// The index of this response in the array of choices.
  29. /// </summary>
  30. [JsonPropertyName("index")]
  31. public int Index { get; set; }
  32. }
  33. public class ResponseMessage
  34. {
  35. [JsonPropertyName("role")]
  36. public string? Role { get; set; }
  37. [JsonPropertyName("name"), JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
  38. public string? Name { get; set; }
  39. [JsonPropertyName("content"), JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
  40. public string? Content { get; set; }
  41. [JsonPropertyName("tool_calls")]
  42. public IReadOnlyList<ToolCall>? ToolCalls { get; set; }
  43. }
  44. public class ToolCall
  45. {
  46. [JsonPropertyName("id")]
  47. public string? Id { get; set; }
  48. [JsonPropertyName("function")]
  49. public FunctionCall? Function { get; set; }
  50. [JsonPropertyName("type")]
  51. public string? Type { get; set; }
  52. }
  53. public class Usage
  54. {
  55. [JsonPropertyName("prompt_tokens")]
  56. public int PromptTokens { get; set; }
  57. [JsonPropertyName("completion_tokens")]
  58. public int CompletionTokens { get; set; }
  59. [JsonPropertyName("total_tokens")]
  60. public int TotalTokens { get; set; }
  61. }
  62. public class FunctionCall
  63. {
  64. [JsonPropertyName("name")]
  65. public string Name { get; set; } = string.Empty;
  66. [JsonPropertyName("arguments")]
  67. public string Arguments { get; set; } = string.Empty;
  68. }

先试试不执行 function calling ,直接以 assistant 角色回复一句话

  1. public class BypassHandler() : DelegatingHandler(new HttpClientHandler())
  2. {
  3. protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
  4. {
  5. var chatCompletion = new ChatCompletionResponse
  6. {
  7. Id = Guid.NewGuid().ToString(),
  8. Model = "fake-mode",
  9. Object = "chat.completion",
  10. Created = DateTimeOffset.Now.ToUnixTimeSeconds(),
  11. Choices =
  12. [
  13. new()
  14. {
  15. Message = new ResponseMessage
  16. {
  17. Content = "自己动手,丰衣足食",
  18. Role = "assistant"
  19. },
  20. FinishReason = "stop"
  21. }
  22. ]
  23. };
  24. var json = JsonSerializer.Serialize(chatCompletion, GetJsonSerializerOptions());
  25. return new HttpResponseMessage
  26. {
  27. Content = new StringContent(json, Encoding.UTF8, "application/json")
  28. };
  29. }
  30. }

运行控制台程序,输出如下:

  1. User > 请开灯
  2. Assistant > 自己动手,丰衣足食

成功响应,到此,旁门左道成功了一半。

接下来在之前创建的 chatCompletion 基础上添加针对 function calling 的 ToolCall 部分。

先准备好 ChangeState(bool newState) 的参数值

  1. Dictionary<string, bool> arguments = new()
  2. {
  3. { parameterName, true }
  4. };

并将回复内容由 "自己动手,丰衣足食" 改为 "客官,灯已开"

  1. Message = new ResponseMessage
  2. {
  3. Content = "客官,灯已开",
  4. Role = "assistant"
  5. }

然后为 chatCompletion 创建 ToolCalls 实例用于响应 function calling

  1. var messages = chatCompletionRequest.Messages;
  2. if (messages.First(x => x.Role == "user").Content.Contains("开灯") == true)
  3. {
  4. chatCompletion.Choices[0].Message.ToolCalls = new List<ToolCall>()
  5. {
  6. new ToolCall
  7. {
  8. Id = Guid.NewGuid().ToString(),
  9. Type = "function",
  10. Function = new FunctionCall
  11. {
  12. Name = function.Name,
  13. Arguments = JsonSerializer.Serialize(arguments, GetJsonSerializerOptions())
  14. }
  15. }
  16. };
  17. }

运行控制台程序看看效果

  1. User > 请开灯
  2. [开灯啦]
  3. [开灯啦]
  4. [开灯啦]
  5. [开灯啦]
  6. [开灯啦]
  7. Assistant > 客官,灯已开

耶!成功开灯!但是,竟然开了5次,差点把灯给开爆了。

BypassHandler 中打印一下请求内容看看哪里出了问题

  1. var json = await request.Content!.ReadAsStringAsync();
  2. Console.WriteLine(json);

原来分别请求/响应了5次,第2次请求开始,json 中 messages 部分多了 tool_callstool_call_id 内容

  1. {
  2. "messages": [
  3. {
  4. "content": "\u5BA2\u5B98\uFF0C\u706F\u5DF2\u5F00",
  5. "tool_calls": [
  6. {
  7. "function": {
  8. "name": "LightPlugin-ChangeState",
  9. "arguments": "{\u0022newState\u0022:true}"
  10. },
  11. "type": "function",
  12. "id": "76f8dead-b5ad-4e6d-b343-7f78d68fac8e"
  13. }
  14. ],
  15. "role": "assistant"
  16. },
  17. {
  18. "content": "on",
  19. "tool_call_id": "76f8dead-b5ad-4e6d-b343-7f78d68fac8e",
  20. "role": "tool"
  21. }
  22. ]
  23. }

这时恍然大悟,之前 AI assistant 对 function calling 的响应只是让 Plugin 执行对应的 function,assistant 还需要根据执行的结果决定下一下做什么,第2次请求中的 tool_callstool_call_id 就是为了告诉 assistant 执行的结果,所以,还需要针对这个请求进行专门的响应。

到了旁门左道最后100米冲刺的时刻!

RequestMessage 添加 ToolCallId 属性

  1. public class RequestMessage
  2. {
  3. [JsonPropertyName("role")]
  4. public string? Role { get; set; }
  5. [JsonPropertyName("name")]
  6. public string? Name { get; set; }
  7. [JsonPropertyName("content")]
  8. public string? Content { get; set; }
  9. [JsonPropertyName("tool_call_id")]
  10. public string? ToolCallId { get; set; }
  11. }

BypassHandler 中响应时判断一下 ToolCallId,如果是针对 Plugin 的 function 执行结果的请求,只返回 Message.Content,不进行 function calling 响应

  1. var messages = chatCompletionRequest.Messages;
  2. var toolCallId = "76f8dead- b5ad-4e6d-b343-7f78d68fac8e";
  3. var toolCallIdMessage = messages.FirstOrDefault(x => x.Role == "tool" && x.ToolCallId == toolCallId);
  4. if (toolCallIdMessage != null && toolCallIdMessage.Content == "on")
  5. {
  6. chatCompletion.Choices[0].Message.Content = "客官,灯已开";
  7. }
  8. else if (messages.First(x => x.Role == "user").Content.Contains("开灯") == true)
  9. {
  10. chatCompletion.Choices[0].Message.Content = "";
  11. //..
  12. }

改进代码完成,到了最后10米冲刺的时刻,再次运行控制台程序

  1. User > 请开灯
  2. [开灯啦]
  3. Assistant > 客官,灯已开

只有一次开灯,冲刺成功,旁门左道走通,用这种方式体验一下 Semantic Kernel Plugin,也别有一番风味。

完整示例代码已上传到 github https://github.com/cnblogs-dudu/sk-plugin-sample-101

补充:如果不使用 BypassHandler,直接接入 OpenAI,运行结果如下

  1. User > 请开灯
  2. [开灯啦]
  3. Assistant > 灯已经打开了。

原文链接:https://www.cnblogs.com/dudu/p/18018718

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号