You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何使Google Drive图片链接可被多模态LLM读取分析?

问题:Google Drive图片链接无法被多模态LLM读取的解决方法

问题背景

尝试将Google Drive中的图片上传至多模态LLM进行分析,使用维基百科公开图片URL时LLM可正常返回结果,但使用Google Drive共享链接时返回错误,无法读取图片。

现有代码

function openRouterApiRequest() {
  var apiKey = "****";

  // var imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg';
  var imageUrl = "https://drive.google.com/file/d/11178UwHmPb2TAnYCyxFOKXlPh-vSPecv/view?usp=sharing";

  var apiEndpoint = 'https://openrouter.ai/api/v1/chat/completions';
  
  var payload = {
    "model": "meta-llama/llama-4-maverick",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "What is in this image?"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": imageUrl
            }
          }
        ]
      }
    ]
  };
  
  var options = {
    'method': 'post',
    'headers': {
      'Authorization': 'Bearer ' + apiKey,
      'Content-Type': 'application/json'
    },
    'payload': JSON.stringify(payload),
    'muteHttpExceptions': true // To get the response even if the request fails
  };
  
  var response = UrlFetchApp.fetch(apiEndpoint, options);
  var responseCode = response.getResponseCode();
  var responseBody = response.getContentText();
  
  // Handle the response as needed
  Logger.log('Response Code: ' + responseCode);
  Logger.log('Response Body: ' + responseBody);
  
  // You might want to parse the responseBody if it's JSON
  try {
    var jsonResponse = JSON.parse(responseBody);
    Logger.log(jsonResponse);
  } catch (e) {
    Logger.log('Failed to parse response as JSON: ' + e.message);
  }
}

正常测试结果(维基图片URL)

取消注释使用维基图片URL时,LLM返回正常分析结果:

{model=meta-llama/llama-4-maverick, created=1.744144885E9, system_fingerprint=, choices=[{finish_reason=stop, native_finish_reason=stop, index=0.0, logprobs=null, message={content=The image depicts a serene landscape featuring a wooden boardwalk or path that traverses through a lush grassy field. The boardwalk, constructed from weathered wooden planks, is flanked by tall grasses on both sides and appears to be slightly elevated above the surrounding terrain.

错误情况(Google Drive链接)

使用Google Drive共享链接时,返回错误:

{"error":{"message":"Provider returned error","code":502,"metadata":{"raw":"error, status code: 500, status: , message: invalid character 'I' looking for beginning of value, body: ","provider_name":"Novita"}},"user_id":"user_2i6MRzMhAMlWPTYRLTP5uRwhSyx"}

可行解决方法

  • 转换为Google Drive直接下载链接
    Google Drive的共享查看链接是网页地址,而非直接的图片资源URL,需修改为直接下载格式:
    原链接格式:https://drive.google.com/file/d/[FILE_ID]/view?usp=sharing
    修改后格式:https://drive.google.com/uc?export=download&id=[FILE_ID]
    针对本次的链接,修改后为:https://drive.google.com/uc?export=download&id=11178UwHmPb2TAnYCyxFOKXlPh-vSPecv
    将此链接替换代码中的imageUrl即可。

  • 确认文件权限设置
    确保Google Drive中的图片已设置为「任何有链接的人都能查看」,否则LLM服务无法访问该资源。

  • 使用Base64编码传入图片(替代方案)
    如果直接链接仍有问题,可通过Google Apps Script读取图片并转换为Base64编码后传入LLM:

    function getDriveImageBase64(fileId) {
      var file = DriveApp.getFileById(fileId);
      var blob = file.getBlob();
      return 'data:' + blob.getContentType() + ';base64,' + Utilities.base64Encode(blob.getBytes());
    }
    
    // 在原函数中调用并替换payload内容:
    var imageBase64 = getDriveImageBase64('11178UwHmPb2TAnYCyxFOKXlPh-vSPecv');
    // 修改payload里的image_url部分:
    {
      "type": "image_url",
      "image_url": {
        "url": imageBase64
      }
    }
    

    注意:此方法适用于较小的图片,避免Base64编码后数据过大导致请求失败。

内容的提问来源于stack exchange,提问作者user1592380

火山引擎 最新活动