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:

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

python 复制代码
# 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 RecursiveCharacterTextSplitter


def process_message():
    
    # Create a ConfigParser object and read the config.ini file
    config = configparser.ConfigParser()
    config.read('config.ini')
    # Retrieve the value of OPENAI_API_KEY
    openai_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 data
    print(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 documents
    text_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 here

    docsearch = Pinecone.from_texts([t.page_content for t in texts], embeddings, index_name=index_name)
    # Query those docs to get your answer back
    llm = 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 文件。

相关推荐
白手小弟16 分钟前
docker部署Stirling-PDF
docker·容器·pdf
数云界22 分钟前
使用 VSCode 在 Python 中创建项目环境
ide·vscode·python
m0_6085709825 分钟前
vscode对python进行多卡调试
ide·vscode·python
咔咔学姐kk33 分钟前
2024最新版,人大赵鑫老师《大语言模型》新书pdf分享
数据库·人工智能·语言模型·自然语言处理·pdf·知识图谱·产品经理
zhang-ge37 分钟前
python提取pdf表格到excel:拆分、提取、合并
python·pdf·excel
计算机学姐38 分钟前
基于python+django+vue的在线学习资源推送系统
开发语言·vue.js·python·学习·django·pip·web3.py
吃什么芹菜卷1 小时前
2024.9最新:CUDA安装,pytorch库安装
人工智能·pytorch·笔记·python·深度学习
鬼圣1 小时前
【软件测试】--xswitch将请求代理到测试桩
软件测试·python·flask·测试桩·xswitch插件
王大傻09281 小时前
python selenium 显示等待 + EC
开发语言·python·selenium·自动化
闲人编程1 小时前
Python实现 Socket.IO 的在线游戏场景
开发语言·python·游戏·网络编程·io·socket·异步