����λ�ã���ҳ > �����̳� > �̳� > ��Ƚ���Semantic Kernel�е�HttpPlugin���

��Ƚ���Semantic Kernel�е�HttpPlugin���

��Դ������������|��ʱ�䣺2024-06-25 09:48:47 |���Ķ���58��|�� ��ǩ�� T El K S C TT in HTTP Ӧ�� ug �� |����������

ǰ�� ��һ��������Ϥ��Semantic Kernel�е����ò���Ͷ�ConversationSummaryPlugin���������ʵս���������ǽ���һ����һ�����õ����ò��HttpPlugin��Ӧ�á� ��һ�¶�ConversationSummaryPlugin�ܽ�����˵���֮��˳���Semantic

��һ��������Ϥ��Semantic Kernel�е����ò���Ͷ�ConversationSummaryPlugin���������ʵս���������ǽ�����̽����һ�����õ����ò��HttpPlugin��Ӧ�á�

��һ�¶�ConversationSummaryPlugin�ܽ�����˵���֮��˳���Semantic Kernel����һ��PR�Ѿ��������ˣ��ڴ˼�¼һ�£�

Éî¶È½âÎöSemantic KernelÖеÄHttpPlugin²å¼þ

.Net: refactor : SummarizeConversation #6719

HttpPlugin

HttpPlugin�������Native Pluginsԭ����������ṩ��Http�Ĺ��ܣ������û�ͨ��HttpЭ�����ⲿ���н�����

���Ƕ���������������з���һ��

���캯��

�ṩ���������캯������һ�����캯��û�в������������˵ڶ������캯����������null��Ϊ������

�ڶ������캯������һ��HttpClient���͵IJ��������δ�ṩ����ʹ��HttpClientProvider.GetHttpClient()������ȡһ���µ�HttpClientʵ����

�����ص�˵һ�µڶ������캯����֧��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();

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

�������ַ�ʽ��Ӧ�����������ڵ�ע��

�����Ľӿ�

Éî¶È½âÎöSemantic KernelÖеÄHttpPlugin²å¼þ

����ӿڶ��ܼ� ������Student�������ɾ�IJ�

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

ִ�в���

���ǵIJ��Գ�������Semantic Kernel�ĻỰ�����Զ�����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��һ��get���� Assistant > ��https://localhost:7014/Student����GET�����ɹ��õ�����Ӧ�����ص�������ʾ������һ��ѧ������Ϣ����ѧ����Ϊ ����������Ϊ16�ꡣ���������ִ�гɹ�����ȡ����Ԥ�ڵ����ݡ�

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); }

������ʽ��new StringContent(body)��Ҳ����˵MediaTypeHeaderValueý������Ĭ��ΪStringContent text/plain��

Asp.Net Coreֻ�ܽ���Post����json��ʽ��string�����ܽ���ԭʼstring
��content-typeΪtext/plain��post�������֧����Ҫ�Զ���ʵ��û���ṩ��Ӧ��MediaTypeFormatter��

����˵��������Post���󳡾����ޣ������õ���������Ҫ�Լ�ȥʵ��һ�����������

User > ��https://localhost:7014/student ��һ��post���� Assistant > �ѳɹ��� https://localhost:7014/student ������ POST ���������Ҫ���;�������ݣ����ṩҪ�������������ڵ� JSON ���ݡ�

����

Put��Delete���ơ�

���

���Խ��HttpPlugin��ʵ��˼·����Ŀ���������У������֧���ǾͿ����Զ��������������Ŀ��������DZȽ��ڴ��������ܹ��������Ƶ�һ�㣬��δ���Ը����ķ�ʽ֧��Post������Ķ�����ʽ��

С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

K
K
���ͣ���ɫ����������Ӫ״̬����⡡�����ԣ����� ����

��Ϸ����

��Ϸ���

��Ϸ��Ƶ

��Ϸ����

��Ϸ�

��K�������ִ�Ԫ������һ����ϵ����RPG��Ϸ����Ϸ����ͬ�������ı��������ˮ׼�������������������Ϸ��

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������