当前位置: 首页 > news >正文

使用chatgpt api快速分析pdf

需求背景

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

需求分析

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

引入依赖

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

生成向量

# 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()

加载向量库

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访问

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

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

http://www.lryc.cn/news/362857.html

相关文章:

  • Vue:状态管理pinia
  • 【Android Studio】导入import android.support.v7.app.AppcompatActivity;时报错
  • 汽车区域控制器技术分析
  • myEclipse新手使用教程
  • 【WPF编程宝典】第6讲:资源
  • 容器化部署Pig微服务快速开发框架
  • Windows编程:图标资源、光标资源、字符串资源、加速键资源、WM_PAINT消息、绘图
  • 【2024 短剧0元轻资产创业风口】做自己的老板,做新媒体的领路人
  • Docker安装Bitbucket
  • FlyMcu串口下载STLINK Utility
  • CSS(盒子模型,定位,浮动,扩展)
  • AIGC如何改变人类生活20240529
  • 【python】成功解决“TypeError: ‘method’ object is not subscriptable”错误的全面指南
  • 若依 Spring Security 短信,扫码登录
  • Web 网页性能优化
  • JDBC-MySQL
  • MySQL经典练习50题(上)(解析版)
  • 每日一题33:数据统计之广告效果
  • 52、有边数限制的最短路
  • Spring boot实现基于注解的aop面向切面编程
  • MySQL之查询性能优化(四)
  • 定时任务详解
  • OnlyOffice DocumentServer 8.0.1编译破解版本(¥100)
  • Android 应用权限
  • MATLAB 匿名函数
  • Java 新手入门:基础知识点一览
  • 三维模型轻量化工具:手工模型、BIM、倾斜摄影等皆可用!
  • 小程序CI/CD之自动化打包预览并钉钉通知发布进程
  • C++使用QtHttpServer开发服务端Server的Http POST接口和客户端Client示例
  • 计算机基础(8)——音频数字化(模电与数电)