基于GPT-4和LangChain构建云端定制化PDF知识库AI聊天机器人

news2024/9/29 1:24:37

参考:

GitHub - mayooear/gpt4-pdf-chatbot-langchain: GPT4 & LangChain Chatbot for large PDF docs

1.摘要:

使用新的GPT-4 api为多个大型PDF文件构建chatGPT聊天机器人。

使用的技术栈包括LangChain, Pinecone, Typescript, Openai和Next.js。LangChain是一个框架,可以更容易地构建可扩展的AI/LLM大语言模型应用程序和聊天机器人。Pinecone是一个矢量存储,用于存储嵌入和文本格式的PDF,以便以后检索类似的文档。

2.准备工作:

OpenAI API Key GPT-3.5或者GPT-4 openai 

Pinecone API Key/Environment/Index  pinecone

Pinecone Starter(免费)计划用户的Index在7天后被删除。为了防止这种情况,在7天之前向Pinecone发送API请求重置计数器。就可以继续免费使用了。

3.克隆或下载项目gpt4-pdf-chatbot-langchain

git clone https://github.com/mayooear/gpt4-pdf-chatbot-langchain.git

4.安装依赖包

使用npm安装yarn,如果没有npm,参考安装 

npm/Node.js介绍及快速安装 - Linux CentOS_Entropy-Go的博客-CSDN博客

npm install yarn -g

 再使用yarn安装依赖包

 进入项目根目录,执行命令

yarn install

安装成功后,可以看到 node_modules 目录

gpt4-pdf-chatbot-langchain-main$ ls -a
.           declarations  .eslintrc.json  node_modules        .prettierrc  styles               utils           yarn.lock
..          docs          .gitignore      package.json        public       tailwind.config.cjs  venv
components  .env          .idea           pages               README.md    tsconfig.json        visual-guide
config      .env.example  next.config.js  postcss.config.cjs  scripts      types                yarn-error.log

5.环境配置

将.env.example复制成.env配置文件

OPENAI_API_KEY=sk-xxx

# Update these with your pinecone details from your dashboard.
# PINECONE_INDEX_NAME is in the indexes tab under "index name" in blue
# PINECONE_ENVIRONMENT is in indexes tab under "Environment". Example: "us-east1-gcp"
PINECONE_API_KEY=xxx
PINECONE_ENVIRONMENT=us-west1-gcp-free
PINECONE_INDEX_NAME=xxx

config/pinecone.ts修改

在config文件夹中,将PINECONE_NAME_SPACE替换为一个namespace,当你运行npm run ingest时,你想在这个namespace中存储嵌入到PINECONE_NAME_SPACE。这个namespace稍后将用于查询和检索。

修改聊天机器人的提示词和OpenAI模型

utils/makechain.ts中为您自己的用例更改QA_PROMPT。

如果您可以访问gpt-4 api,请将新OpenAI中的modelName更改为gpt-4。请在此repo之外验证您是否可以访问gpt-4 api,否则应用程序将无法工作。

import { OpenAI } from 'langchain/llms/openai';
import { PineconeStore } from 'langchain/vectorstores/pinecone';
import { ConversationalRetrievalQAChain } from 'langchain/chains';

const CONDENSE_PROMPT = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.

Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:`;

const QA_PROMPT = `You are a helpful AI assistant. Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say you don't know. DO NOT try to make up an answer.
If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context.

{context}

Question: {question}
Helpful answer in markdown:`;

export const makeChain = (vectorstore: PineconeStore) => {
  const model = new OpenAI({
    temperature: 0, // increase temepreature to get more creative answers
    modelName: 'gpt-3.5-turbo', //change this to gpt-4 if you have access
  });

  const chain = ConversationalRetrievalQAChain.fromLLM(
    model,
    vectorstore.asRetriever(),
    {
      qaTemplate: QA_PROMPT,
      questionGeneratorTemplate: CONDENSE_PROMPT,
      returnSourceDocuments: true, //The number of source documents returned is 4 by default
    },
  );
  return chain;
};

6.添加PDF文档为知识库

因为会和OpenAI和Pinecone有数据交互,建议上传文档之前,慎重考虑数据隐私和安全。

将1个或多个PDF文档上传到 docs 目录下

执行上传命令

npm run ingest

在Pinecone上检查是否上传成功

7.运行知识库聊天机器人

当你验证了嵌入和内容已经成功地添加到你的Pinecone中,你可以运行应用程序npm run dev来启动本地开发环境,然后在聊天界面中输入一个问题,进行对话。

执行命令:

npm run dev

8.常见问题Troubleshooting

https://github.com/mayooear/gpt4-pdf-chatbot-langchain#troubleshooting

In general, keep an eye out in the issues and discussions section of this repo for solutions.

General errors

  • Make sure you're running the latest Node version. Run node -v
  • Try a different PDF or convert your PDF to text first. It's possible your PDF is corrupted, scanned, or requires OCR to convert to text.
  • Console.log the env variables and make sure they are exposed.
  • Make sure you're using the same versions of LangChain and Pinecone as this repo.
  • Check that you've created an .env file that contains your valid (and working) API keys, environment and index name.
  • If you change modelName in OpenAI, make sure you have access to the api for the appropriate model.
  • Make sure you have enough OpenAI credits and a valid card on your billings account.
  • Check that you don't have multiple OPENAPI keys in your global environment. If you do, the local env file from the project will be overwritten by systems env variable.
  • Try to hard code your API keys into the process.env variables if there are still issues.

Pinecone errors

  • Make sure your pinecone dashboard environment and index matches the one in the pinecone.ts and .env files.
  • Check that you've set the vector dimensions to 1536.
  • Make sure your pinecone namespace is in lowercase.
  • Pinecone indexes of users on the Starter(free) plan are deleted after 7 days of inactivity. To prevent this, send an API request to Pinecone to reset the counter before 7 days.
  • Retry from scratch with a new Pinecone project, index, and cloned repo.

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Day12-2-面向对象编程

Day12-面向对象编程 一 回顾 变量,数组,对象都是容器,都可以用来存储数据 let n = 10 let arr = [3,5,7] let stu = {name:"张恒",age:18,sex:"女"}二 面向对象思想 面向过程:将开发的步骤按照顺序一步一步往下执行,直到程序结束 面向对象:将项目中…

第2步---MySQL卸载和图形化工具展示

第2步---MySQL卸载和图形化工具展示 1.MySQL的卸载 2.MySQL的图形化工具 2.1常见的图形化工具 SQLyog:简单。SQLyog首页、文档和下载 - MySQL 客户端工具 - OSCHINA - 中文开源技术交流社区 Mysql Workbench :MySQL :: MySQL Workbench DataGrip&…

百度吴甜重磅发布文心一言面向开发者的三大举措,激活生态创新

近日,第九届WAVE SUMMIT深度学习开发者大会在京举办。百度集团副总裁、深度学习技术及应用国家工程研究中心副主任吴甜分享了百度自研大语言模型“文心一言”的最新进展,重磅发布5个原生插件,面向开发者正式推出AI Studio星河大模型社区、插件…

如何限制PDF打印?限制清晰度?

想要限制PDF文件的打印功能,想要限制PDF文件打印清晰度,都可以通过设置限制编辑来达到目的。 打开PDF编辑器,找到设置限制编辑的界面,切换到加密状态,然后我们就看到 有印刷许可。勾选【权限密码】输入一个PDF密码&am…

【Python从入门到进阶】32、bs4的基本使用

接上篇《31、使用JsonPath解析淘票票网站地区接口数据》 上一篇我们介绍了如何使用JSONPath来解析淘票票网站的地区接口数据,本篇我们来学习BeautifulSoup的基本概念,以及bs4的基本使用。 一、BeautifulSoup简介 1、bs4基本概念 BeautifulSoup是一个P…

ssm网络游戏公司官方平台源码和文档

ssm网络游戏公司官方平台源码和文档029 开发工具:idea 数据库mysql5.7 数据库链接工具:navcat,小海豚等 技术:ssm 摘 要 互联网发展至今,无论是其理论还是技术都已经成熟,而且它广泛参与在社会中的方方面面。它…

git协议实现管理(三个步骤)

GitHub官网访问: https://github.com/dashboard 初次使用git的用户要使用git协议大概需要三个步骤: 一、生成密钥对 二、设置远程仓库(本文以github为例)上的公钥 三、把git的remote url远程仓库URL可访问路径修改为git协议(以上两个步骤初次设置过以后&#xff0c…

react 11之 router6路由 (两种路由模式、两种路由跳转、两种传参与接收参数、嵌套路由,layout组件、路由懒加载)

目录 react路由1:安装和两种模式react路由2:两种路由跳转 ( 命令式与编程式)2-1 路由跳转-命令式2-2 路由跳转-编程式 - 函数组件2-2-1 app.jsx2-2-2 page / Home.jsx2-2-3 page / About.jsx2-2-4 效果 react路由3:函数…

OpenCL实现SobelFilter(行列分解)

1.行列分解数学原理 row_filter[1 0 -1], col_filter[1 2 1] row_filter[1 2 1], col_filter[1 0 -1] 2.非局部内存实现 __kernel void sobel_filter_separable(__global uchar* padSrc, __global uchar* dst, int height, int width, int pad_width) {__local short local_ou…

【机器学习 | 分类指标大全】全面解析分类评估指标:从准确率到AUC,多分类问题也不在话下, 确定不来看看?

🤵‍♂️ 个人主页: AI_magician 📡主页地址: 作者简介:CSDN内容合伙人,全栈领域优质创作者。 👨‍💻景愿:旨在于能和更多的热爱计算机的伙伴一起成长!!&…

上传镜像到阿里云的ACR

1、开通阿里云ACR 2、在ACR 中创建命名空间 3、本地安装docker 4、登录到 开通ACR,需要配置访问凭证 [rootmaster ~]# docker login --username***lb registry.cn-beijing.aliyuncs.com Password: 5、给镜像打标签 [rootmaster ~]# docker images REPOSITORY …

❤ Vue工作常用的一些动态数据和方法处理

❤ Vue工作常用的一些动态数据和方法处理 &#xff08;1&#xff09;动态拼接相对路径结尾的svg 错误写法一 ❌ 正确写法 &#x1f646; <img :src"require(/assets//amazon/svg/homemenu${index}.svg)" style"height: 20px;display: block;margin: 0 au…

操作系统-笔记-第三章-内存管理

目录 三、第三章——内存管理 1、内存的基础知识 &#xff08;1.1&#xff09;程序装入&#xff08;三种&#xff09;——绝对装入 &#xff08;1.2&#xff09;程序装入&#xff08;三种&#xff09;——可重定位装入 &#xff08;1.3&#xff09;程序装入&#xff08;三…

[b01lers2020]Life on Mars1

打开靶场 直接bp抓包 多次点击左侧超链接 发现/query?search这个参数一直在发生改变 可以发现它返回了json格式的数据&#xff0c;猜测是sql注入 放进hackbar进行操作 orderby进行判断 http://6f5976a0-0364-4c05-a7f5-6f0c863e7e41.node4.buuoj.cn:81/query?searchamazonis…

铜矿人员定位安全方案

针对铜矿中的人员定位安全需求&#xff0c;可以采用以下方案&#xff1a; 1.实时人员定位系统&#xff1a;建立一个实时人员定位系统&#xff0c;通过在矿工的工作服或安全帽上安装UWB或RFID定位设备&#xff0c;以及相应的接收器和基站&#xff0c;实时跟踪和定位矿工的位置。…

苍穹外卖 day2 反向代理和负载均衡

一 前端发送的请求&#xff0c;是如何请求到后端服务 前端请求地址&#xff1a;http://localhost/api/employee/login 路径并不匹配 后端接口地址&#xff1a;http://localhost:8080/admin/employee/login 二 查找前端接口 在这个页面上点击f12 后转到networ验证&#xff0…

高并发内存池(PageCache)[3]

原理 PageCache 共128页 static const size_t NPAGES 128;centralcache向pagecache申请2page时&#xff0c;首先向下扫描&#xff0c;有大的会切分出来&#xff0c;然后再挂在对应桶当中 centralcache从pagecache获取span 计算一次获取几页 static const size_t PAGE_SH…

Servlet+Jsp+JDBC实现房屋租赁管理系统(源码+数据库+论文+系统详细配置指导+ppt)

一、项目简介 本项目是一套基于ServletJsp房屋租赁管理系统&#xff0c;主要针对计算机相关专业的正在做毕设的学生与需要项目实战练习的Java学习者。 包含&#xff1a;项目源码、数据库脚本等&#xff0c;该项目附带全部源码可作为毕设使用。 项目都经过严格调试&#xff0c;…

如何最简单、通俗地理解什么是机器学习?

那就究竟什么是学习呢?诺贝尔经济学奖和图灵奖双料得主、卡耐基梅隆大学的赫伯特 西蒙 (Herbert Simon) 教授是这样定义的&#xff1a;“学习是系统通过经验提升性能的过程”。可以看到&#xff0c;学习是一个过程&#xff0c;并且这里有3个关键词&#xff0c;即经验、提升和…

【万能模型训练方法】你没看错,就这么简单

1. 只支持DF结构(Liaef结构特殊&#xff0c;不支持true face机制) 2. dst丢各种人脸数据进去&#xff0c;越杂越好&#xff0c;src保持单人数据 3. 训练时把true face参数打开&#xff0c;推荐0.01 你可以在别的DF预训练模型上&#xff0c;按上述方法练。然后就挂机练&#xff…