使用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接口还是蛮贵的,所以限制了试用次数,如果有需要的可以联系我开放限制,希望能反馈一些使用意见。

相关推荐
mCell6 小时前
ChatGPT Codex CLI 系统提示词
chatgpt·openai·ai编程
轻竹办公PPT6 小时前
上传PDF直接生成PPT,适合工作汇报和总结场景
python·pdf·powerpoint
非凡ghost7 小时前
FlexiPDF(专业PDF编辑软件)
windows·学习·pdf·软件需求
xinyu_Jina8 小时前
PaperStudio:WYSIWYG文档的Web实现——从CSS Print到客户端PDF生成的技术解析
前端·css·pdf
Maynor99617 小时前
Claude vs ChatGPT vs Gemini:全方位对比与选用指南
chatgpt
2501_907136821 天前
PDF Splitter Pro - PDF页面分割器
pdf·软件需求
中國龍在廣州1 天前
AI顶会ICML允许AI参与审稿
人工智能·深度学习·算法·机器学习·chatgpt
hellocode_1 天前
【2025年】GPT-5.2怎么样?Instant/Thinking/Pro 怎么选?如何订阅使用chatgptplus?GPT-5.2使用实例展示
人工智能·gpt·chatgpt
Maynor9961 天前
Claude vs ChatGPT vs Gemini: 기능 비교, 사용 경험, 적합 인군
人工智能·chatgpt
AI大模型学徒2 天前
大模型应用开发(十六)_知识库2
chatgpt·大模型·知识库·deepseek