使用 SK Plugin 给 LLM 添加能力
前几篇我们介绍了如何使用 SK + ollama 跟 LLM 进行基本的对话。如果只是对话的话其实不用什么 SK 也是可以的。今天让我们给 LLM 整点活,让它真的给我们干点啥。
What is Plugin?
Plugins are a key component of Semantic Kernel. If you have already used plugins from ChatGPT or Copilot extensions in Microsoft 365, you’re already familiar with them. With plugins, you can encapsulate your existing APIs into a collection that can be used by an AI. This allows you to give your AI the ability to perform actions that it wouldn’t be able to do otherwise.
Behind the scenes, Semantic Kernel leverages function calling, a native feature of most of the latest LLMs to allow LLMs, to perform planning and to invoke your APIs. With function calling, LLMs can request (i.e., call) a particular function. Semantic Kernel then marshals the request to the appropriate function in your codebase and returns the results back to the LLM so the LLM can generate a final response.
以上是微软文档的原话。说人话:
Plugins 是 SK 的关键组件。基于 Plugins 你可以封装已有的 API 给 AI 使用。这给了 AI 执行动作的能力。在背后,SK 利用了大多数最新大语言模型 (LLMs) 的本地功能调用功能,使LLMs能够进行规划并调用您的API。通过功能调用,LLMs可以请求(即调用)特定的函数。然后,Semantic Kernel 将请求传递给代码库中的相应函数,并将结果返回给LLM,以便LLM生成最终的响应。
说的更直白一点,Plugins 给 LLM 提供了方法调用的能力。这就比较有意思了。我们知道 LLM 是基于过往的内容训练出来的,也就是说 LLM 并不能回答当前的一些信息,因为它不知道。比如你问它今天有什么新闻,它肯定不知道,因为这不在它的训练集里面。或者你问它今天天气怎么样它也不知道。同样它也没有办法给你做一些特定领域的事情,比如你让他们给某某发一条短信,它做不到,因为它没有这个能力。但是现在有了 Plugin,这一切就有了可能。
以下让我们使用 SK + Plugin 给 LLM 添加感知天气的能力。当我们问 LLM 某个城市的天气的时候,它能精确的给出回答。
在我们开始之前还是先试试直接问 LLM 天气问题会得到什么结果:

可以看到当我问 What is the weather now of SuZhou ? 后 LLM 直接说它不能获得实时数据。
定义 WeatherPlugin
using System.ComponentModel;
using Microsoft.SemanticKernel;
namespace SKLearning
{
public sealed class WeatherPlugin
{
[KernelFunction, Description("Gets the weather details of a given location")]
[return: Description("Weather details")]
public static async Task<string> GetWeatherByLocation([Description("name of the location")]string location)
{
var key = "...";
var url = @$"http://api.weatherapi.com/v1/current.json?key={key}&q={location}";
using var client = new HttpClient();
var response = await client.GetAsync(url);
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
return content;
}
}
}
一个 plugin 就是一个 C# 的类。在这个类里面承载了一个或N个方法。我们给方法添加描述,给入参,出参添加描述好让 LLM 认识这个方法的作用。这个描述非常重要,请使用尽量简洁明了的语言。
我们可以看到 WeatherPlugin 里的 GetWeatherByLocation 是通过要给 API 实时获取某个城市的天气信息。这个返回值是 JSON 格式的,并不需要进行特殊的处理,LLM 可以直接识别里面的内容。
添加 Plugin 到 kernel
var httpClient = new HttpClient();
var builder = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(modelId: modelId!, apiKey: null, endpoint: endpoint, httpClient: httpClient);
var kernel = builder.Build();
kernel.Plugins.AddFromType<WeatherPlugin>();
以上代码片段演示了如何添加 OpenAI 的 Chat 服务以及如何添加 Plugin 。
与 AI 对话
var settings = new OpenAIPromptExecutionSettings()
{ Temperature = 0.0, ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();
var initMessage =
"I am a weatherman. I can tell you the weather of any location. Try asking me about the weather of a location.";
history.AddSystemMessage(initMessage);
Console.WriteLine(initMessage);
while (true)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.Write("You:");
var input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input))
{
break;
}
history.AddUserMessage(input);
// Get the response from the AI
var contents = await chatCompletionService.GetChatMessageContentsAsync(history, settings, kernel);
foreach (var chatMessageContent in contents)
{
var content = chatMessageContent.Content;
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.WriteLine($"AI: {content}");
history.AddMessage(chatMessageContent.Role, content ?? "");
}
}
以上内容跟上次演示的内容没啥特别大的区别,无非就是读取用户的输入,等待 LLM 的回答。
试一下
让我们运行程序,然后再次问 What is the weather now of SuZhou?
可以看到 LLM 精确的回答出了当前苏州的天气:
I am a weatherman. I can tell you the weather of any location. Try asking me about the weather of a location.
You: What is the weather now of SuZhou?
AI: The current weather in Suzhou, China is patchy light drizzle with a temperature of 17.2°C (62.9°F). The wind is blowing at 3.6 mph (5.8 kph) from the NNE direction. The humidity is 86% and the visibility is 5 km (3 miles).

总结
通过以上演示,我们自定义了一个实时获取天气信息的 plugin 给 LLM 使用。当我们问到天气信息的时候 LLM 会实时调用这个方法,然后使用方法结果构造一个可读性非常高的回答。有了 plugin 之后 LLM 真正的可以触发一些动作,执行一些任务,获取实时信息了。
希望此文对你有所帮助,谢谢。
使用 SK Plugin 给 LLM 添加能力的更多相关文章
- Semantic Kernel 入门系列:🔥Kernel 内核和🧂Skills 技能
理解了LLM的作用之后,如何才能构造出与LLM相结合的应用程序呢? 首先我们需要把LLM AI的能力和原生代码的能力区分开来,在Semantic Kernel(以下简称SK),LLM的能力称为 sem ...
- Eclipse RCP /Plugin移除Search对话框
RCP:如何移除Search对话框中不需要的项 2013-08-18 22:31 by Binhua Liu, 231 阅读, 0 评论, 收藏, 编辑 前言 很久没写文章了,准备写一系列关于Ecli ...
- MySQL审计工具Audit Plugin安装使用
本实验的审计插件均是安装在 mysql-community-server-5.7.9 的服务器上. 插件安装(社区版) 插件下载地址: https://bintray.com/mcafee/mysql ...
- Semantic Kernel 知多少 | 开启面向AI编程新篇章
引言 在ChatGPT 火热的当下, 即使没有上手亲自体验,想必也对ChatGPT的强大略有耳闻.当一些人在对ChatGPT犹犹豫豫之时,一些敏锐的企业主和开发者们已经急不可耐的开展基于ChatGPT ...
- .NET周报 【4月第2期 2023-04-08】
国内文章 LRU缓存替换策略及C#实现 https://www.cnblogs.com/eventhorizon/p/17290125.html 这篇文章讲述了缓存替换策略,特别是LRU算法.LRU算 ...
- JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
JAVA WEB快速入门系列之前的相关文章如下:(文章全部本人[梦在旅途原创],文中内容可能部份图片.代码参照网上资源) 第一篇:JAVA WEB快速入门之环境搭建 第二篇:JAVA WEB快速入门之 ...
- collectd+influxDB+Grafana搭建性能监控平台
网上查看了很多关于环境搭建的文章,都比较久远了很多安装包源都不可用了,今天收集了很多资料组合尝试使用新版本来搭建,故在此记录. 采集数据(collectd)-> 存储数据(influxdb) - ...
- Maven中使用Jetty容器
1.在pom.xml中添加Jetty的插件 <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId ...
- 浅谈Nutch插件机制(含开发实例)
plugin(插件)为nutch提供了一些功能强大的部件,举个例子,HtmlParser就是使用比较普遍的用来分析nutch抓取的html文件的插件. 为什么nutch要使用这样的plugin系统? ...
- 基于Jenkins自动构建系统开发
1 绪论 1.1 课题的研究背景 随着IT行业的不断发展,软件开发的复杂度也随着不断提高.与此同时,软件的开发团队也越来越庞大,而如何更好地协同整个团队进行高效准确的工作,从而确保软件开发的质量已经 ...
随机推荐
- go:极简上手使用 stretchr/testify 进行mock测试
库安装 首先,安装 Mock 类生成工具 Mockery: go install github.com/vektra/mockery/v2@v2.45.1 实际上,你也可以手动创建 Mock 类. 生 ...
- Value error: 'ascii' codec can't decode byte 0xe6 in position 26: ordinal not in range(128) The traceback for the exception was written to the log file
原因:工作空间中有中文编码问题,导致的运行ros异常 解决办法: 1.解决urdf生成异常问题 urdf文件中不允许有中文,所有当输入中文的时候容易出问题,解决方案: ①在根目录下:/opt/ros/ ...
- WSGI、mini-web框架
阅读目录: 1.服务器动态资源请求 2.应用程序示例 3.Web 动态服务器 4.mini-web框架-1-文件结构 5.mini-web框架-2-显示页面 6.mini-web框架-3-替换模板 一 ...
- Selenium弹框处理
Selenium中有三种弹框,本文介绍了处理三种弹框的方法 一.Selenium三种弹框 alert:用来提示,显示一个带有指定消息和确认按钮的警告框 confirm:用于确认,显示一个带有指定消息和 ...
- ConsulManager应用场景2:如何优雅的使用Consul管理Blackbox站点监控
[ConsulManager介绍] Consul字段设计说明 所有数据存在一个名为blackbox_exporter的Services项中,每个监控目标为一个子Service. 每个Service使用 ...
- 在线激活win
目前DragonKMS神龙版能激活win11.win10.win8/8.1.win7以及server2008/2012/2016/2019/2022等系统版本,其中包括:专业工作站版.企业版.专业版. ...
- .NET +Vue 开发的高级报表工具
前言 本文介绍一款基于 .NET 6 开发的高级报表工具.该工具支持多种数据源(如数据库.Excel 文件.API 服务等),并具备强大的数据处理和可视化功能.通过内置的集合函数和类 Excel 界面 ...
- Metasploit会话连接不稳定问题排查
使用msfvenom生成木马,语句如下: msfvenom -p windows/x64/meterpreter_reverse_tcp lhost=43.154.xxx.xxx lport=4455 ...
- ABP-VNext 用户权限管理系统实战06---多租户集成
一.集成 1.引用安装包 Volo.Abp.AspNetCore.MultiTenancy 2.配置租户key Configure<AbpAspNetCoreMultiTenancyOption ...
- think in java interview-高级开发人员面试宝典(二)
从现在开始,以样题的方式一一列出各种面试题以及点评,考虑到我在前文中说的,对于一些大型的外资型公司,你将会面临全程英语面试,因此我在文章中也会出现许多全英语样题. 这些题目来自于各个真实的公司,公司名 ...