集成文档加载器

使用 LangChain 开发 LLM 应用

Jonathan Bennion

AI Engineer & LangChain Contributor

检索增强生成(RAG)

  • 使用嵌入来检索相关信息并集成到提示中

典型的 RAG 工作流。

使用 LangChain 开发 LLM 应用

RAG 开发步骤

通用 RAG 流程:文档加载器、文档切分器,以及存储与检索过程。

使用 LangChain 开发 LLM 应用

LangChain 文档加载器

  • 用于加载和配置文档以集成到系统中的类
  • 常见文件类型的加载器:.pdf.csv
  • 第三方加载器:S3、.ipynb.wav

document-loader.jpg

1 https://python.langchain.com/docs/integrations/document_loaders
使用 LangChain 开发 LLM 应用

PDF 文档加载器

  • 需安装 pypdf 包:pip install pypdf
from langchain_community.document_loaders import PyPDFLoader

loader = PyPDFLoader("path/to/file/attention_is_all_you_need.pdf")
data = loader.load()
print(data[0])
Document(page_content='Provided proper attribution is provided, Google hereby grants 
permission to\nreproduce the tables and figures in this paper solely for use in [...]
使用 LangChain 开发 LLM 应用

CSV 文档加载器

from langchain_community.document_loaders.csv_loader import CSVLoader


loader = CSVLoader('fifa_countries_audience.csv')
data = loader.load()
print(data[0])
Document(page_content='country: United States\nconfederation: CONCACAF\npopulation_share: [...]
使用 LangChain 开发 LLM 应用

HTML 文档加载器

  • 需安装 unstructured 包:pip install unstructured
from langchain_community.document_loaders import UnstructuredHTMLLoader


loader = UnstructuredHTMLLoader("white_house_executive_order_nov_2023.html") data = loader.load()
print(data[0])
print(data[0].metadata)
page_content="To search this site, enter a search term\n\nSearch\n\nExecutive Order on the Safe, Secure,
and Trustworthy Development and Use of Artificial Intelligence\n\nHome\n\nBriefing Room\n\nPresidential
Actions\n\nBy the authority vested in me as President by the Constitution and the laws of the United
States of America, it is hereby ordered as follows: ..."

{'source': 'white_house_executive_order_nov_2023.html'}
使用 LangChain 开发 LLM 应用

Passons à la pratique !

使用 LangChain 开发 LLM 应用

Preparing Video For Download...