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

这篇具有很好参考价值的文章主要介绍了基于GPT-4和LangChain构建云端定制化PDF知识库AI聊天机器人。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

参考:

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请求重置计数器。就可以继续免费使用了。

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

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文章来源地址https://www.toymoban.com/news/detail-669632.html

  • 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.

到了这里,关于基于GPT-4和LangChain构建云端定制化PDF知识库AI聊天机器人的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LLM本地知识库问答系统(一):使用LangChain和LlamaIndex从零构建PDF聊天机器人指南

           随着大型语言模型(LLM)(如ChatGPT和GPT-4)的兴起,现在比以往任何时候都更容易构建比普通熊更智能的智能聊天机器人,并且可以浏览堆积如山的文档,为您的输入提供准确的响应。        在本系列中,我们将探索如何使用pre-trained的LLM创建一个聊天机器人,该聊

    2024年02月11日
    浏览(47)
  • 基于Langchain+向量数据库+ChatGPT构建企业级知识库

    ▼最近直播超级多, 预约 保你有收获 近期直播: 《 基于 LLM 大模型的向量数据库企业级应用实践 》  1 — LangChain 是什么? 众所周知 OpenAI 的 API 无法联网的,所以如果只使用自己的功能实现联网搜索并给出回答、总结 PDF 文档、基于某个 Youtube 视频进行问答等等的功能肯定

    2024年02月06日
    浏览(49)
  • Quivr 基于GPT和开源LLMs构建本地知识库 (更新篇)

    自从大模型被炒的越来越火之后,似乎国内涌现出很多希望基于大模型构建本地知识库的需求,大概在5月底的时候,当时Quivr发布了第一个0.0.1版本,第一个版本仅仅只是使用LangChain技术结合OpenAI的GPT模型实现了一个最基本的架子,功能并不够完善,但可以研究研究思路,当

    2024年02月12日
    浏览(38)
  • GPT实战系列-简单聊聊LangChain搭建本地知识库准备

    LangChain 是一个开发由语言模型驱动的应用程序的框架,除了和应用程序通过 API 调用, 还会: 数据感知 : 将语言模型连接到其他数据源 具有代理性质 : 允许语言模型与其环境交互 LLM大模型相关文章: GPT实战系列-简单聊聊LangChain GPT实战系列-ChatGLM3本地部署CUDA11+1080Ti+显卡

    2024年02月01日
    浏览(42)
  • LangChain入门(四)-构建本地知识库问答机器人

    在这个例子中,我们会介绍如何从我们本地读取多个文档构建知识库,并且使用 Openai API 在知识库中进行搜索并给出答案。 目录 一、安装向量数据库chromadb和tiktoken 二、使用案例 三、embeddings持久化 四、在线的向量数据库Pinecone 一、安装向量数据库chromadb和tiktoken    其中h

    2024年02月05日
    浏览(36)
  • GPT-Crawler一键爬虫构建GPTs知识库

    GPT-Crawler一键爬虫构建GPTs知识库 能够爬取网站数据,构建GPTs的知识库,项目依赖node.js环境,接下来我们按步骤来安装,非常简单 参考:https://zhuanlan.zhihu.com/p/668700619 在信息爆炸的时代,数据成为了新的石油。但是,如何有效地从这无穷无尽的网络信息中提取有价值的知识,

    2024年02月04日
    浏览(36)
  • 基于 InternLM 和 LangChain 搭建你的知识库

    如何打造垂域大模型是一个重要落地方向。 如何打造个人专属的大模型应用也是重要的问题。 RAG 外挂一个知识库 优势:成本低,实时更新 劣势:能力受基座模型影响大,RAG每次需要将检索文档和问题提交给大模型,极大占用上下文限制。 Finetune 轻量级的微调 优势:可以充

    2024年01月19日
    浏览(44)
  • AnythingLLM:基于RAG方案构专属私有知识库(开源|高效|可定制)

    继OpenAI和Google的产品发布会之后,大模型的能力进化速度之快令人惊叹,然而,对于很多个人和企业而言,为了数据安全不得不考虑私有化部署方案,从GPT-4发布以来,国内外的大模型就拉开了很明显的差距,能够实现的此路径无非就只剩下国内的开源大模型可以选择了。而

    2024年02月04日
    浏览(53)
  • 【基于 InternLM 和 LangChain 搭建你的知识库】学习笔记

    学习参考文档【基于 InternLM 和 LangChain 搭建你的知识库】 学习参考链接【书生・浦语大模型实战营第三课作业(基础+进阶)】 收集2018年-2020年几年间的优秀数学建模论文 LangChain 相关环境配置 下载 NLTK 相关资源 下载相关仓库 脚本文件 Web Demo部署

    2024年02月01日
    浏览(36)
  • Chinese-LangChain:基于ChatGLM-6b+langchain实现本地化知识库检索与智能答案生成

    Chinese-LangChain:中文langchain项目,基于ChatGLM-6b+langchain实现本地化知识库检索与智能答案生成 https://github.com/yanqiangmiffy/Chinese-LangChain 俗称:小必应,Q.Talk,强聊,QiangTalk 🐯 2023/04/19 引入ChuanhuChatGPT皮肤 📱 2023/04/19 增加web search功能,需要确保网络畅通! 📚 2023/04/18 webui增加知

    2024年02月06日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包