跳到主要內容
Open In ColabOpen on GitHub

如何使用少量範例

先決條件

本指南假設您熟悉以下概念

在本指南中,我們將學習如何建立一個簡單的提示範本,在生成時為模型提供範例輸入和輸出。為 LLM 提供一些這樣的範例稱為 少量樣本提示,這是一種簡單但功能強大的方法,可以引導生成,並在某些情況下大幅提高模型效能。

少量樣本提示範本可以從一組範例或從 範例選擇器 類別建構,該類別負責從定義的集合中選擇範例子集。

本指南將涵蓋使用字串提示範本進行少量樣本提示。有關使用聊天訊息為聊天模型進行少量樣本提示的指南,請參閱 此處

為少量範例建立格式化器

配置一個格式化器,將少量範例格式化為字串。此格式化器應為 PromptTemplate 物件。

from langchain_core.prompts import PromptTemplate

example_prompt = PromptTemplate.from_template("Question: {question}\n{answer}")
API 參考:PromptTemplate

建立範例集

接下來,我們將建立少量範例的清單。每個範例都應該是一個字典,表示上述定義的格式化器提示的範例輸入。

examples = [
{
"question": "Who lived longer, Muhammad Ali or Alan Turing?",
"answer": """
Are follow up questions needed here: Yes.
Follow up: How old was Muhammad Ali when he died?
Intermediate answer: Muhammad Ali was 74 years old when he died.
Follow up: How old was Alan Turing when he died?
Intermediate answer: Alan Turing was 41 years old when he died.
So the final answer is: Muhammad Ali
""",
},
{
"question": "When was the founder of craigslist born?",
"answer": """
Are follow up questions needed here: Yes.
Follow up: Who was the founder of craigslist?
Intermediate answer: Craigslist was founded by Craig Newmark.
Follow up: When was Craig Newmark born?
Intermediate answer: Craig Newmark was born on December 6, 1952.
So the final answer is: December 6, 1952
""",
},
{
"question": "Who was the maternal grandfather of George Washington?",
"answer": """
Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball
""",
},
{
"question": "Are both the directors of Jaws and Casino Royale from the same country?",
"answer": """
Are follow up questions needed here: Yes.
Follow up: Who is the director of Jaws?
Intermediate Answer: The director of Jaws is Steven Spielberg.
Follow up: Where is Steven Spielberg from?
Intermediate Answer: The United States.
Follow up: Who is the director of Casino Royale?
Intermediate Answer: The director of Casino Royale is Martin Campbell.
Follow up: Where is Martin Campbell from?
Intermediate Answer: New Zealand.
So the final answer is: No
""",
},
]

讓我們使用其中一個範例測試格式化提示

print(example_prompt.invoke(examples[0]).to_string())
Question: Who lived longer, Muhammad Ali or Alan Turing?

Are follow up questions needed here: Yes.
Follow up: How old was Muhammad Ali when he died?
Intermediate answer: Muhammad Ali was 74 years old when he died.
Follow up: How old was Alan Turing when he died?
Intermediate answer: Alan Turing was 41 years old when he died.
So the final answer is: Muhammad Ali

將範例和格式化器傳遞給 FewShotPromptTemplate

最後,建立一個 FewShotPromptTemplate 物件。此物件接收少量範例和少量範例的格式化器。當格式化此 FewShotPromptTemplate 時,它會使用 example_prompt 格式化傳遞的範例,然後在 suffix 之前將它們新增到最終提示中

from langchain_core.prompts import FewShotPromptTemplate

prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
suffix="Question: {input}",
input_variables=["input"],
)

print(
prompt.invoke({"input": "Who was the father of Mary Ball Washington?"}).to_string()
)
Question: Who lived longer, Muhammad Ali or Alan Turing?

Are follow up questions needed here: Yes.
Follow up: How old was Muhammad Ali when he died?
Intermediate answer: Muhammad Ali was 74 years old when he died.
Follow up: How old was Alan Turing when he died?
Intermediate answer: Alan Turing was 41 years old when he died.
So the final answer is: Muhammad Ali


Question: When was the founder of craigslist born?

Are follow up questions needed here: Yes.
Follow up: Who was the founder of craigslist?
Intermediate answer: Craigslist was founded by Craig Newmark.
Follow up: When was Craig Newmark born?
Intermediate answer: Craig Newmark was born on December 6, 1952.
So the final answer is: December 6, 1952


Question: Who was the maternal grandfather of George Washington?

Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball


Question: Are both the directors of Jaws and Casino Royale from the same country?

Are follow up questions needed here: Yes.
Follow up: Who is the director of Jaws?
Intermediate Answer: The director of Jaws is Steven Spielberg.
Follow up: Where is Steven Spielberg from?
Intermediate Answer: The United States.
Follow up: Who is the director of Casino Royale?
Intermediate Answer: The director of Casino Royale is Martin Campbell.
Follow up: Where is Martin Campbell from?
Intermediate Answer: New Zealand.
So the final answer is: No


Question: Who was the father of Mary Ball Washington?

透過為模型提供像這樣的範例,我們可以引導模型產生更好的回應。

使用範例選擇器

我們將重複使用上一節中的範例集和格式化器。但是,我們不會將範例直接饋送到 FewShotPromptTemplate 物件中,而是將它們饋送到名為 SemanticSimilarityExampleSelector 實例的 ExampleSelector 實作中。此類別根據少量範例與輸入的相似性,從初始集中選擇少量範例。它使用嵌入模型來計算輸入和少量範例之間的相似性,以及向量儲存來執行最近鄰搜尋。

為了展示它的外觀,讓我們初始化一個實例並單獨調用它

from langchain_chroma import Chroma
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_openai import OpenAIEmbeddings

example_selector = SemanticSimilarityExampleSelector.from_examples(
# This is the list of examples available to select from.
examples,
# This is the embedding class used to produce embeddings which are used to measure semantic similarity.
OpenAIEmbeddings(),
# This is the VectorStore class that is used to store the embeddings and do a similarity search over.
Chroma,
# This is the number of examples to produce.
k=1,
)

# Select the most similar example to the input.
question = "Who was the father of Mary Ball Washington?"
selected_examples = example_selector.select_examples({"question": question})
print(f"Examples most similar to the input: {question}")
for example in selected_examples:
print("\n")
for k, v in example.items():
print(f"{k}: {v}")
Examples most similar to the input: Who was the father of Mary Ball Washington?


answer:
Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball

question: Who was the maternal grandfather of George Washington?

現在,讓我們建立一個 FewShotPromptTemplate 物件。此物件接收範例選擇器和少量範例的格式化器提示。

prompt = FewShotPromptTemplate(
example_selector=example_selector,
example_prompt=example_prompt,
suffix="Question: {input}",
input_variables=["input"],
)

print(
prompt.invoke({"input": "Who was the father of Mary Ball Washington?"}).to_string()
)
Question: Who was the maternal grandfather of George Washington?

Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball


Question: Who was the father of Mary Ball Washington?

下一步

您現在已學會如何將少量範例新增到您的提示中。

接下來,查看本節中關於提示範本的其他操作指南、關於 使用聊天模型進行少量樣本提示 的相關操作指南,或其他的 範例選擇器操作指南


此頁面是否有幫助?