趋近智
提示模板管理大型语言模型输入的结构。少样本提示则能控制其行为。与其仅仅告诉模型做什么,不如直接展示给它。这种方法,又称作情境学习,通过在提示中直接提供所需输入-输出模式的示例,能大幅提升模型在特定任务上的表现。
少样本提示在引导模型的输出格式、语气和推理过程方面尤为有效,且无需进行成本高昂的微调。
其本质是一个少样本示例由一对输入及其对应的预期输出组成。这些示例在模型处理新的、未见过的输入时作为参考。在工具包中,您可以使用 create_example 函数来构建这些配对。
我们以情感分类任务为例。一个示例会包含一段示例文本和其正确的情感标签。
from kerb.prompt import create_example
# 为情感分类任务创建示例
ex_positive = create_example(
input_text="The product exceeded my expectations!",
output_text="positive",
)
ex_negative = create_example(
input_text="Terrible customer service, very disappointed.",
output_text="negative",
)
每个由 create_example 创建的对象都封装了一个完整的模型演示。
对于任何特定任务,您很可能会有几十甚至几百个高质量示例。鉴于单个提示只能容纳少量示例,您需要一种方法来组织它们,并为特定查询挑选出最相关的。ExampleSelector 正是为此设计的。它充当您所有潜在少样本示例的存储库,或者说“库”。
您可以使用已创建的示例来填充 ExampleSelector。
from kerb.prompt import ExampleSelector, create_example
# 创建一个选择器来组织我们的示例
sentiment_selector = ExampleSelector()
# 向选择器添加示例
examples_data = [
("The movie was fantastic!", "positive"),
("Worst purchase ever made.", "negative"),
("Average quality, decent price.", "neutral"),
("Absolutely love this product!", "positive"),
("Not worth the money.", "negative"),
]
for inp, out in examples_data:
sentiment_selector.add(create_example(input_text=inp, output_text=out))
示例整理好后,您现在可以采用不同的策略来为您的提示选择最有效的子集。
并非所有示例都对每个查询同样有用。ExampleSelector 上的 select 方法允许您选择最适合您需求的选择策略。
随机选择示例是一个好的默认策略,可以避免模型对示例库中特定顺序或模式的过拟合。它引入了多样性,并帮助模型更好地泛化。
# 从示例库中选择 3 个随机示例
selected_examples = sentiment_selector.select(k=3, strategy="random")
for ex in selected_examples:
print(f"'{ex.input}' -> '{ex.output}'")
有时,您希望确保选出的示例涵盖多种模式。diverse 策略选择彼此有别的示例,以此使呈现给模型的信息种类达到最大。这对于向模型展示如何处理不同类型的输入很有助益。
# 选择 4 个尽可能各不相同的示例
diverse_examples = sentiment_selector.select(k=4, strategy="diverse")
多样性如何计算
基于多样性的选择使用一种简单的基于嵌入的算法,以寻找在向量空间中彼此相距较远的示例。这确保所选示例在语义上各不相同,为大型语言模型提供了更宽泛的背景信息。
最精细的策略是语义选择。它选择与当前用户查询意义最相似的示例。这是一种极其有效的方法,用于创建动态、情境感知的提示。例如,如果用户询问关于 Python 列表的问题,选择器将找到并纳入同样处理列表操作的少样本示例。
这种策略需要一个嵌入模型来计算语义相似度。如果所需模块不可用,它会妥善地回退到像 random 这样的不同策略。
# 语义选择的使用示例
query = "How do I iterate over a list?"
# 选择器会找到与循环或列表相关的示例
# try:
# semantic_examples = code_selector.select(
# k=2, strategy="semantic", query=query
# )
# except Exception:
# # 如果嵌入模型不可用,则回退
# semantic_examples = code_selector.select(k=2, strategy="random")
一旦您选择了示例,就需要将它们格式化为一个字符串,以便插入到您的提示模板中。format_examples 函数处理此项工作。
您可以通过 template 参数控制每个示例的显示形式,并通过 separator 参数控制它们之间的间距。
from kerb.prompt import format_examples
# 我们将使用之前随机选择的示例
selected_examples = sentiment_selector.select(k=3, strategy="random")
# 将示例格式化为字符串
formatted_text = format_examples(
selected_examples,
template="Input: {input}\nOutput: {output}",
separator="\n\n"
)
print(formatted_text)
这将生成一个干净、组织良好的文本块,可供大型语言模型使用,如下所示:
输入: The movie was fantastic!
输出: positive
输入: Not worth the money.
输出: negative
输入: Average quality, decent price.
输出: neutral
最后一步是将您的系统指令、格式化后的少样本示例和用户的新查询组合成一个完整提示。
以下是姓名提取任务的完整流程:
from kerb.prompt import (
ExampleSelector,
create_example,
format_examples,
render_template
)
# 1. 创建示例库
name_extractor_bank = ExampleSelector()
training_data = [
("Extract the name: John Smith lives in NYC", "John Smith"),
("The CEO, Michael Brown, announced the merger.", "Michael Brown"),
("Dr. Sarah Johnson is a leading scientist.", "Dr. Sarah Johnson"),
("We spoke with Professor David Lee.", "Professor David Lee"),
]
for inp, out in training_data:
name_extractor_bank.add(create_example(input_text=inp, output_text=out))
# 2. 定义主提示模板
prompt_template = """你是一个姓名提取系统。
以下是一些提取姓名的示例:
{examples}
现在,对以下文本执行提取操作。
输入: {user_query}
输出:"""
# 3. 处理新的用户查询
new_query = "Jennifer Martinez is the new director of the project."
# 4. 选择并格式化少样本示例
selected = name_extractor_bank.select(k=2, strategy="random")
examples_text = format_examples(
selected,
template="Input: {input}\nOutput: {output}",
separator="\n\n"
)
# 5. 渲染最终提示
final_prompt = render_template(
prompt_template,
{
"examples": examples_text,
"user_query": new_query
}
)
print(final_prompt)
这种有组织的方法不仅提高了模型准确性,也使您的提示逻辑更具模块化和可维护性。通过将示例与提示模板分开,您可以独立于核心提示逻辑来更新、维护和测试您的少样本数据。
这部分内容有帮助吗?
© 2026 ApX Machine Learning用心打造