在一个 Semantic Kernel plugin 中可以创建两种类型的 function,分别是 native function 与 prompt function(之前叫 semantic function)。
下面这款 plugin 中给 C# method 添加了 [KernelFunction]
attribute,就是 native function
public class LightPlugin
{
public bool IsOn { get; set; } = false;
[KernelFunction]
[Description("帮看一下灯是开是关")]
public string GetState() => IsOn ? "on" : "off";
}
今天学习的是另一款 plugin, 是基于 prompt function 实现的。
写个简单的示例 plugin 体验一下,其中的提示词来自博文 Intro to Semantic Kernel – Part One
创建控制台项目
dotnet new consoele
dotnet add package Microsoft.SemanticKernel
创建 Plugin
plugin 名称为 DevOps
,prompt function 名称为 GenerateKubernetesYaml
,创建对应的文件夹,文件夹名称分别对应 plugin name 与 function name
mkdir -p Plugins/DevOps/GenerateKubernetesYaml
在文件夹中分别添加 skprompt.txt 与 config.json 文件
提示词配置文件 skprompt.txt
,需要输入的变量是 input
INSTRUCTIONS:
Generate YAML files to create a kubernetes deployment according to the given description.
RULES:
- All YAML files must be complete
- YAML files must be listed one by one
- Every file is indicated with "FILE:" followed by its path
- Every file content must begin and end with #----#
DESCRIPTION:{{$input}}
请求大模型 api 的配置文件 config.json
{
"schema": 1,
"description": "Create kubernetes YAML files for given devops task",
"type": "completion",
"completion": {
"max_tokens": 1000,
"temperature": 0.5,
"top_p": 1,
"presence_penalty": 0,
"frequency_penalty": 0
},
"input": {
"parameters": [
{
"name": "input",
"description": "The description of the deployment to be be created",
"defaultValue": "keycloak with postgres backend and prod mode activated. nginx-ingress is used to forward calls to keycloak. ingress uses tls termination."
}
]
}
}
1个文件夹,2个文件,plugin 就创建好了,就这么简单,提示词 + 请求参数。
添加 Plugin
调用 AddFromPromptDirectory
方法添加 plugin,注意参数中的路径是 plugin 的路径,不是 prompt function 的路径
var builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("gpt-3.5-turbo", apiKey);
builder.Plugins.AddFromPromptDirectory("Plugins/DevOps");
var kernel = builder.Build();
控制台输出 plugin 信息,看看插件是否已成功添加
foreach (var plugin in kernel.Plugins)
{
Console.WriteLine("plugin: " + plugin.Name);
foreach (var function in plugin)
{
Console.WriteLine(" - prompt function: " + function.Name);
}
}
输出如下
plugin: DevOps
- prompt function: GenerateKubernetesYaml
插件添加成功。
运行 Plugin
调用 Kernel.InvokeAsync
方法运行插件,需要传3个参数:
- plugin name
- function name
- prompt 中用到的变量值
代码如下
var input = """
Deploy keycloak (quarkus variant) that uses mysql as its backend.
Keycloak runs in prod mode and is TLS secured with a self-signed certificate.
Use images from bitnami.
""";
var result = await kernel.InvokeAsync(
pluginName: "DevOps",
functionName: "GenerateKubernetesYaml",
arguments: new() {
{ "input", input}
});
Console.WriteLine(result);
Plugin 运行成功,输出结果很长,详见 https://www.cnblogs.com/dudu/articles/18030965
#----#
FILE: keycloak-mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: keycloak-mysql-deployment
...
笔记完成,学习完成。
标签:function,Kernel,prompt,name,plugin,input,Semantic From: https://www.cnblogs.com/dudu/p/18030425