探索Semantic Kernel内置插件:深入了解HttpPlugin的应用

简介: 其他Put和Delete类似。最后可以借鉴HttpPlugin的实现思路在项目中灵活的运行,如果不支持那就可以自定义插件来完成需求的开发,还是比较期待这个插件能够更加完善的一点,在未来以更灵活的方式支持Post等请求的多种形式。

前言

上一章我们熟悉了Semantic Kernel中的内置插件和对ConversationSummaryPlugin插件进行了实战,本章我们讲解一下另一个常用的内置插件HttpPlugin的应用。

上一章对ConversationSummaryPlugin总结进行了调整之后,顺便给Semantic Kernel提了一个PR已经被采纳了,在此记录一下!

.Net: refactor : SummarizeConversation #6719

HttpPlugin

HttpPlugin插件属于Native Plugins原生插件。它提供了Http的功能,允许用户通过Http协议与外部进行交互。

我们对这个插件的整体进行分析一下

构造函数

提供了两个构造函数。第一个构造函数没有参数,它调用了第二个构造函数,并传递null作为参数。

第二个构造函数接受一个HttpClient类型的参数,如果未提供,则使用HttpClientProvider.GetHttpClient()方法获取一个新的HttpClient实例。

public HttpPlugin() : this(null)
   {
   }
    [ActivatorUtilitiesConstructor]
    public HttpPlugin(HttpClient? client = null) =>
     this._client = client ?? HttpClientProvider.GetHttpClient();

这里重点说一下第二个构造函数,支持HttpClient的构造函数,这就有更多的可玩性了,比如可以定义一个HttpclientHandler对请求进行添加自定义的HttpHeader或者进行参数的拼接转发等操作。

Native functions

GetAsync:发送一个HTTP GET请求,并返回响应体作为字符串。

PostAsync:发送一个HTTP POST请求,带有请求体,并返回响应体作为字符串。

PutAsync:发送一个HTTP PUT请求,带有请求体,并返回响应体作为字符串。

DeleteAsync:发送一个HTTP DELETE请求,并返回响应体作为字符串。

实战

第一步需要安装Nuget

NuGet\Install-Package Microsoft.SemanticKernel.Plugins.Core -Version 1.14.1-alpha

该包目前只有预览版本,如果用VS的包管理器安装,那需要勾选包括预览发行版

Semantic Kernel注册插件有两种方式:

kernel.ImportPluginFromType<HttpPlugin>();
var httpclient = new HttpClient();
kernel.ImportPluginFromObject(new HttpPlugin(httpclient));

以上两种方式对应两种生命周期的注册

创建的接口

这个接口都很简单 对我们Student对象的增删改查

public class Student
{
 
    public string Name { get; set; }
    public int Age { get; set; }
}

执行测试

我们的测试程序还是以Semantic Kernel的会话服务自动触发function calling的形式

// Get chat completion service
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
// Start the conversation
Console.Write("User > ");
string? userInput;
while ((userInput = Console.ReadLine()) is not null)
{
    // Add user input
    history.AddUserMessage(userInput);
    // Enable auto function calling
    OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
    {
        ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions
    };
    // Get the response from the AI
    var result = await chatCompletionService.GetChatMessageContentAsync(
        history,
        executionSettings: openAIPromptExecutionSettings,
        kernel: kernel);
    // Print the results
    Console.WriteLine("Assistant > " + result);
    // Add the message from the agent to the chat history
    history.AddMessage(result.Role, result.Content ?? string.Empty);
    // Get user input again
    Console.Write("User > ");
}

Get请求测试

User > 帮我向https://localhost:7014/Student发一个get请求
Assistant > 向https://localhost:7014/Student发起GET请求后成功得到了响应,返回的数据显示包含了一个学生的信息。该学生名为 张三,年龄为16岁。这表明请求执行成功,获取到了预期的数据。

Post请求测试

HttpPlugin的这个功能比较鸡肋,可以看一下代码

[KernelFunction]
 [Description("Makes a POST request to a uri")]
 public Task<string> PostAsync([Description("The URI of the request")] string uri, [Description("The body of the request")] string body, CancellationToken cancellationToken = default(CancellationToken))
 {
     return SendRequestAsync(uri, HttpMethod.Post, new StringContent(body), cancellationToken);
 }

参数形式是new StringContent(body),也就是说MediaTypeHeaderValue媒体类型默认为 StringContent text/plain

Asp.Net Core 只能接收Post请求json格式的string,不能接收原始string

content-typetext/plainpost请求,如果支持需要自定义实现没有提供对应的MediaTypeFormatter

所以说这个插件的Post请求场景局限,真正用到生产还需要自己去实现一个插件!!!

User > 向https://localhost:7014/student 发一个post请求
Assistant > 已成功向 https://localhost:7014/student 发送了 POST 请求。如果需要发送具体的数据,请提供要包含在请求体内的 JSON 数据。

其他

PutDelete类似。

最后

可以借鉴HttpPlugin的实现思路在项目中灵活的运行,如果不支持那就可以自定义插件来完成需求的开发,还是比较期待这个插件能够更加完善的一点,在未来以更灵活的方式支持Post等请求的多种形式。


相关文章
|
9月前
|
JavaScript API
如何使用 TypeScript 的 module augmentation 技术增强 Spartacus Feature Library
如何使用 TypeScript 的 module augmentation 技术增强 Spartacus Feature Library
|
11月前
|
人工智能 自然语言处理 算法
UIE: Unified Structure Generation for Universal Information Extraction 论文解读
信息提取受到其不同目标、异构结构和特定需求模式的影响。本文提出了一个统一的文本到结构生成框架,即UIE,该框架可以对不同的IE任务进行统一建模,自适应生成目标结构
347 0
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
One SPRING to Rule Them Both Symmetric AMR Semantic Parsing and Generation without Complex Pipeline
在文本到AMR解析中,当前最先进的语义解析器集成了几个不同模块或组件的繁琐管道,并利用图重新分类,即在训练集的基础上开发的一组特定内容的启发式方法。
104 0
|
机器学习/深度学习 编解码 文字识别
Hybrid Multiple Attention Network for Semantic Segmentation in Aerial Images(二)
Hybrid Multiple Attention Network for Semantic Segmentation in Aerial Images
162 0
Hybrid Multiple Attention Network for Semantic Segmentation in Aerial Images(二)
|
机器学习/深度学习 编解码 文字识别
Hybrid Multiple Attention Network for Semantic Segmentation in Aerial Images(一)
Hybrid Multiple Attention Network for Semantic Segmentation in Aerial Images
132 0
Hybrid Multiple Attention Network for Semantic Segmentation in Aerial Images(一)
|
Oracle 关系型数据库 Unix
Linux Kernel Lowmem Pressure Issues and Related Kernel Structures (Doc ID 452326.1)
Linux Kernel Lowmem Pressure Issues and Related Kernel Structures (Doc ID 452326.1)
110 0
|
开发工具
修复VirtualBox "This kernel requires the following features not present on the CPU: pae Unable to boot – please use a kernel appropriate for your CPU"
异常处理汇总-开发工具  http://www.cnblogs.com/dunitian/p/4522988.html 修复VirtualBox "This kernel requires the following features not present on the CPU: pae Una...
996 0