整合文件載入器

使用 LangChain 開發 LLM 應用

Jonathan Bennion

AI Engineer & LangChain Contributor

檢索增強生成(RAG)

  • 使用 embeddings 來 _擷取_ 相關資訊,並整合進 prompt

典型的 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 應用

一起來練習吧!

使用 LangChain 開發 LLM 應用

Preparing Video For Download...