前言
上一章我们熟悉了Semantic Kernel中的内置插件和对ConversationSummaryPlugin插件进行了实战,本章我们讲解一下另一个常用的内置插件HttpPlugin的应用。
上一章对
ConversationSummaryPlugin总结进行了调整之后,顺便给Semantic Kernel提了一个PR已经被采纳了,在此记录一下!

.Net: refactor : SummarizeConversation #6719
HttpPlugin
HttpPlugin插件属于Native Plugins原生插件。它提供了Http的功能,允许用户通过Http协议与外部进行交互。
我们对这个插件的整体进行分析一下
构造函数
提供了两个构造函数。第一个构造函数没有参数,它调用了第二个构造函数,并传递null作为参数。
第二个构造函数接受一个HttpClient类型的参数,如果未提供,则使用HttpClientProvider.GetHttpClient()方法获取一个新的HttpClient实例。
c#
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 包
package
NuGet\Install-Package Microsoft.SemanticKernel.Plugins.Core -Version 1.14.1-alpha
该包目前只有预览版本,如果用VS的包管理器安装,那需要勾选
包括预览发行版
Semantic Kernel注册插件有两种方式:
c#
kernel.ImportPluginFromType<HttpPlugin>();
c#
var httpclient = new HttpClient();
kernel.ImportPluginFromObject(new HttpPlugin(httpclient));
以上两种方式对应两种生命周期的注册
创建的接口

这个接口都很简单 对我们Student对象的增删改查
c#
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
执行测试
我们的测试程序还是以Semantic Kernel的会话服务,自动触发function calling的形式
c#
// 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请求测试
text
User > 帮我向https://localhost:7014/Student发一个get请求
Assistant > 向https://localhost:7014/Student发起GET请求后成功得到了响应,返回的数据显示包含了一个学生的信息。该学生名为 张三,年龄为16岁。这表明请求执行成功,获取到了预期的数据。
Post请求测试
HttpPlugin的这个功能比较鸡肋,可以看一下代码
c#
[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-type为text/plain的post请求,如果支持需要自定义实现没有提供对应的MediaTypeFormatter。
所以说这个插件的
Post请求场景局限,真正用到生产还需要自己去实现一个插件!!!
text
User > 向https://localhost:7014/student 发一个post请求
Assistant > 已成功向 https://localhost:7014/student 发送了 POST 请求。如果需要发送具体的数据,请提供要包含在请求体内的 JSON 数据。
其他
Put和Delete类似。
最后
可以借鉴HttpPlugin的实现思路在项目中灵活的运行,如果不支持那就可以自定义插件来完成需求的开发,还是比较期待这个插件能够更加完善的一点,在未来以更灵活的方式支持Post等请求的多种形式。