如何在单次 LLM 调用中总结文本
LLM 可以总结文本并提炼出所需信息,即使是大量文本。在许多情况下,尤其是对于具有更大上下文窗口的模型而言,这可以通过单次 LLM 调用来充分实现。
LangChain 实现了一个简单的 预构建链,它会将提示“塞入”用于摘要和其他目的所需内容。在本指南中,我们将演示如何使用该链。
加载聊天模型
我们先来加载一个聊天模型:
Select chat model:
pip install -qU "langchain[google-genai]"
import getpass
import os
if not os.environ.get("GOOGLE_API_KEY"):
os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter API key for Google Gemini: ")
from langchain.chat_models import init_chat_model
llm = init_chat_model("gemini-2.0-flash", model_provider="google_genai")
加载文档
接下来,我们需要一些文档来总结。下面,我们生成一些玩具文档以供说明。有关其他数据源,请参阅文档加载器 操作指南 和 集成页面。摘要教程 还包括一个总结博客文章的示例。
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 Reference:Document
加载链
下面,我们定义一个简单的提示,并用我们的聊天模型和文档来实例化该链:
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)
API Reference:create_stuff_documents_chain | ChatPromptTemplate
调用链
由于该链是一个 Runnable,它实现了通常的调用方法:
result = chain.invoke({"context": documents})
result
'The content describes the colors of three fruits: apples are red, blueberries are blue, and bananas are yellow.'
流式传输
请注意,该链还支持单独输出 token 的流式传输:
for chunk in chain.stream({"context": documents}):
print(chunk, end="|")
|The| content| describes| the| colors| of| three| fruits|:| apples| are| red|,| blueberries| are| blue|,| and| bananas| are| yellow|.||
后续步骤
请参阅 如何进行摘要 操作指南,了解其他摘要策略,包括专为大批量文本设计的策略。
另请参阅 此教程,了解更多关于摘要的详细信息。