Question mutiple pdf‘s using openai, pinecone, langchain

2024-08-31 15:12

本文主要是介绍Question mutiple pdf‘s using openai, pinecone, langchain,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题意:使用 OpenAI、Pinecone 和 LangChain 对多个 PDF 文件进行提问。

问题背景:

I am trying to ask questions against a multiple pdf using pinecone and openAI but I dont know how to.

我正在尝试使用 Pinecone 和 OpenAI 对多个 PDF 文件进行提问,但我不知道该怎么做。

The code below works for asking questions against one document. but I would like to have multiple documents to ask questions against:

下面的代码可以用于对一个文档进行提问,但我想要能够对多个文档提问:

# process_message.py
from flask import request
import pinecone
# from PyPDF2 import PdfReader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import ElasticVectorSearch, Pinecone, Weaviate, FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
import os
import json
# from constants.company import file_company_id_column, file_location_column, file_name_column
from services.files import FileFireStorage
from middleware.auth import check_authorization
import configparser
from langchain.document_loaders import UnstructuredPDFLoader, OnlinePDFLoader, PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitterdef process_message():# Create a ConfigParser object and read the config.ini fileconfig = configparser.ConfigParser()config.read('config.ini')# Retrieve the value of OPENAI_API_KEYopenai_key = config.get('openai', 'OPENAI_API_KEY')pinecone_env_key = config.get('pinecone', 'PINECONE_ENVIRONMENT')pinecone_api_key = config.get('pinecone', 'PINECONE_API_KEY')loader = PyPDFLoader("docs/ops.pdf")data = loader.load()# data = body['data'][1]['name']# Print information about the loaded dataprint(f"You have {len(data)} document(s) in your data")print(f"There are {len(data[30].page_content)} characters in your document")# Chunk your data up into smaller documentstext_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)texts = text_splitter.split_documents(data)embeddings = OpenAIEmbeddings(openai_api_key=openai_key)pinecone.init(api_key=pinecone_api_key, environment=pinecone_env_key)index_name = "pdf-chatbot"  # Put in the name of your Pinecone index heredocsearch = Pinecone.from_texts([t.page_content for t in texts], embeddings, index_name=index_name)# Query those docs to get your answer backllm = OpenAI(temperature=0, openai_api_key=openai_key)chain = load_qa_chain(llm, chain_type="stuff")query = "Are there any other documents listed in this document?"docs = docsearch.similarity_search(query)answer = chain.run(input_documents=docs, question=query)print(answer)return answer

I added as many comments as I could there. I got this information from 

我在代码中添加了尽可能多的注释。我从以下来源获取了这些信息:https://www.youtube.com/watch?v=h0DHDp1FbmQ

I tried to look at other stackoverflow questions about this but could not find anything similar

我试图查看其他与此相关的 Stack Overflow 问题,但没有找到类似的内容。

问题解决:

You can load multiple PDFS with PyPDFDirectoryLoader

你可以使用 `PyPDFDirectoryLoader` 加载多个 PDF 文件。

这篇关于Question mutiple pdf‘s using openai, pinecone, langchain的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1124254

相关文章

Python结合Free Spire.PDF for Python实现PDF页面旋转

《Python结合FreeSpire.PDFforPython实现PDF页面旋转》在日常办公或文档处理中,我们经常会遇到PDF页面方向错误的问题,本文将分享如何用Python结合FreeSpir... 目录基础实现:单页PDF精准旋转完整代码代码解析进阶操作:覆盖多场景旋转需求1. 旋转指定角度(90/27

使用C#实现将RTF转换为PDF

《使用C#实现将RTF转换为PDF》RTF(RichTextFormat)是一种通用的文档格式,允许用户在不同的文字处理软件中保存和交换格式化文本,下面我们就来看看如何使用C#实现将RTF转换为PDF... 目录Spire.Doc for .NET 简介安装 Spire.Doc代码示例处理异常总结RTF(R

SpringBoot集成iText快速生成PDF教程

《SpringBoot集成iText快速生成PDF教程》本文介绍了如何在SpringBoot项目中集成iText9.4.0生成PDF文档,包括新特性的介绍、环境准备、Service层实现、Contro... 目录SpringBoot集成iText 9.4.0生成PDF一、iText 9新特性与架构变革二、环

使用Python在PDF中绘制多种图形的操作示例

《使用Python在PDF中绘制多种图形的操作示例》在进行PDF自动化处理时,人们往往首先想到的是文本生成、图片嵌入或表格绘制等常规需求,然而在许多实际业务场景中,能够在PDF中灵活绘制图形同样至关重... 目录1. 环境准备2. 创建 PDF 文档与页面3. 在 PDF 中绘制不同类型的图形python

使用Python实现在PDF中添加、导入、复制、移动与删除页面

《使用Python实现在PDF中添加、导入、复制、移动与删除页面》在日常办公和自动化任务中,我们经常需要对PDF文件进行页面级的编辑,使用Python,你可以轻松实现这些操作,而无需依赖AdobeAc... 目录1. 向 PDF 添加空白页2. 从另一个 PDF 导入页面3. 删除 PDF 中的页面4. 在

OFD格式文件及如何适应Python将PDF转换为OFD格式文件

《OFD格式文件及如何适应Python将PDF转换为OFD格式文件》OFD是中国自主研发的一种固定版式文档格式,主要用于电子公文、档案管理等领域,:本文主要介绍OFD格式文件及如何适应Python... 目录前言什么是OFD格式文档?使用python easyofd库将PDF转换为OFD第一步:安装 eas

基于Java实现PPT到PDF的高效转换详解

《基于Java实现PPT到PDF的高效转换详解》在日常开发中,经常会遇到将PPT文档批量或单文件转换为PDF的需求,本文将详细介绍其使用流程、核心代码与常见问题解决方案,希望对大家有所帮助... 目录一、环境配置Maven 配置Gradle 配置二、核心实现:3步完成PPT转PDF1. 单文件转换(基础版)

利用Python将PDF文件转换为PNG图片的代码示例

《利用Python将PDF文件转换为PNG图片的代码示例》在日常工作和开发中,我们经常需要处理各种文档格式,PDF作为一种通用且跨平台的文档格式,被广泛应用于合同、报告、电子书等场景,然而,有时我们需... 目录引言为什么选择 python 进行 PDF 转 PNG?Spire.PDF for Python

Java高效实现PowerPoint转PDF的示例详解

《Java高效实现PowerPoint转PDF的示例详解》在日常开发或办公场景中,经常需要将PowerPoint演示文稿(PPT/PPTX)转换为PDF,本文将介绍从基础转换到高级设置的多种用法,大家... 目录为什么要将 PowerPoint 转换为 PDF安装 Spire.Presentation fo

5 种使用Python自动化处理PDF的实用方法介绍

《5种使用Python自动化处理PDF的实用方法介绍》自动化处理PDF文件已成为减少重复工作、提升工作效率的重要手段,本文将介绍五种实用方法,从内置工具到专业库,帮助你在Python中实现PDF任务... 目录使用内置库(os、subprocess)调用外部工具使用 PyPDF2 进行基本 PDF 操作使用