前几篇我们介绍了如何使用 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. Golang 开源库分享:anko - 给 Go 加点“脚本魔法”

    GitHub 仓库链接:https://github.com/mattn/anko 1. anko 是干嘛用的? anko 是一个可以让 Go 项目支持脚本语言的小工具.换句话说,就是我们可以给 Go ...

  2. php如何快速入门

    PHP交流群  656679284  为PHP广大爱好者提供技术交流,有问必答,相互学习相互进步! 学习教程 学习前期,首要的WEB前端基础知识,比如html5/css3/java/jquery有个简 ...

  3. php操作sqlite3

    距离上次接触sqlite3已经快一年了,去年这篇文章讲跟着菜鸟教程学python操作sqlite3,https://www.cnblogs.com/lizhaoyao/p/13717381.html ...

  4. 报错处理TypeError: can't multiply sequence by non-int of type 'float'

    在练习格式化输出时出现错误TypeError: can't multiply sequence by non-int of type 'float' 为什么会出现TypeError:不能将序列乘以'f ...

  5. 【昌哥IT课堂】MySQL8.0新特性之不可见主键

    一.概述作为 MySQL DBA ,相信大家都经历过在复制模式下,如果没有主键,遇到 load data ,大事务,ddl 等有大量表数据行扫描的行为时,会带来严重的主从延迟,给数据库稳定性和数据一致 ...

  6. Think in Java之构造器的真正调用顺序

    构造器是OOP的重要组成部分,很多人认为它很容易.只不过是new了一个对象而已.而think in java的作者却告诉我们,其实这并不容易.先看下面这个例子.在你没看结果之前,你觉得你的答案是对的么 ...

  7. README.md书写范例

    具体参考: https://learnku.com/docs/laravel-specification/5.5/readme-examplemd/523

  8. manim边做边学--曲面

    Surface类是Manim中专为创建和操控复杂的三维表面而打造的. 在实际应用中,无论是创建数学教学中的几何模型,还是模拟物理现象中的曲面变化,甚至是构建复杂的动画场景中的三维元素,Surface类 ...

  9. 构建你的.NET Aspire解决方案

    .NET Aspire 是一组功能强大的工具.模板和包,用于构建可观察的生产就绪应用程序..NET Aspire 通过处理特定云原生问题的 NuGet 包集合提供.云原生应用程序通常由小型互连部分或微 ...

  10. kubeadm 部署k8s

    kubeadm 部署k8s 闲聊 考虑了很久,打算写一篇保姆级部署从0-1构建企业级cicd流水线,把工作上面所用到的技术点分享给大家.从最k8s,harbor,jenkins,gitlab,dock ...