51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2300|回复: 1
打印 上一主题 下一主题

[资料] Python代码调用ChatGPT分享

[复制链接]
  • TA的每日心情
    擦汗
    昨天 09:00
  • 签到天数: 1025 天

    连续签到: 4 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2023-3-29 11:26:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    阅读论文可以说是我们的日常工作之一,论文的数量太多,我们如何快速阅读归纳呢?自从ChatGPT出现以后,有很多阅读论文的服务可以使用。其实使用ChatGPT API非常简单,我们只用30行python代码就可以在本地搭建一个自己的应用。
      使用 Python 和 ChatGPT API 总结论文的步骤很简单:
      ·用于 PDF 处理的 PyPDF2 和用于与 GPT-3.5-turbo 接口的 OpenAI。
      · 使用 PyPDF2 打开并阅读 PDF 文件。
      · 遍历 PDF 文档中的每一页,提取文本。
      · 使用 GPT-3.5-turbo 为每个页面的文本生成摘要。
      · 合并摘要并将最终摘要文本保存到文件中。
      import PyPDF2
       import openai
       pdf_summary_text = ""


      解析pdf
      pdf_file_path = "./pdfs/paper.pdf"
       pdf_file = open(pdf_file_path, 'rb')
       pdf_reader = PyPDF2.PdfReader(pdf_file)


      获取每一页的文本:
      for page_num in range(len(pdf_reader.pages)):
          page_text = pdf_reader.pages[page_num].extract_text().lower()


      使用openai的api进行汇总:
      response = openai.ChatCompletion.create(
          model="gpt-3.5-turbo",
          messages=[
              {"role": "system", "content": "You are a helpful research assistant."},
              {"role": "user", "content": f"Summarize this: {page_text}"},
          ],
       )
       page_summary = response["choices"][0]["message"]["content"]


      合并摘要:
      pdf_summary_text += page_summary + "\n"
       pdf_summary_file = pdf_file_path.replace(os.path.splitext(pdf_file_path)[1], "_summary.txt")
       with open(pdf_summary_file, "w+") as file:
          file.write(pdf_summary_text)


      搞定,关闭pdf文件,回收内存:
      pdf_file.close()

      完整代码如下:
      import os
       import PyPDF2
       import re
       import openai

       # Here I assume you are on a Jupiter Notebook and download the paper directly from the URL
       !curl -o paper.pdf https://arxiv.org/pdf/2301.00810v3.pdf?utm_source=pocket_saves

       # Set the string that will contain the summary   
       pdf_summary_text = ""
       # Open the PDF file
       pdf_file_path = "paper.pdf"
       # Read the PDF file using PyPDF2
       pdf_file = open(pdf_file_path, 'rb')
       pdf_reader = PyPDF2.PdfReader(pdf_file)
       # Loop through all the pages in the PDF file
       for page_num in range(len(pdf_reader.pages)):
          # Extract the text from the page
          page_text = pdf_reader.pages[page_num].extract_text().lower()

          response = openai.ChatCompletion.create(
                          model="gpt-3.5-turbo",
                          messages=[
                              {"role": "system", "content": "You are a helpful research assistant."},
                              {"role": "user", "content": f"Summarize this: {page_text}"},
                                  ],
                                      )
          page_summary = response["choices"][0]["message"]["content"]
          pdf_summary_text+=page_summary + "\n"
          pdf_summary_file = pdf_file_path.replace(os.path.splitext(pdf_file_path)[1], "_summary.txt")
          with open(pdf_summary_file, "w+") as file:
              file.write(pdf_summary_text)

       pdf_file.close()

       with open(pdf_summary_file, "r") as file:
          print(file.read())


      需要说明的是2个事情:
      1、openai的API免费调用额度是有限的,这个方法一篇论文大概在0.2-0.5美元左右,根据论文长度会有变化。
      2、gpt4的API我没测试,因为我还没有申请到,并且看价格那个太贵了(贵20倍)我觉得不值,但是可以试试把论文的图表一同传过去,是不是会有更好效果(不确定)。

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    该用户从未签到

    2#
    发表于 2023-7-28 07:47:43 | 只看该作者

    Really many of valuable knowledge!
    cheap essay writing unique essay writing service admission essay writing service
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-27 21:27 , Processed in 0.071209 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表