开始使用免费开始使用

构建 few-shot 提示模板

现在示例已经是结构化格式,接下来就来创建 few-shot 提示模板吧!您将把问答对转换成以下格式:

Question: Example question
Example Answer

完成本练习所需的所有 LangChain 类都已为您预加载。

本练习是课程的一部分

使用 LangChain 开发 LLM 应用

查看课程

练习说明

  • 完成用于格式化答案的提示,使其包含 questionanswer 键。
  • 使用 FewShotPromptTemplate,结合 examplesexample_prompt 创建 few-shot 提示。
  • 根据给定的后缀补全输入变量列表。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Complete the prompt for formatting answers
example_prompt = PromptTemplate.from_template("Question: {____}\n{____}")

# Create the few-shot prompt
prompt_template = ____(
    examples=____,
    example_prompt=____,
    suffix="Question: {input}",
    input_variables=["____"],
)

prompt = prompt_template.invoke({"input": "What is Jack's favorite technology on DataCamp?"})
print(prompt.text)
编辑并运行代码