动态

详情 返回 返回

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

user avatar huangmingji 头像
点赞 1 用户, 点赞了这篇动态!
点赞

Add a new 评论

Some HTML is okay.