You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Apps Script中调用微调Gemini模型?免费层可行吗?

连接微调Gemini模型到Google Apps Script的方法

免费层可行性说明

Gemini API免费层(Gemini 1.5 Flash)不支持调用自定义微调模型,你需要升级到Gemini Advanced订阅或者Pay-as-you-go(按需付费)计划,才能获得微调模型的API调用权限。

分步操作流程(需已升级至符合要求的付费计划,且完成模型微调)

1. 获取微调模型的模型ID

  • 打开Google AI Studio,进入「我的模型」页面
  • 找到已完成微调的目标模型,复制它的模型ID(格式示例:projects/[你的项目ID]/locations/[区域]/models/[微调模型ID])

2. 修改Apps Script中的API端点

将原通用模型端点替换为微调模型的专属端点,调用方法仍使用generateContent,示例代码如下:

const geminiApiKey = "你的API密钥";
// 替换为你复制的微调模型ID
const fineTunedModelId = "projects/你的项目ID/locations/us-central1/models/你的微调模型ID";
const geminiEndpoint = `https://generativelanguage.googleapis.com/v1beta/${fineTunedModelId}:generateContent?key=${geminiApiKey}`;

3. 编写请求代码调用微调模型

沿用原有请求逻辑,构造请求体并发送POST请求,完整示例代码:

function callFineTunedGemini() {
  const geminiApiKey = "你的API密钥";
  const fineTunedModelId = "projects/你的项目ID/locations/us-central1/models/你的微调模型ID";
  const endpoint = `https://generativelanguage.googleapis.com/v1beta/${fineTunedModelId}:generateContent?key=${geminiApiKey}`;

  // 构造请求内容,可根据你的微调场景调整文本
  const requestBody = {
    contents: [{
      parts: [{ text: "你的提问内容" }]
    }]
  };

  const options = {
    method: "POST",
    contentType: "application/json",
    payload: JSON.stringify(requestBody)
  };

  try {
    const response = UrlFetchApp.fetch(endpoint, options);
    const result = JSON.parse(response.getContentText());
    // 打印模型返回结果
    console.log(result.candidates[0].content.parts[0].text);
  } catch (error) {
    console.error("调用失败:", error);
  }
}

4. 验证权限(若遇错误时检查)

  • 确保你的API密钥关联的账号拥有对应Google Cloud项目的aiplatform.models.predict权限
  • 可在Google Cloud控制台的IAM页面,为账号添加「AI Platform预测用户」等相关角色

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 13:33:13