2025-11-20
C#
00

目录

YAML架构的基本结构
完整的YAML提示示例
在C#代码中使用YAML提示
总结

YAML架构是语义内核提供的一种声明式语法,用于定义AI提示模板。通过使用YAML,开发者可以更加结构化地创建、管理和维护提示,使得提示的版本控制、测试和部署变得更加简单。 问题来了,YAML是我现阶段最不喜欢的配置文件格式。。。。

YAML架构的基本结构

语义内核提示的YAML文件由几个关键部分组成,下面我们将详细介绍每个部分:

YAML
# 提示函数的基本信息 name: SummarizeText # 函数名称 description: 将文本概括为简明扼要的摘要 # 函数描述 template_format: semantic-kernel # 模板格式 template: | # 提示模板内容 请将以下文本概括为简洁的摘要: {{$input}} 摘要: # 输入变量定义 input_variables: - name: input # 变量名称 description: 需要被概括的文本 # 变量描述 is_required: true # 是否必需 default: "" # 默认值 # 输出变量定义 output_variables: - name: summary # 输出变量名 description: 生成的文本摘要 # 输出描述 # 执行设置 execution_settings: default: # 默认设置 service: OpenAI # 服务提供商 model: gpt-4 # 使用的模型 temperature: 0.7 # 温度参数 max_tokens: 500 # 最大令牌数

完整的YAML提示示例

以下是一个聊天机器人提示的完整YAML示例:

C#
name: CustomerGoldMemberPrivileges template_format: liquid description: 黄金会员专属服务模板 input_variables: - name: customer description: 客户详细信息 is_required: true json_schema: type: object properties: first_name: type: string last_name: type: string membership: type: string enum: ["黄金会员", "白金会员", "钻石会员"] points: type: number minimum: 0 next_level_points: type: number minimum: 0 - name: history description: 聊天历史 is_required: true json_schema: type: array items: type: object properties: role: type: string enum: ["user", "system", "assistant"] content: type: string template: | <message role="system"> # 黄金会员专属服务指南 ## 会员信息 姓名: {{customer.first_name}}{{customer.last_name}} 会员等级: {{customer.membership}} ## 个性化问候 亲爱的 {{customer.first_name}},感谢您一直以来对我们的支持! ## 会员权益详情 1. **专属折扣** - 门店和线上平台消费享受15%折扣 - 适用于全品类商品(特价商品除外) 2. **优先客服** - 专属服务热线 - 24/7快速响应 3. **配送服务** - 所有订单免运费 - 顺丰快递配送 4. **生日礼遇** - 生日当月享受专属礼券 - 200元无门槛礼券 5. **积分管理** - 当前积分:{{customer.points}}分 - 升级提示:再消费{{customer.next_level_points}}元即可升级铂金会员 祝您购物愉快! </message> output_variables: - name: response description: AI助手的响应 json_schema: type: string maxLength: 500 execution_settings: default: service: DeepSeek model: deepseek-chat temperature: 0.3 max_tokens: 300 function_choice_behavior: auto allow_dangerously_set_content: false

在C#代码中使用YAML提示

以下是在C#应用程序中加载和使用YAML提示的完整示例:

C#
#pragma warning disable SKEXP0010 using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.PromptTemplates.Liquid; namespace AppYAML { internal class Program { static async Task Main(string[] args) { System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); // 创建语义内核实例 var kernelBuilder = Kernel.CreateBuilder(); kernelBuilder.AddOpenAIChatCompletion( modelId: "deepseek-chat", apiKey: Environment.GetEnvironmentVariable("DEEPSEEK_API_KEY") ?? "sk-xxxx", endpoint: new Uri("https://api.deepseek.com/v1") ); // 构建内核 var kernel = kernelBuilder.Build(); // 从文件加载YAML提示 var yamlPrompt = System.IO.File.ReadAllText("PersonalizedChatAssistant.yaml" ,System.Text.Encoding.GetEncoding("GB2312")); // 创建模板工厂(根据您的模板格式选择) var templateFactory = new LiquidPromptTemplateFactory(); var promptTemplateConfig = new PromptTemplateConfig() { Template = yamlPrompt, TemplateFormat = "liquid", Name = "ContosoChatPrompt", }; // 从YAML创建函数 var function = kernel.CreateFunctionFromPrompt(promptTemplateConfig, templateFactory); // 准备函数参数 var arguments = new KernelArguments() { ["customer"] = new { first_name = "张", last_name = "三", membership = "黄金会员", points = 2850, next_level_points = 1500 }, ["history"] = new[] { new { role = "user", content = "我想了解会员权益" } } }; // 调用函数并获取响应 var response = await kernel.InvokeAsync(function, arguments); // 输出结果 Console.WriteLine(response); Console.ReadKey(); } } }

image.png

总结

语义内核的YAML架构为开发者提供了一种强大而灵活的方式来定义和管理AI提示。通过YAML,您可以:

  • 以结构化的方式创建和管理提示
  • 为不同场景配置不同的执行设置
  • 实现提示的版本控制和协作开发
  • 将提示作为代码处理,进行测试和部署

掌握YAML架构是成为一名出色的提示工程师的重要一步,它将帮助您构建更智能、更个性化的AI应用程序。

本文作者:技术老小子

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!