优化语言模型提示
提示是向大型语言模型 (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?";
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?"""
在此提示中,你将提供语言模型的内容以及说明。 内容可帮助模型生成与用户更相关的结果。
提示涉及创建上下文丰富的清晰指令来指导模型生成所需的响应。 为了制作有效的提示,精准度和清晰度是关键。 可能需要试验并调整提示以获取准确的结果。
使用示例来引导模型
可以在提示中包含示例,以帮助指导响应。 这些示例可以在说明之前或之后提供。 提示示例根据它们是否包含逐字完成,分类为零次学习或少量学习。 逐字完成是提示中包含的响应的特定预定义示例。 它逐字展示了对 AI 期望的答案类型。 通过这些示例,AI 可以更轻松地模拟提供完成的结构、样式或语气。
零样本学习
通过零样本学习,可以包括说明,但不包括逐字完成。 零样本提示依赖于模型的现有知识来生成响应。 需要常规答案或任务简单且不需要太多指导时,零样本提示非常有用。 零样本提示也不太消耗资源,因为它依赖于现有知识。
下面是一个零样本提示的示例,该提示指示模型评估用户输入、确定用户的意向,并使用“意向:”开头输出。
string prompt = $"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.
User Input: {request}
Intent:
""";
prompt = f"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.
User Input: {request}
Intent:
"""
少样本学习
通过少样本学习,可以在提示中包含逐字完成,以帮助指导模型的响应。 通常包括一到五个示例。 这些示例演示所需的响应结构、样式或类型。 少样本学习会产生更多令牌,也会导致模型更新其知识库。 很少的提示对于减少歧义和使结果与所需结果保持一致尤其有用。
下面是一个少样本提示的示例,该提示告知模型评估用户输入、确定用户的意向,并以“意向:”作为输出的前缀。
string prompt = $"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.
User Input: Can you send a very quick approval to the marketing team?
Intent: SendMessage
User Input: Can you send the full update to the marketing team?
Intent: SendEmail
User Input: {request}
Intent:
""";
prompt = f"""
Instructions: What is the intent of this request?
If you don't know the intent, don't guess; instead respond with "Unknown".
Choices: SendEmail, SendMessage, CompleteTask, CreateDocument, Unknown.
User Input: Can you send a very quick approval to the marketing team?
Intent: SendMessage
User Input: Can you send the full update to the marketing team?
Intent: SendEmail
User Input: {request}
Intent:
"""
在提示中使用角色
在提示中分配角色是一种用于指导模型在生成响应时采用特定观点、语气或专业知识的技术。 角色允许你定制输出,以更好地适应任务的上下文或受众。 当需要让响应模拟某种职业或反映特定语气时,角色非常有用。 若要分配角色,应在提示中清楚地描述角色定义。
下面是分配角色的提示示例:
string prompt = $"""
You are a highly experienced software engineer. Explain the concept of asynchronous programming to a beginner.
""";
prompt = """
You are a highly experienced software engineer. Explain the concept of asynchronous programming to a beginner.
"""
思维链提示
通过思考提示链,可以提示模型分步执行任务,并按输出顺序呈现每个步骤及其结果。 链提示可以通过将某些执行计划卸载到模型来简化提示工程。 链提示有助于更轻松地将问题隔离到具体步骤,让您明确需要集中精力的方向。 可以指示模型包含其思维链,也可以使用示例来显示模型如何分解任务。
以下示例指示模型描述分步推理:
string prompt = $"""
A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?
Instructions: Explain your reasoning step by step before providing the answer.
""";
prompt = """
A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?
Instructions: Explain your reasoning step by step before providing the answer.
"""
下面是描述完成模型的步骤的示例:
prompt = $"""
Instructions: A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?
First, calculate how many full baskets the farmer can make by dividing the total apples by the apples per basket:
1.
Next, subtract the number of apples used in the baskets from the total number of apples to find the remainder:
1.
"Finally, the farmer will eat the remaining apples:
1.
""";
prompt = """
Instructions: A farmer has 150 apples and wants to sell them in baskets. Each basket can hold 12 apples. If any apples remain after filling as many baskets as possible, the farmer will eat them. How many apples will the farmer eat?
First, calculate how many full baskets the farmer can make by dividing the total apples by the apples per basket:
1.
Next, subtract the number of apples used in the baskets from the total number of apples to find the remainder:
1.
Finally, the farmer will eat the remaining apples:
1.
"""
此提示的输出应类似于以下输出:
Divide 150 by 12 to find the number of full baskets the farmer can make: 150 / 12 = 12.5 full baskets
The farmer can make 12 full baskets with 12 apples each.
Multiply 12 full baskets by 12 apples per basket to find the number of apples used in the baskets: 12 * 12 = 144 apples
Subtract the number of apples used in the baskets from the total number of apples: 150 - 144 = 6 apples
The farmer will eat 6 remaining apples.
撰写提示的技巧
特定输入产生特定输出:LLM 根据收到的输入做出响应。 制作清晰具体的提示对于获取所需输出至关重要。
试验是关键:可能需要循环访问和试验不同的提示,以了解模型如何解释和生成响应。 微小的调整可能会导致结果发生重大变化。
上下文问题:LLM 会考虑提示中提供的上下文。 应确保上下文定义明确且相关,以获取准确且连贯的响应。
处理歧义:请记住,LLM 可能难以处理不明确的查询。 提供上下文或结构可避免模糊或意外的结果。
提示长度:虽然 LLM 可以处理短提示和长提示,但应考虑简洁性和清晰度之间的权衡。 试验提示长度有助于找到最佳平衡。
制作有效的提示需要清晰、精确和周到的设计。 诸如零样本学习、角色指派和思维链引导等技术可以提高响应的质量和相关性。 通过根据需要提供明确的说明、定义完善的上下文和示例,可以指导模型生成经过微调的相关响应。 若要获得最佳效果,请记得试验和优化提示。