從 StuffDocumentsChain 遷移
StuffDocumentsChain 通過將文件連接到單個上下文窗口來組合文件。這是一種直接且有效的文件組合策略,適用於問答、摘要和其他目的。
create_stuff_documents_chain 建議使用作為替代方案。它的功能與 StuffDocumentsChain
相同,但對串流和批次功能有更好的支援。由於它是 LCEL 原語的簡單組合,因此也更容易擴展並整合到其他 LangChain 應用程式中。
下面我們將通過一個簡單的範例來說明 StuffDocumentsChain
和 create_stuff_documents_chain
。
讓我們首先載入一個聊天模型
選擇 聊天模型
pip install -qU "langchain[openai]"
import getpass
import os
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
from langchain.chat_models import init_chat_model
llm = init_chat_model("gpt-4o-mini", model_provider="openai")
範例
讓我們來看一個範例,我們分析一組文件。我們先生成一些簡單的文件以作說明
from langchain_core.documents import Document
documents = [
Document(page_content="Apples are red", metadata={"title": "apple_book"}),
Document(page_content="Blueberries are blue", metadata={"title": "blueberry_book"}),
Document(page_content="Bananas are yelow", metadata={"title": "banana_book"}),
]
API 參考:Document
舊版
詳細資訊
下面我們展示了使用 StuffDocumentsChain
的實現。我們為摘要任務定義了提示範本,並為此目的實例化了一個 LLMChain 物件。我們定義了如何將文件格式化到提示中,並確保各種提示中鍵的一致性。
from langchain.chains import LLMChain, StuffDocumentsChain
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
# This controls how each document will be formatted. Specifically,
# it will be passed to `format_document` - see that function for more
# details.
document_prompt = PromptTemplate(
input_variables=["page_content"], template="{page_content}"
)
document_variable_name = "context"
# The prompt here should take as an input variable the
# `document_variable_name`
prompt = ChatPromptTemplate.from_template("Summarize this content: {context}")
llm_chain = LLMChain(llm=llm, prompt=prompt)
chain = StuffDocumentsChain(
llm_chain=llm_chain,
document_prompt=document_prompt,
document_variable_name=document_variable_name,
)
我們現在可以調用我們的鏈
result = chain.invoke(documents)
result["output_text"]
'This content describes the colors of different fruits: apples are red, blueberries are blue, and bananas are yellow.'
for chunk in chain.stream(documents):
print(chunk)
{'input_documents': [Document(metadata={'title': 'apple_book'}, page_content='Apples are red'), Document(metadata={'title': 'blueberry_book'}, page_content='Blueberries are blue'), Document(metadata={'title': 'banana_book'}, page_content='Bananas are yelow')], 'output_text': 'This content describes the colors of different fruits: apples are red, blueberries are blue, and bananas are yellow.'}
LCEL
詳細資訊
下面我們展示了使用 create_stuff_documents_chain
的實現。
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template("Summarize this content: {context}")
chain = create_stuff_documents_chain(llm, prompt)
調用鏈後,我們獲得了與之前類似的結果
result = chain.invoke({"context": documents})
result
'This content describes the colors of different fruits: apples are red, blueberries are blue, and bananas are yellow.'
請注意,此實作支援輸出 Token 的串流
for chunk in chain.stream({"context": documents}):
print(chunk, end=" | ")
| This | content | describes | the | colors | of | different | fruits | : | apples | are | red | , | blue | berries | are | blue | , | and | bananas | are | yellow | . | |
後續步驟
請查看 LCEL 概念文件以獲取更多背景資訊。
請參閱這些 操作指南,以了解更多關於使用 RAG 進行問答任務的資訊。
請參閱本教學以了解更多基於 LLM 的摘要策略。