您的位置:首頁 > 軟件教程 > 教程 > 深度解析Semantic Kernel中的HttpPlugin插件

深度解析Semantic Kernel中的HttpPlugin插件

來源:好特整理 | 時間:2024-06-25 09:48:47 | 閱讀:98 |  標簽: T El K S C TT in HTTP 應用 ug   | 分享到:

前言 上一章我們熟悉了Semantic Kernel中的內置插件和對ConversationSummaryPlugin插件進行了實戰(zhàn),本章我們講解一下另一個常用的內置插件HttpPlugin的應用。 上一章對ConversationSummaryPlugin總結進行了調整之后,順便給Semantic

上一章我們熟悉了Semantic Kernel中的內置插件和對ConversationSummaryPlugin插件進行了實戰(zhàn),本章我們將深入探討另一個常用的內置插件HttpPlugin的應用。

上一章對ConversationSummaryPlugin總結進行了調整之后,順便給Semantic Kernel提了一個PR已經被采納了,在此記錄一下!

深度解析Semantic Kernel中的HttpPlugin插件

.Net: refactor : SummarizeConversation #6719

HttpPlugin

HttpPlugin插件屬于Native Plugins原生插件。它提供了Http的功能,允許用戶通過Http協(xié)議與外部進行交互。

我們對這個插件的整體進行分析一下

構造函數(shù)

提供了兩個構造函數(shù)。第一個構造函數(shù)沒有參數(shù),它調用了第二個構造函數(shù),并傳遞null作為參數(shù)。

第二個構造函數(shù)接受一個HttpClient類型的參數(shù),如果未提供,則使用HttpClientProvider.GetHttpClient()方法獲取一個新的HttpClient實例。

這里重點說一下第二個構造函數(shù),支持HttpClient的構造函數(shù),這就有更多的可玩性了,比如可以定義一個HttpclientHandler對請求進行添加自定義的HttpHeader或者進行參數(shù)的拼接轉發(fā)等操作。

Native functions

GetAsync:發(fā)送一個HTTP GET請求,并返回響應體作為字符串。
PostAsync:發(fā)送一個HTTP POST請求,帶有請求體,并返回響應體作為字符串。
PutAsync:發(fā)送一個HTTP PUT請求,帶有請求體,并返回響應體作為字符串。
DeleteAsync:發(fā)送一個HTTP DELETE請求,并返回響應體作為字符串。

實戰(zhàn)

第一步需要安裝Nuget包

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

該包目前只有預覽版本,如果用VS的包管理器安裝,那需要勾選包括預覽發(fā)行版

Semantic Kernel注冊插件有兩種方式:

kernel.ImportPluginFromType();

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

以上兩種方式對應兩種生命周期的注冊

創(chuàng)建的接口

深度解析Semantic Kernel中的HttpPlugin插件

這個接口都很簡單 對我們Student對象的增刪改查

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

執(zhí)行測試

我們的測試程序還是以Semantic Kernel的會話服務,自動觸發(fā)function calling的形式

// Get chat completion service var chatCompletionService = kernel.GetRequiredService(); // 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發(fā)一個get請求 Assistant > 向https://localhost:7014/Student發(fā)起GET請求后成功得到了響應,返回的數(shù)據(jù)顯示包含了一個學生的信息。該學生名為 張三,年齡為16歲。這表明請求執(zhí)行成功,獲取到了預期的數(shù)據(jù)。

Post請求測試

HttpPlugin的這個功能比較雞肋,可以看一下代碼

[KernelFunction] [Description("Makes a POST request to a uri")] public Task 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); }

參數(shù)形式是new StringContent(body),也就是說MediaTypeHeaderValue媒體類型默認為StringContent text/plain。

Asp.Net Core只能接收Post請求json格式的string,不能接收原始string
即content-type為text/plain的post請求,如果支持需要自定義實現(xiàn)沒有提供對應的MediaTypeFormatter。

所以說這個插件的Post請求場景局限,真正用到生產還需要自己去實現(xiàn)一個插件。!

User > 向https://localhost:7014/student 發(fā)一個post請求 Assistant > 已成功向 https://localhost:7014/student 發(fā)送了 POST 請求。如果需要發(fā)送具體的數(shù)據(jù),請?zhí)峁┮谡埱篌w內的 JSON 數(shù)據(jù)。

其他

Put和Delete類似。

最后

可以借鑒HttpPlugin的實現(xiàn)思路在項目中靈活的運行,如果不支持那就可以自定義插件來完成需求的開發(fā),還是比較期待這個插件能夠更加完善的一點,在未來以更靈活的方式支持Post等請求的多種形式。

小編推薦閱讀

好特網發(fā)布此文僅為傳遞信息,不代表好特網認同期限觀點或證實其描述。

K
K
類型:角色扮演  運營狀態(tài):封測  語言:中文   

游戲攻略

游戲禮包

游戲視頻

游戲下載

游戲活動

《K》是由樂次元開發(fā)的一款日系動漫RPG游戲,游戲根據(jù)同名動漫改編而來,高水準的漫畫和音樂是這款游戲的

相關視頻攻略

更多

掃二維碼進入好特網手機版本!

掃二維碼進入好特網微信公眾號!

本站所有軟件,都由網友上傳,如有侵犯你的版權,請發(fā)郵件[email protected]

湘ICP備2022002427號-10 湘公網安備:43070202000427號© 2013~2024 haote.com 好特網