(五)、基于 LangChain 实现大模型应用程序开发 | 基于知识库的个性化问答 (文档加载 Document Loading)

😄 大语言模型(Large Language Model, LLM), 可以回答许多不同的问题。但是大语言模型的知识来源于其训练数据集,并没有用户的信息(比如用户的个人数据,公司的自有数据),也没有最新发生时事的信息(在大模型数据训练后发表的文章或者新闻)。因此大模型能给出的答案比较受限。

所有我们可以基于 LangChain 提供的个人数据访问能力,指导开发者如何使用 LangChain 开发能够访问用户个人数据、提供个性化服务的大模型应用。

本博客主要集中于:文档加载

  • 用户个人数据可以以多种形式呈现:PDF 文档、视频、网页等。
  • 基于 LangChain 提供给 LLM 访问用户个人数据的能力,首先要加载并处理用户的多样化、非结构化个人数据。
  • 在本博客中,我们首先介绍如何加载文档(包括文档、视频、网页等),这是访问个人数据的第一步。

文章目录

  • 0、初始化openai环境
  • 1、PDF 文档
  • 2、YouTube 音频
  • 3、网页 文档
  • 4、Notion 文档

0、初始化openai环境

from langchain.chat_models import ChatOpenAI
import os
import openai
# 运行此API配置,需要将目录中的.env中api_key替换为自己的
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.environ['OPENAI_API_KEY']

1、PDF 文档

# 安个pdf依赖库
!pip install -q pypdf

采用langchain的PyPDFLoader加载

  • 创建一个 PyPDFLoader Class 实例,输入为待加载的pdf文档路径
  • 以吴恩达老师的《2009年机器学习课程字幕文件》作为示例,下载地址:https://see.stanford.edu/materials/aimlcs229/transcripts/MachineLearning-Lecture01.pdf
from langchain.document_loaders import PyPDFLoader
loader = PyPDFLoader("./data/MachineLearning-Lecture01.pdf")
# 调用 PyPDFLoader Class 的函数 load对pdf文件进行加载
pages = loader.load()

print(len(pages)) # 可以看到输出22,刚好pdf就是22页,是按页数分块
pages
22
[Document(page_content='MachineLearning-Lecture01  \nInstructor (Andrew Ng):  Okay. Good morning. Welcome to CS229, the machine \nlearning class. So what I wanna do today is ju st spend a little time going over the logistics \nof the class, and then we\'ll start to  talk a bit about machine learning.  \nBy way of introduction, my name\'s  Andrew Ng and I\'ll be instru ctor for this class. And so \nI personally work in machine learning, and I\' ve worked on it for about 15 years now, and \nI actually think that machine learning is th e most exciting field of all the computer \nsciences. So I\'m actually always excited about  teaching this class. Sometimes I actually \nthink that machine learning is not only the most exciting thin g in computer science, but \nthe most exciting thing in all of human e ndeavor, so maybe a little bias there.  \nI also want to introduce the TAs, who are all graduate students doing research in or \nrelated to the machine learni ng and all aspects of machin e learning. Paul Baumstarck \nworks in machine learning and computer vision.  Catie Chang is actually a neuroscientist \nwho applies machine learning algorithms to try to understand the human brain. Tom Do \nis another PhD student, works in computa tional biology and in sort of the basic \nfundamentals of human learning. Zico Kolter is  the head TA — he\'s head TA two years \nin a row now — works in machine learning a nd applies them to a bunch of robots. And \nDaniel Ramage is — I guess he\'s not here  — Daniel applies l earning algorithms to \nproblems in natural language processing.  \nSo you\'ll get to know the TAs and me much be tter throughout this quarter, but just from \nthe sorts of things the TA\'s do, I hope you can  already tell that machine learning is a \nhighly interdisciplinary topic in which just the TAs find l earning algorithms to problems \nin computer vision and biology and robots a nd language. And machine learning is one of \nthose things that has and is having a large impact on many applications.  \nSo just in my own daily work, I actually frequently end up talking to people like \nhelicopter pilots to biologists to people in  computer systems or databases to economists \nand sort of also an unending stream of  people from industry coming to Stanford \ninterested in applying machine learni ng methods to their own problems.  \nSo yeah, this is fun. A couple of weeks ago, a student actually forwar ded to me an article \nin "Computer World" about the 12 IT skills th at employers can\'t say no to. So it\'s about \nsort of the 12 most desirabl e skills in all of IT and all of information technology, and \ntopping the list was actually machine lear ning. So I think this is a good time to be \nlearning this stuff and learning algorithms and having a large impact on many segments \nof science and industry.  \nI\'m actually curious about something. Learni ng algorithms is one of the things that \ntouches many areas of science and industrie s, and I\'m just kind of curious. How many \npeople here are computer science majors, are in the computer science department? Okay. \nAbout half of you. How many people are from  EE? Oh, okay, maybe about a fifth. How ', metadata={'source': './data/MachineLearning-Lecture01.pdf', 'page': 0}),
 Document(page_content="many biologers are there here? Wow, just a few, not many. I'm surprised. Anyone from \nstatistics? Okay, a few. So where are the rest of you from?  \nStudent : iCME.  \nInstructor (Andrew Ng) : Say again?  \nStudent : iCME.  \nInstructor (Andrew Ng) : iCME. Cool.  \nStudent : [Inaudible].  \nInstructor (Andrew Ng) : Civi and what else?  \nStudent : [Inaudible]  \nInstructor (Andrew Ng) : Synthesis, [inaudible] systems. Yeah, cool.  \nStudent : Chemi.  \nInstructor (Andrew Ng) : Chemi. Cool.  \nStudent : [Inaudible].  \nInstructor (Andrew Ng) : Aero/astro. Yes, right. Yeah, okay, cool. Anyone else?  \nStudent : [Inaudible].  \nInstructor (Andrew Ng) : Pardon? MSNE. All ri ght. Cool. Yeah.  \nStudent : [Inaudible].  \nInstructor (Andrew Ng) : Pardon?  \nStudent : [Inaudible].  \nInstructor (Andrew Ng) : Endo —  \nStudent : [Inaudible].  \nInstructor (Andrew Ng) : Oh, I see, industry. Okay. Cool. Great, great. So as you can \ntell from a cross-section of th is class, I think we're a very diverse audience in this room, \nand that's one of the things that makes this class fun to teach and fun to be in, I think.  ", metadata={'source': './data/MachineLearning-Lecture01.pdf', 'page': 1}),
....]

可以看到每个Document对象包含了2个属性:

  • 1、.page_conttent
  • 2、.metadata
# 选一页出来瞧瞧:page_content
pages[0].page_content
MachineLearning-Lecture01  \nInstructor (Andrew Ng):  Okay. Good morn....

# 文档页面相关的描述性数据:,meta_data
pages[0].metadata
{'source': './data/MachineLearning-Lecture01.pdf', 'page': 0}

2、YouTube 音频

在这部分的内容中,对于给定的 YouTube 视频链接,我们会详细讨论:

  • 1、利用langchain加载工具,为指定的 YouTube 视频链接下载对应的音频至本地
  • 2、通过OpenAIWhisperPaser工具,将这些音频文件转化为可读的文本内容
# 安装个依赖库
!pip -q install yt_dlp
!pip -q install pydub
!pip install ffmpeg
!pip install youtube-dl
!pip install ffprobe-python

构建一个 GenericLoader 实例来对 Youtube 视频的下载到本地并加载。

from langchain.document_loaders.generic import GenericLoader
from langchain.document_loaders.parsers import OpenAIWhisperParser
from langchain.document_loaders.blob_loaders.youtube_audio import YoutubeAudioLoader

url="https://www.youtube.com/watch?v=_PHdzsQaDgw"
save_dir="./data/"

# 创建一个 GenericLoader Class 实例
loader = GenericLoader(
    #将链接url中的Youtube视频的音频下载下来,存在本地路径save_dir
    YoutubeAudioLoader([url],save_dir), 
    
    #使用OpenAIWhisperPaser解析器将音频转化为文本
    OpenAIWhisperParser()
)

# 调用 GenericLoader Class 的函数 load对视频的音频文件进行加载
pages = loader.load()


print(pages)
print("Page_content: ", pages[0].page_content)
print("Meta Data: ", pages[0].metadata)
[Document(page_content='大家好,欢....', metadata={'source': 'data})]
Page_content: 大家好,欢迎来到我的频道 今天...
Meta Data:  {'source': 'data....}

3、网页 文档

以 GitHub 上的一个markdown格式文档为例,学习如何对其进行加载
构建一个WebBaseLoader实例来对网页进行加载

from langchain.document_loaders import WebBaseLoader


# 创建一个 WebBaseLoader Class 实例
url = "https://github.com/datawhalechina/d2l-ai-solutions-manual/blob/master/docs/README.md"
header = {'User-Agent': 'python-requests/2.27.1', 
          'Accept-Encoding': 'gzip, deflate, br', 
          'Accept': '*/*',
          'Connection': 'keep-alive'}
loader = WebBaseLoader(web_path=url,header_template=header)

# 调用 WebBaseLoader Class 的函数 load对文件进行加载
pages = loader.load()



print("Type of pages: ", type(pages))
print("Length of pages: ", len(pages))

page = pages[0]
print("Type of page: ", type(page))
print("Page_content: ", page.page_content)
print("Meta Data: ", page.metadata)

在这里插入图片描述

因为是网页,数据非常脏,通常来讲,我们需要进行对这种数据进行进一步处理(Post Processing)

import json
convert_to_json = json.loads(page.page_content)
extracted_markdow = convert_to_json['payload']['blob']['richText'] # 提取出markdown的内容
print(extracted_markdow)

在这里插入图片描述

4、Notion 文档

  • 点击Notion示例文档(https://yolospace.notion.site/Blendle-s-Employee-Handbook-e31bff7da17346ee99f531087d8b133f)。右上方复制按钮(Duplicate),复制文档到你的Notion空间
  • 点击右上方⋯ 按钮,选择导出为Mardown&CSV。导出的文件将为zip文件夹
  • 解压并保存mardown文档到本地路径docs/Notion_DB/

使用NotionDirectoryLoader来对Notion Markdown文档进行加载

from langchain.document_loaders import NotionDirectoryLoader
loader = NotionDirectoryLoader("./data/Notion_DB")
pages = loader.load()

print("Length of pages: ", len(pages))

page = pages[0]
print("Type of page: ", type(page))
print("Page_content: ", page.page_content)
print("Meta Data: ", page.metadata)

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/167513.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

算法-二叉树-简单-二叉树的最大和最小深度

记录一下算法题的学习7 二叉树的最大深度 题目:给定一个二叉树 root ,返回其最大深度。 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。 输入:root [3,9,20,null,null,15,7] 输出:3 示例分析&#xff…

概念解析 | 光电神经网络:optoelectronic neural network

注1:本文系“概念解析”系列之一,致力于简洁清晰地解释、辨析复杂而专业的概念。本次辨析的概念是:光电神经网络。 概念解析 | 光电神经网络的原理、挑战与未来 1. 背景介绍 在过去的十年中,深度学习和神经网络在许多领域取得了显著的成就,如图像识别、自然语言处理、医疗…

【大数据开发】FineReport报表基础入门

博主:👍不许代码码上红 欢迎:🐋点赞、收藏、关注、评论。 格言: 大鹏一日同风起,扶摇直上九万里。 文章目录 一 登录账号二 创建一个新的表格三 单元格扩展3.1 无扩展3.2 纵向扩展3.3 横向扩展 四 父子格…

代码随想录算法训练营第四十一天【动态规划part03】 | 343. 整数拆分、96.不同的二叉搜索树

343. 整数拆分 题目链接: 力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台 求解思路: 动规五部曲 确定dp数组及其下标含义:dp[i] 拆分i,可以得到的最大乘积为dp[i]确定递推公式:从1开始遍…

一周互联网简讯 | 本周互联网发生了啥?(第3期)

1.百度T7跳槽字节3-1,总包145万,压力太大想降级 硕士毕业工作10年,一百度T7大头兵发文称,自己最近拿到字节3-1的offer,年包从现有的110万涨30%到145万。但是担心去字节后因为定的职级高需要带人,压力会很大…

nginx代理本地服务请求,避免跨域;前端图片压缩并上传

痛点 有时用vscode进行一些测试 请求不同端口服务、或者其他服务接口时时,老是会报跨域,非常的烦 所有就想用 nginx 进行请求代理,来解决这个痛点 nginx 下载地址:nginx: download 下载到某一目录: window下nginx相关…

10个值得关注的即时通讯软件开发趋势

作为即时通讯软件开发领域的专家,以下是我对即时通讯软件开发的十个值得关注的趋势的分享。 1. 云通信技术的进步 随着云计算和网络技术的不断发展,云通信技术在即时通讯软件开发中扮演着越来越重要的角色。通过使用云通信技术,开发者可以实…

文具办公产品展示预约小程序的作用如何

从整体来看,文具办公品牌/门店的生意来源于线下自然流量或线上自营商城/入驻第三方商城的的流量,线上多数情况都是以直接销售配送为主,但其实对文具品牌/门店而言还有信息展示、服务预约、在线咨询、产品介绍等需求。 虽然小区周边的消费者需…

vue安装three.js并创建第一个入门场景

vue安装three.js,并创建第一个入门场景 安装three.js npm install --save three引入three.js import * as THREE from threethree.js结构 three.js坐标 创建一个场景 scene场景,camera相机,renderer渲染器 创建一个场景 this.scene new T…

从矿源到指尖——周大福天然钻石的非凡实力

(2023年11月20日,北京)在近百年历程中,周大福珠宝集团一直致力珠宝工艺传承与创新设计的孕育,于1929年创立周大福品牌,凭借对中国传统黄金工艺的传承与创新、对中国传统文化的融合与发扬,将黄金…

阿里云oss使用签名url上传时的一些配置注意事项

我来讲一下测试下来遇到的问题点和解决方案: 一、配置相关问题 你可以先按照阿里云的文档把一些oss的基本配置弄好,再看下面的内容; 配置跨域访问规则; 这是非常重要的一步。默认情况下,oss不允许上传文件时携带Cont…

分享购的实战攻略:让您轻松掌握流量密码

​小编介绍:10年专注商业模式设计及软件开发,擅长企业生态商业模式,商业零售会员增长裂变模式策划、商业闭环模式设计及方案落地;扶持10余个电商平台做到营收过千万,数百个平台达到百万会员,欢迎咨询。 分…

从0开始学习JavaScript--JavaScript中的对象

JavaScript中的对象是一种重要的数据结构,它不仅是语言的基石,还提供了丰富的功能和灵活性。本文将深入研究JavaScript对象的创建、属性访问、方法定义,以及实际应用中的技巧,通过丰富的示例代码,帮助读者更全面地了解…

Blender洪水淹没毁墙效果

本文中用到了两个Blender插件:FLIP Fluid(流体模拟相关插件) 和 RBDLab(碎裂插件): 1.用FLIP Fluid制作流体、域、障碍,确定好流体的冲刷方向(后期好摆放被摧毁的墙体),利用插件做出水流动画&a…

HarmonyOS开发(四):应用程序入口UIAbility

1、UIAbility概述 UIAbility 一种包含用户界面的应用组件用于与用户进行交互系统调度的单元为应用提供窗口在其中绘制界同 注:每一个UIAbility实例,都对应一个最近任务列表中的任务。 一个应用可以有一个UIAbility也可以有多个UIAbility。 如一般的…

.NET8.0 AOT 经验分享 - 专项测试各大 ORM 是否支持

AOT 特点 发布和部署本机 AOT 应用具有以下优势: 最大程度减少磁盘占用空间:使用本机 AOT 发布时,将生成一个可执行文件,其中仅包含支持程序所需的外部依赖项的代码。减小的可执行文件大小可能会导致:较小的容器映像&a…

求二叉树的宽度(可执行)

输入:abd##e##cf### 输出结果:3 运行环境.cpp 注意:若无运行结果,则一定是建树错误 #include "bits/stdc.h" using namespace std; typedef struct BiTNode{char data;struct BiTNode *lchild,*rchild; }BiTNode,*Bi…

Python编程基础(华为在线课程)

一、免费课程链接 https://e.huawei.com/cn/talent/outPage/#/sxz-course/home?courseId3mCm7X8-UyWyS6pioCSJeUI0yFo 二、学习环境搭建 0、参考文章 搭建 Python 高效开发环境: Pycharm Anaconda - 知乎 1、下载anaconda 官网地址: https://ww…

手机数码类展示预约小程序效果如何

对于一家手机数码/电脑品牌来说,研发产品或衍生产品不少,通常会通过线上商城进行售卖。十年以来,流量成本逐渐增加,获客不易也难以寻找到合适的渠道,即使通过广告形式也因缺乏创意而耗时耗力,效果不佳。 同…

YB506AB是一款理电池充、放电管理专用芯片,集成锂电池充电管理和降压DC-DC电路。

YB506AB 锂电转可充电AA/AAA电池专用SOC芯片 概述: YB506AB是一款理电池充、放电管理专用芯片,集成锂电池充电管理和降压DC-DC电路。充电过程满足锂电池三段式滑流/恒流/恒压充电规范,B506内部的线性充电电路采用了恒流可配置模式,可以通过…