如何使用 GPT API 从 PDF 出版物导出研究图表?

原文地址:how-to-use-gpt-api-to-export-a-research-graph-from-pdf-publications

揭示内部结构——提取研究实体和关系

2024 年 2 月 6 日

介绍

研究图是研究对象的结构化表示,它捕获有关实体的信息以及研究人员、组织、出版物、资助和研究数据之间的关系。目前,出版物以 PDF 文件形式提供,由于文本格式自由,因此很难解析 PDF 文件以提取结构化信息。在本文中,我们将尝试从出版物的 PDF 中创建研究图,方法是从文本中提取相关信息,并使用 OpenAI 将其组织成图结构。

4

OpenAI

在这项工作中,我们使用 OpenAI API 和 GPT 的新助手功能(目前处于测试阶段),将 PDF 文档转换为一组基于研究图模式的结构化 JSON 文件。

助手 API

Assistants API 允许你在应用程序中构建人工智能(AI)助理。助手可以根据预定准则使用模型、工具和信息回答用户的问题。这是一个测试版 API,目前正在积极开发中。使用助手 API,我们可以使用 OpenAI 托管的工具,如代码解释器和知识检索。在本文中,我们将重点介绍知识检索。

知识检索

有时,我们需要人工智能模型回答基于未知知识的查询,如用户提供的文档或敏感信息。我们可以使用助手 API 知识检索工具,用这些信息来增强模型。我们可以将文件上传到助手,它就会自动对文档进行分块,并创建和存储嵌入,从而在数据上实现矢量搜索。

举例说明

在我们的示例中,我们将向 OpenAI 助手和知识检索工具上传 PDF 格式的出版物文件,以获得给定出版物图模式的 JSON 输出。本示例中使用的出版物可通过以下链接访问

5

步骤 1

读取存储出版物 PDF 的输入路径和存储 JSON 输出的输出路径。

import configparser
config = configparser.ConfigParser()
config.read('{}/config.ini'.format(current_path))
input_path = config['DEFAULT']['Input-Path']
output_path = config['DEFAULT']['Output-Path']
debug = config['DEFAULT']['Debug']

步骤 2

从输入路径中获取所有 PDF 文件。

onlyfiles = [f for f in os.listdir(input_path) if os.path.isfile(os.path.join(input_path, f))]

步骤 3

然后,我们需要初始化助手以使用知识检索工具。为此,我们需要在 API 中指定 “检索 ”工具的类型。我们还需要指定助手的指令和要使用的 OpenAI 模型。

my_file_ids = []
if client.files.list().data==[]:for f in onlyfiles:file = client.files.create(file=open(input_path + f, "rb"),purpose='assistants')my_file_ids.append(file.id)
# Add the file to the assistant
assistant = client.beta.assistants.create(instructions = "You are a publication database support chatbot. Use pdf files uploaded to best respond to user queries in JSON.",model = "gpt-4-1106-preview",tools = [{"type": "retrieval"}],# Do not attach all files to the assistant, otherwise, it will mismatch the answers even though specify file ID in query messages.# We will attach to each message instead
)

步骤 4

然后,我们指定需要从出版物文件中提取的信息,并将其作为用户查询传递给助手。在对助手的指令进行试验后,我们发现在每条用户信息中要求使用 JSON 格式能产生最一致的输出。

user_msgs = ["Print the title of this paper in JSON","Print the authors of this paper in JSON","Print the abstract section of the paper in JSON","Print the keywords of the paper in JSON","Print the DOI number of the paper in JSON","Print the author affiliations of the paper in JSON","Print the reference section of the paper in JSON"]

步骤 5

下一步是将查询传递给助手以生成输出。我们需要为每个用户查询创建一个单独的线程对象,其中包含作为用户消息的查询。然后,我们运行线程并获取助手的答案。

all_results = []
for i in my_file_ids:print('\n#####')# the JSON result it can extract and parse, hopefullyfile_result = {}for q in user_msgs:# create thread, user message and run objects for each querythread = client.beta.threads.create()msg = client.beta.threads.messages.create(thread_id=thread.id,role="user",content=q,file_ids=[i] # specify the file/publication we want to extract from)print('\n',q)run = client.beta.threads.runs.create(thread_id=thread.id,assistant_id=assistant.id,additional_instructions="If answer cannot be found, print 'False'" # not very useful at the time of this presentation)# checking run status by retrieving updated object each timewhile run.status in ["queued",'in_progress']:print(run.status) time.sleep(5)run = client.beta.threads.runs.retrieve(thread_id=thread.id,run_id=run.id)# usually a rate limit errorif run.status=='failed':  logging.info("Run failed: ", run)if run.status=='completed':print("<Complete>")# extract updated message object, this includes user messagesmessages = client.beta.threads.messages.list(thread_id=thread.id)for m in messages:if m.role=='assistant':value = m.content[0].text.value # get the text responseif "json" not in value:if value=='False':logging.info("No answer found for ", str(q))else:logging.info("Not JSON output, maybe no answer found in the file or model is outdated: ", str(value))else:# clean the response and try to parse as jsonvalue = value.split("```")[1].split('json')[-1].strip()try: d = json.loads(value)file_result.update(d)print(d)except Exception as e:logging.info(f"Query {q} \nFailed to parse string to JSON: ", str(e))print(f"Query {q} \nFailed to parse string to JSON: ", str(e))all_results.append(file_result)

上述出版文件生成的 JSON 输出为:

[{'title': 'Dodes (diagnostic nodes) for Guideline Manipulation','authors': [{'name': 'PM Putora','affiliation': 'Department of Radiation-Oncology, Kantonsspital St. Gallen, St. Gallen, Switzerland'},{'name': 'M Blattner','affiliation': 'Laboratory for Web Science, Zürich, Switzerland'},{'name': 'A Papachristofilou','affiliation': 'Department of Radiation Oncology, University Hospital Basel, Basel, Switzerland'},{'name': 'F Mariotti','affiliation': 'Laboratory for Web Science, Zürich, Switzerland'},{'name': 'B Paoli','affiliation': 'Laboratory for Web Science, Zürich, Switzerland'},{'name': 'L Plasswilma','affiliation': 'Department of Radiation-Oncology, Kantonsspital St. Gallen, St. Gallen, Switzerland'}],'Abstract': {'Background': 'Treatment recommendations (guidelines) are commonly represented in text form. Based on parameters (questions) recommendations are defined (answers).','Objectives': 'To improve handling, alternative forms of representation are required.','Methods': 'The concept of Dodes (diagnostic nodes) has been developed. Dodes contain answers and questions. Dodes are based on linked nodes and additionally contain descriptive information and recommendations. Dodes are organized hierarchically into Dode trees. Dode categories must be defined to prevent redundancy.','Results': 'A centralized and neutral Dode database can provide standardization which is a requirement for the comparison of recommendations. Centralized administration of Dode categories can provide information about diagnostic criteria (Dode categories) underutilized in existing recommendations (Dode trees).','Conclusions': 'Representing clinical recommendations in Dode trees improves their manageability handling and updateability.'},'Keywords': ['dodes','ontology','semantic web','guidelines','recommendations','linked nodes'],'DOI': '10.5166/jroi-2-1-6','references': [{'ref_number': '[1]','authors': 'Mohler J Bahnson RR Boston B et al.','title': 'NCCN clinical practice guidelines in oncology: prostate cancer.','source': 'J Natl Compr Canc Netw.','year': '2010 Feb','volume_issue_pages': '8(2):162-200'},{'ref_number': '[2]','authors': 'Heidenreich A Aus G Bolla M et al.','title': 'EAU guidelines on prostate cancer.','source': 'Eur Urol.','year': '2008 Jan','volume_issue_pages': '53(1):68-80','notes': 'Epub 2007 Sep 19. Review.'},{'ref_number': '[3]','authors': 'Fairchild A Barnes E Ghosh S et al.','title': 'International patterns of practice in palliative radiotherapy for painful bone metastases: evidence-based practice?','source': 'Int J Radiat Oncol Biol Phys.','year': '2009 Dec 1','volume_issue_pages': '75(5):1501-10','notes': 'Epub 2009 May 21.'},{'ref_number': '[4]','authors': 'Lawrentschuk N Daljeet N Ma C et al.','title': "Prostate-specific antigen test result interpretation when combined with risk factors for recommendation of biopsy: a survey of urologist's practice patterns.",'source': 'Int Urol Nephrol.','year': '2010 Jun 12','notes': 'Epub ahead of print'},{'ref_number': '[5]','authors': 'Parmelli E Papini D Moja L et al.','title': 'Updating clinical recommendations for breast colorectal and lung cancer treatments: an opportunity to improve methodology and clinical relevance.','source': 'Ann Oncol.','year': '2010 Jul 19','notes': 'Epub ahead of print'},{'ref_number': '[6]','authors': 'Ahn HS Lee HJ Hahn S et al.','title': 'Evaluation of the Seventh American Joint Committee on Cancer/International Union Against Cancer Classification of gastric adenocarcinoma in comparison with the sixth classification.','source': 'Cancer.','year': '2010 Aug 24','notes': 'Epub ahead of print'},{'ref_number': '[7]','authors': 'Rami-Porta R Goldstraw P.','title': 'Strength and weakness of the new TNM classification for lung cancer.','source': 'Eur Respir J.','year': '2010 Aug','volume_issue_pages': '36(2):237-9'},{'ref_number': '[8]','authors': 'Sinn HP Helmchen B Wittekind CH.','title': 'TNM classification of breast cancer: Changes and comments on the 7th edition.','source': 'Pathologe.','year': '2010 Aug 15','notes': 'Epub ahead of print'},{'ref_number': '[9]','authors': 'Paleri V Mehanna H Wight RG.','title': "TNM classification of malignant tumours 7th edition: what's new for head and neck?",'source': 'Clin Otolaryngol.','year': '2010 Aug','volume_issue_pages': '35(4):270-2'},{'ref_number': '[10]','authors': 'Guarino N.','title': 'Formal Ontology and Information Systems','source': '1998 IOS Press'},{'ref_number': '[11]','authors': 'Uschold M Gruniger M.','title': 'Ontologies: Principles Methods and Applications.','source': 'Knowledge Engineering Review','year': '1996','volume_issue_pages': '11(2)'},{'ref_number': '[12]','authors': 'Aho A Garey M Ullman J.','title': 'The Transitive Reduction of a Directed Graph.','source': 'SIAM Journal on Computing','year': '1972','volume_issue_pages': '1(2): 131–137'},{'ref_number': '[13]','authors': 'Tai K','title': 'The tree-to-tree correction problem.','source': 'Journal of the Association for Computing Machinery (JACM)','year': '1979','volume_issue_pages': '26(3):422-433'}]}]

步骤 6

文件对象和助手对象需要清理,因为它们在 “检索 ”模式下需要花钱。此外,这也是一种良好的编码实践。

for f in client.files.list().data:client.files.delete(f.id)
# Retrieve and delete running assistants
my_assistants = client.beta.assistants.list(order="desc",
)
for a in my_assistants.data:    response = client.beta.assistants.delete(a.id)print(response)

步骤 7

下一步是使用 Python Networkx 软件包生成图形可视化。

import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()
node_colors = []
key = "jroi/" + all_results[0]['title']
G.add_nodes_from([(all_results[0]['title'], {'doi': all_results[0]['DOI'], 'title': all_results[0]['title'], 'source': 'jroi', 'key': key})])
node_colors.append('#4ba9dc')
for author in all_results[0]['authors']:key = "jroi/" + author['name']G.add_nodes_from([(author['name'], {'key': key, 'local_id': author['name'], 'full_name': author['name'], 'source': 'jroi'})])G.add_edge(all_results[0]['title'], author['name'])node_colors.append('#63cc9e')
for reference in all_results[0]['references']:key = "jroi/" + reference['title']G.add_nodes_from([(reference['title'].split('.')[0][:25] + '...', {'title': reference['title'], 'source': 'jroi', 'key': key})])G.add_edge(all_results[0]['title'], reference['title'].split('.')[0][:25] + '...')node_colors.append('#4ba9dc')
pos = nx.spring_layout(G)
labels = nx.get_edge_attributes(G, 'label')
nx.draw(G, pos, with_labels=True, node_size=1000, node_color=node_colors, font_size=7, font_color='black')
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
plt.savefig("graph_image.png")
plt.show()

可视化图表如下:

6

注:请注意,OpenAI 生成的输出结构可能因执行方式不同而不同。因此,你可能需要根据该结构更新上述代码。

结论

总之,利用 GPT API 从 PDF 出版物中提取研究图为研究人员和数据分析师提供了一个强大而高效的解决方案。该工作流程简化了将 PDF 出版物转换为结构化、可访问的研究图表的过程。但是,我们也必须仔细关注大型语言模型 (LLM) 生成的回复的不一致性。随着时间的推移,通过定期更新和改进提取模型,可以进一步提高准确性和相关性。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1411142.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

从零开始搭建Springboot项目脚手架1:新建项目

1、技术栈 SpringBoot 3.2.5&#xff1a; 2、 新建项目 使用SpringInitializr 选择Lombok、Configuration Processor、Spring Web&#xff0c;同时IDEA也要安装Lombok插件 删除多余的Maven目录、Maven文件&#xff0c;把HELP.md改成README.md。 当然前提是已经安装好Maven和配…

Linux——socket编程之tcp通信

前言 前面我们学习socket的udp通信&#xff0c;了解到了socket的概念与udp的实现方法&#xff0c;今天我们来学习一下面向连接的tcp通信。 一、tcp套接字创建 UDP和TCP都是通过套接字&#xff08;socket&#xff09;来实现通信的&#xff0c;因此TCP也得使用socket()接口创建…

机器学习批量服务模式优化指南

原文地址&#xff1a;optimizing-machine-learning-a-practitioners-guide-to-effective-batch-serving-patterns 2024 年 4 月 15 日 简介 在机器学习和数据分析中&#xff0c;模型服务模式的战略实施对于在生产环境中部署和操作人工智能模型起着至关重要的作用。其中&…

Elasticsearch:对 Java 对象的 ES|QL 查询

作者&#xff1a;Laura Trotta ES|QL 是 Elasticsearch 引入的一种新的查询语言&#xff0c;它将简化的语法与管道操作符结合起来&#xff0c;使用户能够直观地推断和操作数据。官方 Java 客户端的新版本 8.13.0 引入了对 ES|QL 查询的支持&#xff0c;提供了一个新的 API&…

【前端学习——css】css实现给背景的图片加模糊

我在制作自己的博客的时候&#xff0c;打算做个封面&#xff0c;封面用半透明颜色盖住了预览图&#xff0c;上面印上了文字&#xff0c;但背景图太乱了&#xff0c;所以打算给背景加模糊效果。 效果 方法 主要就是利用这个属性 backdrop-filter: blur(5px);属性很简单&#x…

Flutter笔记:Widgets Easier组件库(9)使用弹窗

Flutter笔记 Widgets Easier组件库&#xff08;9&#xff09;&#xff1a;使用弹窗 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress o…

排序算法之希尔排序(缩小增量排序)

希尔排序是插入排序的优化&#xff0c;如果不了解插入排序可以看排序算法之插入排序-CSDN博客这篇博客&#xff0c;希尔排序算法通过对原始数据集使用 gap 分组的方法&#xff0c;先将数据分组进行插入排序&#xff0c;随着排序的进行&#xff0c;逐渐减小 gap 的值&#xff0c…

一个肉夹馍思考的零耦合设计

刷抖音听说知识付费是普通人的一个收入增长点&#xff0c;写了三十几篇文章一毛钱没赚&#xff0c;感觉有点沮丧。天上下着小雨雨&#xff0c;稀稀嗦嗦的&#xff0c;由于了很久还是买了一个&#x1f928;。 忽然觉得生活有点悲催&#xff0c;现在已经变得斤斤计较&#xff0c;…

TCP重传机制——快速重传

TCP 有一种快速重传机制&#xff0c;它不以时间为驱动&#xff0c;而是以数据驱动重传。 在上图&#xff0c;发送方发出了 1&#xff0c;2&#xff0c;3&#xff0c;4&#xff0c;5 份数据&#xff1a; 第一份 Seq1 先送到了&#xff0c;于是就 Ack 回 2&#xff1b;结果 Seq2…

自定义数据上的YOLOv9分割训练

原文地址&#xff1a;yolov9-segmentation-training-on-custom-data 2024 年 4 月 16 日 在飞速发展的计算机视觉领域&#xff0c;物体分割在从图像中提取有意义的信息方面起着举足轻重的作用。在众多分割算法中&#xff0c;YOLOv9 是一种稳健且适应性强的解决方案&#xff0…

vue2项目webpack3.x打包文件分割优化加载

vue2项目webpack3.x打包文件分割优化加载 0. 项目目录和依赖信息1. 开启 gzip&#xff08;建议&#xff09;2. vue2项目配置懒加载&#xff08;建议&#xff09;3. 拆分 vendor 包注意&#xff1a;webpack3使用CommonsChunkPlugin实现 本文使用 3 种方案进行叠加优化 优先级按以…

JavaWeb--1.Servlet

Servlet&#xff08;基础&#xff09; 1、配置依赖&#xff1a; ​ 在pom.xml文件中加入相关依赖 <dependencies><dependency><groupId>jakarta.servlet</groupId><artifactId>jakarta.servlet-api</artifactId><version>5.0.0&l…

021、Python+fastapi,第一个Python项目走向第21步:ubuntu 24.04 docker 安装mysql8、redis(二)

系列文章目录 pythonvue3fastapiai 学习_浪淘沙jkp的博客-CSDN博客https://blog.csdn.net/jiangkp/category_12623996.html 前言 安装redis 我会以三种方式安装&#xff0c; 第一、直接最简单安装&#xff0c;适用于测试环境玩玩 第二、conf配置安装 第三、集群环境安装 一…

【简单介绍下Lisp的学习历程】

&#x1f3a5;博主&#xff1a;程序员不想YY啊 &#x1f4ab;CSDN优质创作者&#xff0c;CSDN实力新星&#xff0c;CSDN博客专家 &#x1f917;点赞&#x1f388;收藏⭐再看&#x1f4ab;养成习惯 ✨希望本文对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出…

k8s笔记 | Ingress

安装Ingress 添加helm创库 Installation Guide - Ingress-Nginx Controller Ingress | Kubernetes 下载包 将 文件helm 放到 /usr/local/bin/ 并给到执行权限 # 添加可执行权限 chmod ux helm # 测试是否能运行 helm version# 结果 version.BuildInfo{Version:"v3.14…

Eclipse 开创性地集成 Neon Stack,将 EVM 兼容性带到 SVM 网络

2024年5月2日&#xff0c;全球——在塑造区块链网络的战略联盟的过程中&#xff0c;Eclipse 通过集成 Neon EVM 核心团队开发的技术堆栈 Neon Stack&#xff0c;成为首个打破 EVM-SVM 兼容性障碍的生态。 Eclipse 旨在通过结合以太坊和 Solana 的最佳特性&#xff0c;来重构区…

【CTF Reverse】XCTF GFSJ0487 game Writeup(反编译+逆向工程)

game 菜鸡最近迷上了玩游戏&#xff0c;但它总是赢不了&#xff0c;你可以帮他获胜吗 解法 放进 exeinfope 中分析。 拖入 IDA 中。shift f12 查看字符串。ctrl f 搜索 flag。 DATA XREF: sub_45E94028↑o Function 中搜索 sub_45E940。 flag 应该是在这里算出来的&#xff…

C#语言入门

一、基础知识 1. 程序语言是什么 用于人和计算机进行交流&#xff0c;通过程序语言让计算机能够响应我们发出的指令 2. 开发环境 IDE&#xff0c;集成开发环境。它就是一类用于程序开发的软件&#xff0c;这一类软件一般包括了代码编辑、编译器、调试器、图形用户界面等等工…

MTEB - Embedding 模型排行榜

文章目录 关于 MTEBMTEB 任务和数据集概览使用 MTEB Pythont 库Installation使用 关于 MTEB MTEB : Massive Text Embedding Benchmark github : https://github.com/embeddings-benchmark/mtebhuggingface : https://huggingface.co/spaces/mteb/leaderboardpaper : https:/…

微信小程序的执行流程

1.首先会进入app.js 打开微信小程序首先会进入app.js,在app.js中会写入App()方法&#xff0c;App()接受一个object参数&#xff0c;用于指定小程序的生命周期等。App()方法有且仅有一个。 App()的object参数及其描述&#xff1a; onLaunch&#xff1a;监听小程序显示&#x…