前几篇我们介绍了如何使用 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 添加能力的更多相关文章

  1. Semantic Kernel 入门系列:🔥Kernel 内核和🧂Skills 技能

    理解了LLM的作用之后,如何才能构造出与LLM相结合的应用程序呢? 首先我们需要把LLM AI的能力和原生代码的能力区分开来,在Semantic Kernel(以下简称SK),LLM的能力称为 sem ...

  2. Eclipse RCP /Plugin移除Search对话框

    RCP:如何移除Search对话框中不需要的项 2013-08-18 22:31 by Binhua Liu, 231 阅读, 0 评论, 收藏, 编辑 前言 很久没写文章了,准备写一系列关于Ecli ...

  3. MySQL审计工具Audit Plugin安装使用

    本实验的审计插件均是安装在 mysql-community-server-5.7.9 的服务器上. 插件安装(社区版) 插件下载地址: https://bintray.com/mcafee/mysql ...

  4. Semantic Kernel 知多少 | 开启面向AI编程新篇章

    引言 在ChatGPT 火热的当下, 即使没有上手亲自体验,想必也对ChatGPT的强大略有耳闻.当一些人在对ChatGPT犹犹豫豫之时,一些敏锐的企业主和开发者们已经急不可耐的开展基于ChatGPT ...

  5. .NET周报 【4月第2期 2023-04-08】

    国内文章 LRU缓存替换策略及C#实现 https://www.cnblogs.com/eventhorizon/p/17290125.html 这篇文章讲述了缓存替换策略,特别是LRU算法.LRU算 ...

  6. JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识

    JAVA WEB快速入门系列之前的相关文章如下:(文章全部本人[梦在旅途原创],文中内容可能部份图片.代码参照网上资源) 第一篇:JAVA WEB快速入门之环境搭建 第二篇:JAVA WEB快速入门之 ...

  7. collectd+influxDB+Grafana搭建性能监控平台

    网上查看了很多关于环境搭建的文章,都比较久远了很多安装包源都不可用了,今天收集了很多资料组合尝试使用新版本来搭建,故在此记录. 采集数据(collectd)-> 存储数据(influxdb) - ...

  8. Maven中使用Jetty容器

    1.在pom.xml中添加Jetty的插件 <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId ...

  9. 浅谈Nutch插件机制(含开发实例)

    plugin(插件)为nutch提供了一些功能强大的部件,举个例子,HtmlParser就是使用比较普遍的用来分析nutch抓取的html文件的插件. 为什么nutch要使用这样的plugin系统? ...

  10. 基于Jenkins自动构建系统开发

    1  绪论 1.1 课题的研究背景 随着IT行业的不断发展,软件开发的复杂度也随着不断提高.与此同时,软件的开发团队也越来越庞大,而如何更好地协同整个团队进行高效准确的工作,从而确保软件开发的质量已经 ...

随机推荐

  1. springboot实现验证码校验

    验证码校验共三步 1.创建验证码 2.发送验证码 3.验证码校验 创建生成验证码的工具类 public class RandomValidateCode { private Random random ...

  2. Git仓库操作笔记[Git repositories]

    指令操作:Git Bash 重点:远程库和本地库之间操作 首先得在github网站 上创建一个 repository ,才能同步. 命令前提是进入githubLib 下的当前目录. 1.从远程库克隆到 ...

  3. 20240719 CVTE 笔试

    岗位:嵌入式软件开发工程师(Linux方向) 题型:20 道不定项选择题,2 道编程题 1.不定项选择题 1.1 如下哪个命令可以帮助你知道 shell 的用法 (D) more help pwd m ...

  4. 痞子衡嵌入式:利用i.MXRT10xx系列内部DCP引擎计算CRC32值时需注意数据对齐

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是利用i.MXRT10xx系列内部DCP引擎计算CRC32值时需注意数据对齐. MCU 开发里常常需要 CRC 校验来检查数据完整性,CR ...

  5. STATA数据统计软件学习记录

    STATA是一个数据统计软件,正如它的名字一样,STATA=statistic+data.STATA软件的功能和matlab类似,也可以用代码实现数据的统计与可视化.但几乎只能进行整行整列的数据处理, ...

  6. select2的搜索框不能输入内容

    select2的搜索框不能输入内容 原因:原来是模态对话框强制使自己处于焦点状态,导致select2的搜索框无法获取焦点所致. 解决办法:在初始化中重写模态对话框的enforceFocus函数 $.f ...

  7. Java的内存管理1:“并不只有C++程序员关心内存回收”——Java的内存管理2:"不中用的finalize( )方法"

    通常Java的缓存管理会由垃圾回收器(Java Garbage Collection)定时处理,无须程序员操心.但Java Garbage Collection仅有权回收那些非"强引用&qu ...

  8. 安装cnpm时报错

    报错:npm WARN deprecated socks@1.1.10: If using 2.x branch, please upgrade to at least 2.1.6 to avoid ...

  9. F650A光猫的一些命令(一)

    查看有 / # uname -a Linux F650A 4.1.25 #12 SMP Tue Aug 15 21:57:30 CST 2017 armv7l GNU/Linux / # cat /p ...

  10. 2019-2020 ACM-ICPC Brazil Subregional Programming Contest

    D. Denouncing Mafia 给定一颗树,然后给定\(k\)个起点,对于每个起点来说,从该点到根节点的一条链都会被染色,求最多有几个点会被染色 \(3 \leq n \leq 1e5, 1 ...