Semantic Kernel入门系列:利用YAML定义prompts functions

引言

在上一章节我们熟悉了prompts functions(提示函数)的创建,我们了解了PromptTemplateConfig中各个属性的简单使用。Semantic Kernel允许我们利用多种方式去创建prompts包括native functions,prompts functions或者也叫Semantic functions,和Yaml 文件等。

本章的我们将学习利用Yaml的格式来定义prompts functionsYAML 是一种结构化数据格式,通过使用它,我们可以将提示的不同部分集中在一个地方,更好地组织和管理代码。这种方法可以提高代码的可读性和维护性,使得对提示模板的修改和更新变得更加简单和高效。

实战

还是跟之前的章节一样,我们通过OneApi+星火讯飞v3.5进行我们的Semantic Kernel的学习,具体配置可以翻翻我前几章内容。

创建项目

VS 创建控制台应用程序,右键管理用户机密,添加我们大模型的应用配置

{
  "OneApiSpark": {
    "Endpoint": "http://localhost:3000",
    "ModelId": "SparkDesk-v3.5",
    "ApiKey": "sk-LAYzQaWssCYYEVHP1d6a3fFa111745249e94F0364a0cF37c"
  }
}

安装 Nuget 依赖

PM> NuGet\Install-Package Microsoft.SemanticKernel -Version 1.13.0
PM> NuGet\Install-Package Microsoft.SemanticKernel.Yaml -Version 1.13.0

创建 Yaml 文件

创建文件

image

接下来 鼠标点击joke.yaml文件右键 点击属性,设置文件输出目录

image

Yaml 文件编写

我们将编写一个简单的提示函数,目的是生成笑话。
yaml文件的内容其实就是我们上一篇讲解的PromptTemplateConfig函数的 yaml 的表达形式。找到我们上一章节的PromptTemplateConfig的创建加深理解

    var kernelFunctions = kernel.CreateFunctionFromPrompt(new PromptTemplateConfig()
    {
        Name = "intent",
        Description = "use assistant to understand user input intent.",
        TemplateFormat = PromptTemplateConfig.SemanticKernelTemplateFormat,//此处可以省略默认就是"semantic-kernel"
        Template = "What is the intent of this request? {{$request}}",
        InputVariables = [new() { Name = "request", Description = "The user's request.", IsRequired = true }],
        ExecutionSettings = new Dictionary<string, PromptExecutionSettings>() {
               {
                      OpenAIPromptExecutionSettings.DefaultServiceId ,//"default"
                        new OpenAIPromptExecutionSettings()
                        {
                            MaxTokens = 1024,
                            Temperature = 0
                        }
                    },
        }
    });

那开始编写我们的 yaml

name: GenerateJoke
template: |
  Tell me a joke about {{$topic}} that is {{$length}} sentences long.
template_format: semantic-kernel
description: A function that generates a joke about a topic.
input_variables:
  - name: topic
    description: The topic of the joke.
    is_required: true
  - name: length
    description: The number of sentences in the joke.
    is_required: true
output_variable:
  description: The generated joke.
execution_settings:
  default:
    temperature: 0.9
    max_token: 1024

通过PromptTemplateConfig对象来理解就可以事半功倍了,写 yaml 完全没压力,里面的每一个属性细节在上一章节都有介绍,不熟悉的可以去上一章阅读一下。

SK 创建 prompts functions

//定义kernel 对象
var kernel = Kernel.CreateBuilder().AddOpenAIChatCompletion(modelId: config.ModelId,
apiKey: config.ApiKey,
httpClient: client).Build();

//读取yaml文件地址
var yamlDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins", "Yaml", "joke.yaml");
var promptYaml = await File.ReadAllTextAsync(yamlDirectory);
KernelFunction jokeFunc = kernel.CreateFunctionFromPromptYaml(promptYaml);

KernelArguments kernelArgs = new KernelArguments()
{
    {"topic","apple"},
    {"length","5"},

};
// 用内核调用函数并提供kernelArguments
FunctionResult results = await jokeFunc.InvokeAsync(kernel, kernelArgs);

Console.WriteLine(results.ToString());

输出

image

大功告成!

最后

本章简单的熟悉了一下用Yaml文件来创建prompts functions,用 YAML提示不仅简化了开发过程,还提高了应用程序的可维护性,为以后定义更加复杂的prompts内嵌函数,工作流等又进了一步 😃。

参考文献

yaml-prompts-with-semantic-kernel

本文源代码

😄欢迎关注笔者公众号一起学习交流,获取更多有用的知识~
image

posted @ 2024-05-22 09:22  董瑞鹏  阅读(136)  评论(0编辑  收藏  举报