使用chatgpt api快速分析pdf

需求背景

搞材料的兄弟经常要分析pdf,然后看到国外有产品是专门调用chatpdf来分析pdf的,所以就来问我能不能帮他也做一个出来。正好我有chatgpt的api,所以就研究了一下这玩意怎么弄。

需求分析

由于chatgpt是按字符算钱的,所以如果把整个pdf文本全部塞进去,虽然效果是好了,但是钱花的巨快。测试的时候不小心传了个86页的pdf进去,好家伙,直接余额变负值了,一下子花了6刀多。。。。所以我们只能先对pdf做预处理,主要使用了langchain包加载和生成向量库

引入依赖

python 复制代码
from langchain.document_loaders import PyPDFLoader
from langchain.indexes.vectorstore import VectorstoreIndexCreator
from langchain.vectorstores.chroma import Chroma
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.indexes.vectorstore import VectorStoreIndexWrapper

生成向量

python 复制代码
# https://chatpdf4cn.com/
def load_pdf_and_save_to_index(file_path, index_name):
    loader = PyPDFLoader(file_path)
    index = VectorstoreIndexCreator(vectorstore_kwargs={"persist_directory":get_index_path(index_name)}).from_loaders({loader})
    print('save')

    index.vectorstore.persist()

加载向量库

python 复制代码
def load_index(index_name):
    index_path=get_index_path(index_name)
    embedding = OpenAIEmbeddings()
    vectordb = Chroma(
    persist_directory=index_path,
    embedding_function=embedding
 )
    return VectorStoreIndexWrapper(vectorstore=vectordb)

最后调用chatgpt访问

python 复制代码
load_pdf_and_save_to_index(file_path, index_name)
index = load_index(index_name)

ans = index.query("文章中提到的两种磁化模式有什么区别?",chain_type_kwargs={}) #RetrievalQAWithSourcesChain

print(ans)

附上完整项目试用地址
chatpdf4cn

调用chatgpt接口还是蛮贵的,所以限制了试用次数,如果有需要的可以联系我开放限制,希望能反馈一些使用意见。

相关推荐
FinelyYang3 分钟前
uniapp使用kkfileview在线预览ppt,excel,word,pdf类型文件
pdf·uni-app·powerpoint
孤狼GPT1 小时前
ChatGPT、Codex数据库实战:MyBatis-Plus第一页查询正常,翻到第10000页为什么越来越慢?
mysql·chatgpt·mybatis-plus·codex·深分页
WORLDPO922614 小时前
QTE-020-01-L/F/C-D-A-K-TR_实物_数据手册PDF_国产替代
pdf
jerrywus21 小时前
普通人怎么用 Codex:从表格、文件和小工具开始
chatgpt·agent·mcp
asdzx671 天前
C# 操作 PDF 元数据:从属性写入到字段提取的完整流程
前端·pdf·c#
WORLDPO92261 天前
SFSDT-10/15/20/25/35-28-G-19.69-SR_实物图_数据手册PDF_国产替代
pdf·pcb工艺·pcb
梦想出海-Phoebe1 天前
Gemini新增文件编辑功能:PDF、Word、Excel处理体验分享
pdf·word·excel
开开心心就好1 天前
网页图片批量下载工具,输入链接就保存
服务器·算法·leetcode·支持向量机·pdf·ocr·1024程序员节
仙逆GPT2 天前
ChatGPT、Codex工程实战:Agent接入企业知识库,文档明明更新了,为什么回答还是旧版本?
chatgpt·codex·rag·企业知识库·ai agent
汤姆宝库2 天前
法考历年真题及答案解析合集pdf版(2002-2025年)
pdf·法考