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

相关推荐
weixin_469273816 小时前
如何在线把文字转为 pdf 文档
pdf
2601_963906786 小时前
离线截图 OCR 识别:支持表格与 PDF 解析
pdf·ocr·软件需求
乃嘿仔8 小时前
AI 热点日报 · 2026-09-04
人工智能·chatgpt
ServBay9 小时前
GPT-6 Astra 发布,当 AI 开始自己操作电脑,AGI 还远吗?
gpt·chatgpt·openai
精彩AI说11 小时前
ChatGPT照着参考文档改内容为什么总跑偏?模板结构、字段对应与格式约束方法
chatgpt·ai工具·办公效率·文档整理·chatgpt教程·模板改写
浩风祭月12 小时前
GPT-6 Astra API怎么接?Responses API迁移、异步工具调用与兼容项清单
人工智能·chatgpt·大模型·openai api·gpt-6 astra
七牛开发者14 小时前
HarnessDev:让 LLM 自己创建并迭代 Agent Harness
chatgpt·llm·agent
asdzx6714 小时前
Python 实现 PDF 文本查找与高亮标注
开发语言·python·pdf
laoban-yu15 小时前
Word与PDF文档互转
pdf·word·文档转换
python零基础入门小白15 小时前
LangGraph智能体实战:如何用Langfuse构建AI运行时全链路可观测系统?
人工智能·学习·ai·chatgpt·程序员·大模型·智能体