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

相关推荐
小殊小殊1 天前
2025 年人工智能的现状:Agent从黑马到泡沫?
人工智能·chatgpt
2501_907136821 天前
word元数据批量修改与PDF转换工具
pdf·软件需求
摘星编程1 天前
解密RAG:如何用检索增强生成打破大模型“幻觉”困局?
chatgpt
halen3331 天前
Transform PDFs into Stunning Images with Free PDF to IMG!
pdf
开开心心_Every1 天前
局域网大文件传输,设密码双向共享易用工具
运维·服务器·网络·游戏·pdf·电脑·excel
汽车软件工程师0012 天前
ChatGpt指导嵌入式软件开发能力——2、TriCore深度专项训练
人工智能·chatgpt·autosar
开开心心就好2 天前
实用PDF批量加马赛克,抹除敏感信息绿色版
java·linux·开发语言·网络·人工智能·pdf·word2vec
github.com/starRTC2 天前
Claude Code中英文系列教程35:通过API使用Skills创建ppt,word文档和pdf
pdf·word·powerpoint·ai编程
AI英德西牛仔3 天前
ChatGPT和Gemini 输出无乱码
chatgpt
开开心心_Every3 天前
剪切板工具存500条,可搜索备份导入导出
linux·运维·服务器·华为od·edge·pdf·华为云