优化语言模型提示

已完成

提示是向大型语言模型 (LLM) 提供的对话提示,根据查询或指令形成响应。 例如,可以提示 LLM 将句子从英语转换为法语,或生成文本摘要。

在上一单元中,已创建了提示作为输入字符串:

    string input = @"I'm a vegan in search of new recipes. I love spicy food! 
    Can you give me a list of breakfast recipes that are vegan friendly?";

提示涉及创建上下文丰富的清晰指令来指导模型生成所需的响应。 为了制作有效的提示,精准度和清晰度是关键。 可能需要进行试验并调整提示以获取准确的结果。

制作提示的技巧

  • 特定输入产生特定输出:LLM 根据收到的输入做出响应。 制作清晰具体的提示对于获取所需输出至关重要。

  • 试验是关键:可能需要迭代和试验不同的提示,以了解模型如何解释和生成响应。 微小的调整可能会导致结果发生重大变化。

  • 上下文问题:LLM 会考虑提示中提供的上下文。 应确保上下文定义明确且相关,以获取准确且连贯的响应。

  • 处理歧义:请记住,LLM 可能会遇到不明确的查询。 提供上下文或结构可避免模糊或意外的结果。

  • 提示长度:虽然 LLM 可以处理短提示和长提示,但应考虑简洁性和清晰度之间的权衡。 试验提示长度有助于找到最佳平衡。

创建提示模板

语义内核 SDK 支持模板化语言,使你可以在自然语言提示中使用表达式和变量。 这意味着可以创建可使用不同输入参数进行重用的提示。 为了在提示中嵌入表达式,模板化语言使用大括号 {{...}}

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Core;

var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(
    "your-deployment-name",
    "your-endpoint",
    "your-api-key",
    "deployment-model");

builder.Plugins.AddFromType<ConversationSummaryPlugin>();
var kernel = builder.Build();

string history = @"In the heart of my bustling kitchen, I have embraced 
    the challenge of satisfying my family's diverse taste buds and 
    navigating their unique tastes. With a mix of picky eaters and 
    allergies, my culinary journey revolves around exploring a plethora 
    of vegetarian recipes.

    One of my kids is a picky eater with an aversion to anything green, 
    while another has a peanut allergy that adds an extra layer of complexity 
    to meal planning. Armed with creativity and a passion for wholesome 
    cooking, I've embarked on a flavorful adventure, discovering plant-based 
    dishes that not only please the picky palates but are also heathy and 
    delicious.";

string prompt = @"This is some information about the user's background: 
    {{$history}}

    Given this user's background, provide a list of relevant recipes.";

var result = await kernel.InvokePromptAsync(prompt, 
    new KernelArguments() {{ "history", history }});

Console.WriteLine(result);

在此示例中,提示中引用了变量 history,由 $ 符号表示。 调用提示时,变量 history 将替换为 KernelArguments 字典中提供的实际值。 这样,便可以创建可动态填充不同输入的提示。

示例输出如下所示:

1. Lentil and vegetable soup - a hearty, filling soup that is perfect for a cold day. This recipe is vegetarian and can easily be adapted to accommodate allergies.

2. Cauliflower "steaks" - a delicious and healthy main course that is sure to satisfy even the pickiest of eaters. This recipe is vegetarian and can easily be made vegan.

3. Quinoa salad with roasted vegetables - a healthy and filling salad that is perfect for any occasion. This recipe is vegetarian and can easily be adapted to accommodate allergies.

4. Peanut-free pad Thai - a classic dish made without peanut sauce, perfect for those with peanut allergies. This recipe is vegetarian and can easily be made vegan.

5. Black bean and sweet potato enchiladas - a delicious and healthy twist on traditional enchiladas. This recipe is vegetarian and can easily be made vegan.

需要使用不同的输入执行同一任务时,创建可重用提示尤其有用。 在下一个练习中,你将练习使用语义内核 SDK 创建自己的可重用提示。