開始使用免費開始

建立 few-shot 提示樣板

你已經把範例整理成結構化格式,現在要來建立 few-shot 提示樣板!你將建立一個樣板,把問答配對轉為以下格式:

Question: Example question
Example Answer

為了完成本練習所需的所有 LangChain 類別都已為你預先載入。

本練習屬於課程

使用 LangChain 開發 LLM 應用

檢視課程

練習說明

  • 完成用於格式化答案的提示,讓它同時包含 questionanswer 這兩個鍵。
  • 使用 FewShotPromptTemplate,搭配 examplesexample_prompt 來建立 few-shot 提示。
  • 依據提供的 suffix,補上輸入變數清單。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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)
編輯並執行程式碼