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

拥有Gemini 1.5权限但Node.js调用时提示模型不存在

问题:Node.js调用Gemini 1.5 Pro模型报404错误,Python同逻辑代码可正常运行

[404 Not Found] models/gemini-1.5-pro-latest is not found for API version v1, or is not supported for GenerateContent. Call ListModels to see the list of available models and their supported methods.

已确认拥有Gemini 1.5访问权限,相同逻辑的Python代码能正常运行,但Node.js版本执行时出现上述错误,代码如下:

require('dotenv').config();

const API_KEY = process.env.API_KEY; // 从环境变量获取API密钥
const MODEL_NAME = process.env.TEXT_MODEL_NAME_LATEST; // 从环境变量获取模型名称

// 导入@google/generative-ai包中的GoogleGenerativeAI类
const { GoogleGenerativeAI } = require("@google/generative-ai");

// 通过环境变量中的API密钥初始化生成式AI实例
const genAI = new GoogleGenerativeAI(process.env.API_KEY);

async function run() {
    // 针对纯文本输入,使用gemini-pro模型
    const model = genAI.getGenerativeModel({ model: MODEL_NAME});

    const prompt = "写一个关于魔法背包的故事。"

    const result = await model.generateContent(prompt);
    const response = await result.response;
    const text = response.text();
    console.log(text);
}

run();

解决方案

1. 升级Node.js SDK版本

@google/generative-ai旧版本可能不支持Gemini 1.5系列模型,先检查当前包版本:

npm list @google/generative-ai

如果版本较低,升级到最新版:

npm install @google/generative-ai@latest

2. 修正模型名称

Node.js SDK中Gemini 1.5 Pro的正确模型ID是gemini-1.5-pro,而非gemini-1.5-pro-latest。修改环境变量TEXT_MODEL_NAME_LATEST的值为gemini-1.5-pro,或者直接在代码中指定。

3. 验证可用模型(可选)

如果不确定可用模型列表,可以添加以下代码列出所有支持的模型:

async function listModels() {
  const models = await genAI.listModels();
  for (const model of models) {
    console.log(`模型名称: ${model.name}`);
    console.log(`支持的生成方法: ${model.supportedGenerationMethods.join(', ')}\n`);
  }
}
listModels();

修改后的示例代码

require('dotenv').config();

const { GoogleGenerativeAI } = require("@google/generative-ai");

const genAI = new GoogleGenerativeAI(process.env.API_KEY);

async function run() {
    // 使用正确的Gemini 1.5 Pro模型ID
    const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro"});

    const prompt = "写一个关于魔法背包的故事。"

    const result = await model.generateContent(prompt);
    const response = await result.response;
    const text = response.text();
    console.log(text);
}

run();

内容的提问来源于stack exchange,提问作者Linda Lawton - DaImTo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 02:56:09