Java 后端面试题库 / 全部题解免费阅读理解原理,有条理地回答

少样本提示是怎么靠几个例子教模型的?

查看完整题解

参考回答

直接回答

少样本提示在任务说明中放几个输入和目标输出示例。模型可从示例看出格式及判断方式。FewShotPromptTemplate 可用同一例子模板渲染多个示例,再拼到真实问题前。

讲清原理

例子应覆盖容易混淆的边界。示例太多也会占上下文,并可能把模型带向不适用的模式。需要比较时统一使用同一套留出测试题。

例子与易错点

示例展示两个标签示范和一个待分类的问题,只验证模板组合,不代表发送后模型一定答对。

from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate

template = FewShotPromptTemplate(
    examples=[{"text": "late parcel", "label": "delivery"},
              {"text": "refund requested", "label": "refund"}],
    example_prompt=PromptTemplate.from_template("Text: {text}\nLabel: {label}"),
    prefix="Classify each message.",
    suffix="Text: {question}\nLabel:",
    input_variables=["question"],
)
rendered = template.format(question="Where is my delivery?")
assert "Text: late parcel\nLabel: delivery" in rendered
assert "Text: refund requested\nLabel: refund" in rendered
assert rendered.endswith("Text: Where is my delivery?\nLabel:")
print(rendered.splitlines()[-2])

相关题:第 5 题、第 10 题、第 11 题、第 13 题。

这道题,你掌握得怎么样?

掌握程度由你主动标记,阅读和收藏不会自动改变状态。