Anthropic 的高级提示定制
最后更新:2025 年 4 月 16 日
笔记本作者:Bilge Yucel ( LI & X (Twitter))
在本示例中,我们将使用 Anthropic 的提示工程指南 中的提示技术来创建一个 RAG 应用程序。该应用程序将使用 Anthropic Claude 3 模型和 Haystack 从给定文档中提取相关引文,并根据提取的引文生成答案。
📚 有用的资源
设置开发环境
使用 pip 安装 antropic-haystack 包和其他必需的包
!pip install anthropic-haystack "datasets>=2.6.1" "sentence-transformers>=3.0.0"
您需要一个 ANTHROPIC_API_KEY 才能使用 Claude 模型。请在此处 获取您的 API 密钥
import os
from getpass import getpass
os.environ["ANTHROPIC_API_KEY"] = getpass("Enter the ANTHROPIC_API_KEY: ")
Enter the ANTHROPIC_API_KEY: ··········
下载数据集
我们将使用 Hugging Face 上的 bilgeyucel/seven-wonders 数据集
from datasets import load_dataset
from haystack import Document
dataset = load_dataset("bilgeyucel/seven-wonders", split="train")
docs = [Document(content=doc["content"], meta=doc["meta"]) for doc in dataset]
创建嵌入并将文档索引到 DocumentStore
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.embedders import SentenceTransformersDocumentEmbedder
document_store = InMemoryDocumentStore()
doc_embedder = SentenceTransformersDocumentEmbedder(model="sentence-transformers/all-MiniLM-L6-v2")
doc_embedder.warm_up()
docs_with_embeddings = doc_embedder.run(docs)
document_store.write_documents(docs_with_embeddings["documents"])
构建提示
Claude 模型熟悉包含 XML 标签的提示,因为它们在训练过程中接触过此类提示。通过将提示的关键部分(如说明、示例或输入数据)包装在 XML 标签中,我们可以帮助 Claude 更好地理解上下文并生成更准确的输出。
要构建一个首先从相关文档中提取引文,然后引用这些引文来生成答案的提示,请按照提示中的以下步骤操作
- 将检索到的文档放在
<documents> </documents>标签之间。 - 将每个文档放在
<document> </document>标签之间,包括一个文档索引供 Claude 稍后引用。 - 指示 Claude 在
<quotes> </quotes>标签内提取引文,包括文档索引信息。 - 确保 Claude 在
<answer> </answer>标签内生成答案。
prompt = """
Here is a document, in <document></document> XML tags:
<documents>
{% for document in documents %}
<document index="{{loop.index}}">
{{document.content}}
</document>
{% endfor %}
</documents>
First, extract, word-for-word, any quotes relevant to the question, enclose the full list of quotes in <quotes></quotes> XML tags with the corresponding document index and use these quotes to an answer to the question.
If there are no quotes in this document that seem relevant to this question, please say "I can't find any relevant quotes".
Then, answer the question in <answer></answer> tags. Do not include or reference quoted content verbatim in the answer. Ensure that your answer is accurate and doesn't contain any information not directly supported by the quotes. Make references to quotes relevant to each section of the answer solely by adding their bracketed numbers at the end of relevant sentences
Here is the question: {{question}}
"""
当使用相关的文档和问题提供时,渲染后的提示应如下所示
Here is a document, in <document></document> XML tags:
<documents>
<document index="1">
(the text content of the first document)
</document>
<document index="2">
(the text content of the second document)
</document>
<document index="3">
(the text content of the third document)
</document>
...
</documents>
First, extract, word-for-word, any quotes relevant to the question, enclose the full list of quotes in <quotes></quotes> XML tags with the corresponding document index and use these quotes to an answer to the question.
If there are no quotes in this document that seem relevant to this question, please say "I can't find any relevant quotes".
Then, answer the question in <answer></answer> tags. Do not include or reference quoted content verbatim in the answer. Ensure that your answer is accurate and doesn't contain any information not directly supported by the quotes. Make references to quotes relevant to each section of the answer solely by adding their bracketed numbers at the end of relevant sentences
Here is the question: (user question)
作为回报,Claude 的响应应如下所示
<quotes>
<quote index="1">Large numbers of people came to Ephesus in March and in the beginning of May to attend the main Artemis Procession.</quote>
<quote index="2">The Temple of Artemis or Artemision (Greek: Ἀρτεμίσιον; Turkish: Artemis Tapınağı), also known as the Temple of Diana, was a Greek temple dedicated to an ancient, local form of the goddess Artemis (identified with Diana, a Roman goddess).</quote>
</quotes>
<answer>
According to the documents, people visited the Temple of Artemis in Ephesus primarily for religious reasons to attend festivals and processions dedicated to the goddess Artemis (also known as Diana). The temple was dedicated to an ancient local form of the goddess and held a main Artemis Procession in March and early May that drew large crowds. [1] The temple was considered an important religious site for the worship of Artemis in the Greek world. [2] People likely traveled there to make offerings, participate in rituals, and celebrate the goddess during major festivals and ceremonies held at the sacred site.
</answer>
初始化 RAG Pipeline 组件
初始化 RAG Pipeline 所需的组件
- SentenceTransformersTextEmbedder:使用 sentence-transformers 模型为查询创建嵌入
- InMemoryEmbeddingRetriever:用于检索相关文档
-
ChatPromptBuilder:通过处理
ChatMessage对象来构建提示 -
AnthropicChatGenerator:使用 Anthropic Claude 模型的聊天完成 API,我们将在本示例中使用
claude-3-sonnet-20240229
from haystack.components.builders import ChatPromptBuilder
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.components.embedders import SentenceTransformersTextEmbedder
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
text_embedder = SentenceTransformersTextEmbedder(model="sentence-transformers/all-MiniLM-L6-v2")
retriever = InMemoryEmbeddingRetriever(document_store)
messages = [
ChatMessage.from_system("You are an expert who answers questions based on the given documents."),
ChatMessage.from_user(prompt),
]
prompt_builder = ChatPromptBuilder(template=messages, required_variables="*")
llm = AnthropicChatGenerator(model="claude-3-sonnet-20240229")
WARNING:haystack.components.builders.chat_prompt_builder:ChatPromptBuilder has 2 prompt variables, but `required_variables` is not set. By default, all prompt variables are treated as optional, which may lead to unintended behavior in multi-branch pipelines. To avoid unexpected execution, ensure that variables intended to be required are explicitly set in `required_variables`.
构建 RAG Pipeline
了解如何在 创建 Pipelines 中构建 pipeline。
from haystack import Pipeline
rag_with_quotes = Pipeline()
# Add components to your pipeline
rag_with_quotes.add_component("text_embedder", text_embedder)
rag_with_quotes.add_component("retriever", retriever)
rag_with_quotes.add_component("prompt_builder", prompt_builder)
rag_with_quotes.add_component("llm", llm)
# Now, connect the components to each other
rag_with_quotes.connect("text_embedder.embedding", "retriever.query_embedding")
rag_with_quotes.connect("retriever", "prompt_builder.documents")
rag_with_quotes.connect("prompt_builder", "llm")
测试不同问题
question = "Why were people visiting the Temple of Artemis?" # @param ["Why were people visiting the Temple of Artemis?", "How did Colossus of Rhodes collapse?", "What is the importance of Colossus of Rhodes?", "Why did people build Great Pyramid of Giza?", "What does Rhodes Statue look like?"]
result = rag_with_quotes.run(
data={
"text_embedder" : {"text": question},
"retriever" : {"top_k": 5},
"prompt_builder": {"question": question},
}
)
Batches: 0%| | 0/1 [00:00<?, ?it/s]
print(result["llm"]["replies"][0].text)
<quotes>
<quote index="1">Large numbers of people came to Ephesus in March and in the beginning of May to attend the main Artemis Procession.</quote>
<quote index="3">The fame of the Temple of Artemis was known in the Renaissance, as demonstrated in this imagined portrayal of the temple in a 16th-century hand-colored engraving by Martin Heemskerck.</quote>
</quotes>
<answer>
People visited the Temple of Artemis primarily for religious purposes, to participate in the Artemis Procession held in March and May each year. [1] The Temple of Artemis was a famous and renowned structure, known even in the Renaissance period, attracting visitors from far and wide to witness its splendor and attend worship services and festivals dedicated to the goddess Artemis. [3] Its prominence as one of the Seven Wonders of the Ancient World also likely drew many visitors seeking to experience the architectural marvel.
</answer>
提取引文
import re
re.findall(r'<quote .*?>([\s\S]*?)<\/quote>', result["llm"]["replies"][0].text)
['Large numbers of people came to Ephesus in March and in the beginning of May to attend the main Artemis Procession.',
'The fame of the Temple of Artemis was known in the Renaissance, as demonstrated in this imagined portrayal of the temple in a 16th-century hand-colored engraving by Martin Heemskerck.']
提取答案
import re
re.findall(r'<answer>([\s\S]*?)<\/answer>', result["llm"]["replies"][0].text)
['\nPeople visited the Temple of Artemis primarily for religious purposes, to participate in the Artemis Procession held in March and May each year. [1] The Temple of Artemis was a famous and renowned structure, known even in the Renaissance period, attracting visitors from far and wide to witness its splendor and attend worship services and festivals dedicated to the goddess Artemis. [3] Its prominence as one of the Seven Wonders of the Ancient World also likely drew many visitors seeking to experience the architectural marvel.\n']
