告别复制粘贴!用Langchain让大模型直接“阅读”你的文件,打造专属AI知识库!

Langchain 使用文档加载器从各种来源获取信息并准备处理。这些加载器充当数据连接器,获取信息并将其转换为 Langchain 可以理解的格式。

但是实际使用过程中,这些解析的效果层次补齐,需要结合自己的文件去写如何加载具体文档。这个也是在后续开发框架的过程中,我们可以选取langchian的document作为处理对象,但是文件解析需要自己去写和实现。

在本章中,我们将介绍其中的一些:

  • TextLoader
  • CSVLoader
  • UnstructuredFileLoader
  • DirectoryLoader
  • UnstructuredHTMLLoader
  • JSONLoader
  • PyPDFLoader
  • ArxivLoader
  • Docx2txtLoader

一、TextLoader

"text.txt""""[Document(page_content='I have some instructions here.\nThis is the second row.', metadata={'source': 'text.txt'})]""""index.md""""[Document(page_content='some instructions\n', metadata={'source': 'index.md'})]"""

二、CSVLoader

# Create a simple DataFrame'Name''Alice''Bob''Charlie''Age''City''New York''Los Angeles''Chicago'# Export the DataFrame to a CSV file'sample_data.csv''sample_data.csv'"""[Document(page_content='Name: Alice\nAge: 25\nCity: New York', metadata={'source': 'sample_data.csv', 'row': 0}), Document(page_content='Name: Bob\nAge: 30\nCity: Los Angeles', metadata={'source': 'sample_data.csv', 'row': 1}), Document(page_content='Name: Charlie\nAge: 35\nCity: Chicago', metadata={'source': 'sample_data.csv', 'row': 2})]"""

如有必要,我们可以在读取文件时自定义 CSV 参数:

'sample_data.csv''delimiter'',''quotechar''"''fieldnames''Name''Age''City'# now the headers are also a row."""[Document(page_content='Name: Name\nAge: Age\nCity: City', metadata={'source': 'sample_data.csv', 'row': 0}), Document(page_content='Name: Alice\nAge: 25\nCity: New York', metadata={'source': 'sample_data.csv', 'row': 1}), Document(page_content='Name: Bob\nAge: 30\nCity: Los Angeles', metadata={'source': 'sample_data.csv', 'row': 2}), Document(page_content='Name: Charlie\nAge: 35\nCity: Chicago', metadata={'source': 'sample_data.csv', 'row': 3})]"""

当从 CSV 文件加载数据时,加载器通常会为 CSV 中的每一行数据创建一个单独的“文档”对象。

默认情况下,每个文档的来源都设置为 CSV 本身的整个文件路径。如果想跟踪 CSV 中每条信息的来源,这可能并不理想。

可以使用 source_column 指定 CSV 文件中的列名。然后,每行特定列中的值将用作从该行创建的相应文档的单独来源

'sample_data.csv'"Name""""[Document(page_content='Name: Alice\nAge: 25\nCity: New York', metadata={'source': 'Alice', 'row': 0}), Document(page_content='Name: Bob\nAge: 30\nCity: Los Angeles', metadata={'source': 'Bob', 'row': 1}), Document(page_content='Name: Charlie\nAge: 35\nCity: Chicago', metadata={'source': 'Charlie', 'row': 2})]"""

这在使用涉及根据信息来源回答问题的“链”(可能是数据处理管道)时特别有用。通过为每个文档提供单独的源信息,这些链可以在处理时考虑数据的来源,并可能提供更细致入微或更可靠的答案。

三、UnstructuredCSVLoader

CSVLoader不同,CSVLoader将每一行视为一个单独的文档,并使用标题定义数据,而在UnstructuredCSVLoader中,整个 CSV 文件被视为单个“非结构化表”元素。当您想要将数据作为整个表而不是单个条目进行分析时,这很有用。

"sample_data.csv""elements""""[Document(page_content='\n\n\nName\nAge\nCity\n\n\nAlice\n25\nNew York\n\n\nBob\n30\nLos Angeles\n\n\nCharlie\n35\nChicago\n\n\n', metadata={'source': 'sample_data.csv', 'filename': 'sample_data.csv', 'languages': ['eng'], 'last_modified': '2024-03-04T18:05:41', 'text_as_html': '<table border="" class="">\n  <tbody>\n    <tr>\n      <td>Name</td>\n      <td>Age</td>\n      <td>City</td>\n    </tr>\n    <tr>\n      <td>Alice</td>\n      <td>25</td>\n      <td>New York</td>\n    </tr>\n    <tr>\n      <td>Bob</td>\n      <td>30</td>\n      <td>Los Angeles</td>\n    </tr>\n    <tr>\n      <td>Charlie</td>\n      <td>35</td>\n      <td>Chicago</td>\n    </tr>\n  </tbody>\n</table>', 'filetype': 'text/csv', 'category': 'Table'})]"""

如果在“元素”模式下操作,则表的 HTML 表示将可在元数据中访问。

print"text_as_html""""<table border="" class="">  <tbody>    <tr>      <td>Name</td>      <td>Age</td>      <td>City</td>    </tr>    <tr>      <td>Alice</td>      <td>25</td>      <td>New York</td>    </tr>    <tr>      <td>Bob</td>      <td>30</td>      <td>Los Angeles</td>    </tr>    <tr>      <td>Charlie</td>      <td>35</td>      <td>Chicago</td>    </tr>  </tbody></table>"""

四、UnstructuredFileLoader

TextLoader等专为特定格式设计的加载器不同,UnstructuredFileLoader会自动检测您提供的文件类型。

加载器利用了底层的“unstructured”库。该库会分析文件内容并尝试根据文件类型提取有意义的信息。

"text.txt""""[Document(page_content='I have some instructions here.\n\nThis is the second row.', metadata={'source': 'text.txt'})]""""text.txt""elements""""[Document(page_content='I have some instructions here.', metadata={'source': 'text.txt', 'filename': 'text.txt', 'last_modified': '2024-03-04T18:15:12', 'languages': ['eng'], 'filetype': 'text/plain', 'category': 'NarrativeText'}), Document(page_content='This is the second row.', metadata={'source': 'text.txt', 'filename': 'text.txt', 'last_modified': '2024-03-04T18:15:12', 'languages': ['eng'], 'filetype': 'text/plain', 'category': 'NarrativeText'})]""""your_report.html""""[Document(page_content='Toggle navigation\n\nPandas Profiling Report\n\nOverview\n\nVariables\n\nInteractions\n\nCorrelations\n\nMissing values\n\nSample\n\nOverview\n\nOverview\n\nAlerts 44\n\nReproduction\n\nDataset statistics\n\nNumber of variables 44 Number of observations 58592 Missing cells 0 Missing cells (%) 0.0% Duplicate rows 0 Duplicate rows (%) 0.0% Total size in memory 19.7 MiB Average record size in memory 352.0 B\n\nVariable types\n\nText 1 Numeric 10 Categorical 16 Boolean 17\n\nairbags is highly overall correlated with cylinder and 28 other fields High correlation cylinder is highly overall correlated with airbags and 22 other fields High correlation displacement is highly overall correlated with airbags and 33 other fields High correlation engine_type is highly overall correlated with airbags and 30 other fields High correlation fuel_type is highly overall correlated with airbags and 30 other fields High correlation gear_box is highly overall correlated with airbags and 23 other fields High correlation gross_weight is highly overall correlated with airbags and 32 other fields High correlation height is highly overall correla"""# pip install "unstructured[pdf]""ticket.pdf""""[Document(page_content='Event\n\nCommence Date\n\nReference\n\nPaul Kalkbrenner\n\n10 September,Satuinfo@biletino.com', metadata={'source': 'ticket.pdf'})]"""

五、DirectoryLoader

DirectoryLoader可帮助一次性从整个目录加载多个文档。它利用了UnstructuredFileLoader

'folder/'print# 3# we can declare extension, display progress bar, use multithreading'folder/'"*.txt"print# 1

六、UnstructuredHTMLLoader

它利用“非结构化”库的功能从存储为 HTML 文件的网页中提取有意义的内容。

"en""UTF-8""viewport""width=device-width, initial-scale=1.0"
``````plaintext
"index.html""""[Document(page_content='A div element\n\na p element\n\na p inside of a div', metadata={'source': 'index.html'})]"""

我们可以使用BeautifulSoup4通过BSHTMLLoader来解析 HTML 文档。

"index.html""""[Document(page_content='\n\n\n\nDocument\n\n\nA div element\na p element\n\na p inside of a div\n\n\n\n', metadata={'source': 'index.html', 'title': 'Document'})]"""

七、JSONLoader

JSONLoader 被设计用于处理以 JSON 形式存储的数据。

"id""name""John Doe""email""john.doe@example.com""age""city""New York""id""name""Jane Smith""email""jane.smith@example.com""age""city""Los Angeles""id""name""Alice Johnson""email""alice.johnson@example.com""age""city""Chicago"

JSONLoaders利用 JQ 库来解析 JSON 数据。JQ 提供了一种专为处理 JSON 结构而设计的强大查询语言。

jq_schema参数允许在 JSONLoader 函数中提供 JQ 表达式。

'example.json''map({ name, email })'"""[Document(page_content="'name''John Doe''email''john.doe@example.com''name''Jane Smith''email''jane.smith@example.com''name''Alice Johnson''email''alice.johnson@example.com'", metadata={'source': '/Users/okanyenigun/Desktop/codes/python__general/example.json', 'seq_num': 1})]"""

JSON 行文件是一个文本文件,其中每行都是一个有效的 JSON 对象,由换行符分隔。

"name""John Doe""age""name""Jane Smith""age""name""Alice Johnson""age"'example.jsonl''.content'
``````plaintext
"""[Document(page_content='', metadata={'source': '/Users/okanyenigun/Desktop/codes/python__general/example.jsonl', 'seq_num': 1}), Document(page_content='', metadata={'source': '/Users/okanyenigun/Desktop/codes/python__general/example.jsonl', 'seq_num': 2}), Document(page_content='', metadata={'source': '/Users/okanyenigun/Desktop/codes/python__general/example.jsonl', 'seq_num': 3})]"""

八、PyPDFLoader

它利用 pypdf 库来加载 PDF 文件。

"ticket.pdf""""Document(page_content='Paul Kalkbrenner\nThis electronically generated document will grant you entry to the event and time specified on this ticket. The security of the ticket belongs to the\nowner...Sarıyer, İstanbul', metadata={'source': 'ticket.pdf', 'page': 0})"""

我们还可以使用 UnstructuredPDFLoader 来加载 PDF。

"ticket.pdf"

我们有 OnlinePDFLoader 来加载在线 PDF。

"https://arxiv.org/pdf/2302.03803.pdf""""[Document(page_content='3 2 0 2\n\nb e F 7\n\n]\n\nG A . h t a m\n\n[\n\n1 v 3 0 8 3 0 . 2 0 3 2 : v i X r a\n\nA WEAK (k, k)-LEFSCHETZ THEOREM FOR PROJECTIVE TORIC ORBI..."""

还有更多利用不同来源的……

# PyPDFium2Loader"ticket.pdf"# PDFMinerLoader"ticket.pdf"# PDFMinerPDFasHTMLLoader"ticket.pdf"# entire PDF is loaded as a single Document# PyMuPDFLoader"ticket.pdf"# Directory loader for PDF"folder/"

如何学习大模型 AI ?

由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。

但是具体到个人,只能说是:

“最先掌握AI的人,将会比较晚掌握AI的人有竞争优势”。

这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。

我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。

我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。

在这里插入图片描述

第一阶段(10天):初阶应用

该阶段让大家对大模型 AI有一个最前沿的认识,对大模型 AI 的理解超过 95% 的人,可以在相关讨论时发表高级、不跟风、又接地气的见解,别人只会和 AI 聊天,而你能调教 AI,并能用代码将大模型和业务衔接。

  • 大模型 AI 能干什么?
  • 大模型是怎样获得「智能」的?
  • 用好 AI 的核心心法
  • 大模型应用业务架构
  • 大模型应用技术架构
  • 代码示例:向 GPT-3.5 灌入新知识
  • 提示工程的意义和核心思想
  • Prompt 典型构成
  • 指令调优方法论
  • 思维链和思维树
  • Prompt 攻击和防范

第二阶段(30天):高阶应用

该阶段我们正式进入大模型 AI 进阶实战学习,学会构造私有知识库,扩展 AI 的能力。快速开发一个完整的基于 agent 对话机器人。掌握功能最强的大模型开发框架,抓住最新的技术进展,适合 Python 和 JavaScript 程序员。

  • 为什么要做 RAG
  • 搭建一个简单的 ChatPDF
  • 检索的基础概念
  • 什么是向量表示(Embeddings)
  • 向量数据库与向量检索
  • 基于向量检索的 RAG
  • 搭建 RAG 系统的扩展知识
  • 混合检索与 RAG-Fusion 简介
  • 向量模型本地部署

第三阶段(30天):模型训练

恭喜你,如果学到这里,你基本可以找到一份大模型 AI相关的工作,自己也能训练 GPT 了!通过微调,训练自己的垂直大模型,能独立训练开源多模态大模型,掌握更多技术方案。

到此为止,大概2个月的时间。你已经成为了一名“AI小子”。那么你还想往下探索吗?

  • 为什么要做 RAG
  • 什么是模型
  • 什么是模型训练
  • 求解器 & 损失函数简介
  • 小实验2:手写一个简单的神经网络并训练它
  • 什么是训练/预训练/微调/轻量化微调
  • Transformer结构简介
  • 轻量化微调
  • 实验数据集的构建

第四阶段(20天):商业闭环

对全球大模型从性能、吞吐量、成本等方面有一定的认知,可以在云端和本地等多种环境下部署大模型,找到适合自己的项目/创业方向,做一名被 AI 武装的产品经理。

  • 硬件选型
  • 带你了解全球大模型
  • 使用国产大模型服务
  • 搭建 OpenAI 代理
  • 热身:基于阿里云 PAI 部署 Stable Diffusion
  • 在本地计算机运行大模型
  • 大模型的私有化部署
  • 基于 vLLM 部署大模型
  • 案例:如何优雅地在阿里云私有部署开源大模型
  • 部署一套开源 LLM 项目
  • 内容安全
  • 互联网信息服务算法备案

学习是一个过程,只要学习就会有挑战。天道酬勤,你越努力,就会成为越优秀的自己。

如果你能在15天内完成所有的任务,那你堪称天才。然而,如果你能完成 60-70% 的内容,你就已经开始具备成为一名大模型 AI 的正确特征了。

这份完整版的大模型 AI 学习资料已经上传优快云,朋友们如果需要可以微信扫描下方优快云官方认证二维码免费领取【保证100%免费

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值