建设网站的效益分析,制作手机主题的app,免费跨国浏览器,深圳红酒网站建设利用LangChain进行文本摘要的详细总结
LangChain是一个强大的工具#xff0c;可以帮助您使用大型语言模型#xff08;LLM#xff09;来总结多个文档的内容。以下是一个详细指南#xff0c;介绍如何使用LangChain进行文本摘要#xff0c;包括使用文档加载器、三种常见的摘…利用LangChain进行文本摘要的详细总结
LangChain是一个强大的工具可以帮助您使用大型语言模型LLM来总结多个文档的内容。以下是一个详细指南介绍如何使用LangChain进行文本摘要包括使用文档加载器、三种常见的摘要方法Stuff、Map-Reduce和Refine以及具体的实现步骤。
1. 安装和设置
首先确保您已安装LangChain并设置了所需的环境变量。
pip install langchain设置环境变量来开始记录跟踪
import getpass
import osos.environ[LANGCHAIN_TRACING_V2] true
os.environ[LANGCHAIN_API_KEY] getpass.getpass()2. 加载文档
使用文档加载器加载内容。例如可以使用WebBaseLoader从HTML网页加载内容
from langchain_community.document_loaders import WebBaseLoaderloader WebBaseLoader(https://lilianweng.github.io/posts/2023-06-23-agent/)
docs loader.load()3. 三种常见的摘要方法
方法1Stuff
将所有文档内容连接成一个提示然后传递给LLM。适用于较大上下文窗口的模型例如OpenAI的GPT-4或Anthropic的Claude-3。
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.chains.llm import LLMChain
from langchain_core.prompts import PromptTemplate# 定义提示
prompt_template Write a concise summary of the following:
{text}
CONCISE SUMMARY:
prompt PromptTemplate.from_template(prompt_template)# 定义LLM链
llm ChatOpenAI(temperature0, model_namegpt-3.5-turbo-16k)
llm_chain LLMChain(llmllm, promptprompt)# 定义StuffDocumentsChain
stuff_chain StuffDocumentsChain(llm_chainllm_chain, document_variable_nametext)docs loader.load()
result stuff_chain.invoke(docs)
print(result[output_text])方法2Map-Reduce
先将每个文档分别总结然后将这些总结归纳成一个全局摘要。
from langchain.chains import MapReduceDocumentsChain, ReduceDocumentsChain
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAIllm ChatOpenAI(temperature0)# 映射步骤
map_template The following is a set of documents
{docs}
Based on this list of docs, please identify the main themes
Helpful Answer:
map_prompt PromptTemplate.from_template(map_template)
map_chain LLMChain(llmllm, promptmap_prompt)# 归约步骤
reduce_template The following is set of summaries:
{docs}
Take these and distill it into a final, consolidated summary of the main themes.
Helpful Answer:
reduce_prompt PromptTemplate.from_template(reduce_template)
reduce_chain LLMChain(llmllm, promptreduce_prompt)combine_documents_chain StuffDocumentsChain(llm_chainreduce_chain, document_variable_namedocs)reduce_documents_chain ReduceDocumentsChain(combine_documents_chaincombine_documents_chain,collapse_documents_chaincombine_documents_chain,token_max4000,
)map_reduce_chain MapReduceDocumentsChain(llm_chainmap_chain,reduce_documents_chainreduce_documents_chain,document_variable_namedocs,return_intermediate_stepsFalse,
)result map_reduce_chain.invoke(docs)
print(result[output_text])方法3Refine
通过迭代文档更新滚动摘要每次根据新文档和当前摘要生成新的摘要。
chain load_summarize_chain(llm, chain_typerefine)
result chain.invoke(docs)
print(result[output_text])4. 使用AnalyzeDocumentChain
将文本拆分和摘要包装在一个链中方便操作。
from langchain.chains import AnalyzeDocumentChaintext_splitter CharacterTextSplitter.from_tiktoken_encoder(chunk_size1000, chunk_overlap0)
summarize_document_chain AnalyzeDocumentChain(combine_docs_chainchain, text_splittertext_splitter)
result summarize_document_chain.invoke(docs[0].page_content)
print(result[output_text])通过上述步骤您可以使用LangChain高效地总结多个文档的内容并为LLM提供有用的背景信息。