
背景描述
LangChain 提供了多种文档加载器,包括但不限于以下几种:
- TextLoader:用于从各种来源加载文本数据。
- CSVLoader:用于加载 CSV 文件并将其转换为 LangChain 可以处理的文档格式。
- UnstructuredFileLoader:能够自动检测并处理不同格式的文件。
- DirectoryLoader:用于加载指定文件夹中的文件。
- UnstructuredHTMLLoader:用于从 HTML 文件中提取有意义的内容。
- JSONLoader:用于加载和处理 JSON 文件。
- PyPDFLoader:用于加载 PDF 文件。
- ArxivLoader:专门用于加载来自 Arxiv 的文档。
安装依赖
| pip install -qU langchain-core langchain-openai |
复制
加载Text
编写代码
| from langchain_community.document_loaders import TextLoader |
| |
| loader = TextLoader("./index.md") |
| data = loader.load() |
| print(data) |
复制
运行结果
| ➜ python3 test20.py |
| [Document(page_content='# hello world!\nthis is a markdown!\n', metadata={'source': './index.md'})] |
复制
加载CSV
编写代码
| from langchain_community.document_loaders.csv_loader import CSVLoader |
| |
| |
| loader = CSVLoader(file_path='./example_data/mlb_teams_2012.csv') |
| data = loader.load() |
| print(data) |
复制
运行结果
| loader = CSVLoader(file_path='./example_data/mlb_teams_2012.csv', csv_args={ |
| 'delimiter': ',', |
| 'quotechar': '"', |
| 'fieldnames': ['MLB Team', 'Payroll in millions', 'Wins'] |
| }) |
| |
| data = loader.load() |
| print(data) |
复制
加载目录
编写代码
| from langchain_community.document_loaders import DirectoryLoader |
| |
| loader = DirectoryLoader('../', glob="**/*.md") |
| docs = loader.load() |
| print(docs) |
| |
| |
| loader = DirectoryLoader('../', glob="**/*.md", show_progress=True) |
| |
| |
| loader = DirectoryLoader('../', glob="**/*.md", use_multithreading=True) |
| |
| |
| text_loader_kwargs={'autodetect_encoding': True} |
| loader = DirectoryLoader(path, glob="**/*.txt", loader_cls=TextLoader, loader_kwargs=text_loader_kwargs) |
| |
复制
加载HTML
编写代码
| from langchain_community.document_loaders import UnstructuredHTMLLoader |
| from langchain_community.document_loaders import BSHTMLLoader |
| |
| loader = UnstructuredHTMLLoader("example_data/fake-content.html") |
| data = loader.load() |
| print(data) |
| |
| |
| loader = BSHTMLLoader("example_data/fake-content.html") |
| data = loader.load() |
| print(data) |
复制
加载JSON
编写代码
| from langchain_community.document_loaders import JSONLoader |
| |
| import json |
| from pathlib import Path |
| from pprint import pprint |
| |
| |
| file_path='./example_data/facebook_chat.json' |
| data = json.loads(Path(file_path).read_text()) |
| pprint(data) |
| |
| |
| loader = JSONLoader( |
| file_path='./example_data/facebook_chat.json', |
| jq_schema='.messages[].content', |
| text_content=False) |
| |
| data = loader.load() |
| pprint(data) |
复制
加载JSON LINES
编写代码
| from langchain_community.document_loaders import JSONLoader |
| |
| import json |
| from pathlib import Path |
| from pprint import pprint |
| |
| file_path = './example_data/facebook_chat_messages.jsonl' |
| pprint(Path(file_path).read_text()) |
| |
| loader = JSONLoader( |
| file_path='./example_data/facebook_chat_messages.jsonl', |
| jq_schema='.content', |
| text_content=False, |
| json_lines=True) |
| |
| data = loader.load() |
| pprint(data) |
复制
加载Markdown
编写代码
| from langchain_community.document_loaders import UnstructuredMarkdownLoader |
| |
| markdown_path = "../../../../../README.md" |
| loader = UnstructuredMarkdownLoader(markdown_path) |
| data = loader.load() |
| |
复制
加载PDF
安装依赖
| pip install pypdf |
| pip install rapidocr-onnxruntime |
复制
编写代码
| from langchain_community.document_loaders import PyPDFLoader |
| |
| |
| loader = PyPDFLoader("example_data/layout-parser-paper.pdf") |
| pages = loader.load_and_split() |
| print(pages[0]) |
| |
| |
| loader = PyPDFLoader("https://arxiv.org/pdf/2103.15348.pdf", extract_images=True) |
| pages = loader.load() |
| pages[4].page_content |
复制
向量化数据(简单例子 详细可看该系列的其他文章)
编写代码
| from langchain_community.vectorstores import FAISS |
| from langchain_openai import OpenAIEmbeddings |
| |
| faiss_index = FAISS.from_documents(pages, OpenAIEmbeddings()) |
| docs = faiss_index.similarity_search("How will the community be engaged?", k=2) |
| for doc in docs: |
| print(str(doc.metadata["page"]) + ":", doc.page_content[:300]) |
复制