Gemini API:通过直接PDF输入彻底改变内容生成

2024年07月31日 由 alex 发表 121 0

1


摘要

Gemini API 现可直接处理 PDF 内容生成,从而消除图像转换并降低成本。本文提供了一个示例脚本来演示这一新功能及其潜在应用。


简介

Gemini API 最近推出了直接处理 PDF 数据以生成内容的功能,大大增强了其功能。以前,要利用 PDF 数据创建内容,必须将每个 PDF 页面转换为单独的图像格式。现在,这一耗时和资源密集型流程已被取消,从而大大降低了处理成本。


通过直接摄取 PDF 内容,Gemini API 为各种应用带来了新的可能性。本文将提供一个示例脚本,演示如何使用 Gemini API 直接从 PDF 数据生成内容,从而有效利用这一功能。


示例脚本

本脚本使用了 Google Apps 脚本中的 GeminiWithFiles。因此,在测试以下脚本之前,请先安装该脚本。


请将以下脚本复制并粘贴到 Google Apps Script 的脚本编辑器中。此外,请设置你的 API 密钥。


async function sample() {
  const apiKey = "###"; // Please set your API key.
  const urls = [
    "https://journals.aps.org/pr/pdf/10.1103/PhysRev.48.73", // from https://journals.aps.org/pr/abstract/10.1103/PhysRev.48.73
    "https://arxiv.org/pdf/1706.03762.pdf", // from https://research.google/pubs/attention-is-all-you-need/
  ];
  const blobs = UrlFetchApp.fetchAll(urls).map((r) => r.getBlob());
  const jsonSchema = {
    description:
      "Summary the following papers within 50 words. Also, retrieve each title and authors of each paper.",
    type: "array",
    items: {
      type: "object",
      properties: {
        title: { description: "Title of the paper", type: "string" },
        authors: { description: "Authors of the paper", type: "string" },
        summary: {
          description: "Summary of the paper within 50 words.",
          type: "string",
        },
      },
      required: ["title", "summary"],
      additionalProperties: false,
    },
  };
  const g = new GeminiWithFiles.geminiWithFiles({
    apiKey,
    response_mime_type: "application/json",
    model: "models/gemini-1.5-pro-latest",
    doCountToken: true,
  });
  const fileList = await g.setBlobs(blobs, false).uploadFiles();
  console.log(fileList); // Here, you can see the metadata of the uploaded data.
  const res = g
    .withUploadedFilesByGenerateContent(fileList)
    .generateContent({ jsonSchema });
  g.deleteFiles(fileList.map(({ name }) => name));
  console.log(res);
}


上述脚本中的 URL 如下。


  • https://journals.aps.org/pr/pdf/10.1103/PhysRev.48.7: 此处来自本页。
  • https://arxiv.org/pdf/1706.03762.pdf: 来自本页。


在此脚本中,PDF 数据被下载并与 Gemini API 配合使用。


结果

运行此脚本后,会得到以下结果。只需调用一次 API,即可解析多个 PDF 文件。


[
  {
    "title": "The Particle Problem in the General Theory of Relativity",
    "authors": "A. EINSTEIN AND N. ROSEN",
    "summary": "This paper explores an atomistic theory of matter and electricity using general relativity and electromagnetism. It modifies gravitational equations to admit regular solutions for static spherically symmetric cases, representing particles as \"bridges\" connecting two identical sheets of space. The theory explains the absence of negative mass particles and offers a unified treatment of field and motion."
  },
  {
    "title": "Attention Is All You Need",
    "authors": "Ashish Vaswani, Noam Shazeer, Niki Parmar, Llion Jones, Aidan N. Gomez, Illia Polosukhin, Łukasz Kaiser, Jakob Uszkoreit",
    "summary": "This paper introduces the Transformer, a novel network architecture based solely on attention mechanisms for sequence transduction tasks. It replaces traditional recurrent and convolutional layers with multi-head self-attention, enabling superior parallelization and performance. Experiments on machine translation show significant improvements in BLEU scores and training time, achieving state-of-the-art results."
  }
]


脚本利用 const fileList = await g.setBlobs(blobs, false).uploadFiles(); 行的参数 false 来决定 Gemini API 如何处理 PDF 数据。在这里,参数 false 表示脚本直接将 PDF 数据送入 API 进行处理。相反,当参数设置为 true 时(如 const fileList = await g.setBlobs(blobs,true).uploadFiles();),脚本会将 PDF 文件的每一页转换为单独的图像,然后再发送给 API。


该实验比较了直接使用 PDF 数据和将 PDF 转换为图像的处理时间。以下是详细数据:


  • 直接 PDF 处理:15 秒
  • 基于图像的处理 120 秒


(分别对 https://journals.aps.org/pr/pdf/10.1103/PhysRev.48.7 和 https://arxiv.org/pdf/1706.03762.pdf 中 5 页和 15 页的 PDF 进行了测试)


结果表明,与基于图像的方法相比,直接向 Gemini API 输入 PDF 数据的处理成本要低得多。这可能是由于每一页的图像转换相关的开销。


文章来源:https://medium.com/google-cloud/gemini-api-revolutionizing-content-generation-with-direct-pdf-input-105493780fa4
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消