Gemini Developer API 与 Vertex AI

使用 Gemini 开发生成式 AI 解决方案时,Google 提供两款 API 产品:Gemini Developer APIVertex AI Gemini API

Gemini Developer API 可帮助您以最快的速度构建、投入生产和扩缩 Gemini 赋能的应用。大多数开发者应使用 Gemini Developer API,除非需要特定的企业控制功能。

Vertex AI 提供全面的企业级功能和服务生态系统,可用于构建和部署由 Google Cloud 平台提供支持的生成式 AI 应用。

我们最近简化了在这些服务之间迁移数据的流程。现在,您可以通过统一的 Google Gen AI SDK 访问 Gemini Developer API 和 Vertex AI Gemini API。

代码比较

本页面并排比较了 Gemini Developer API 和 Vertex AI 文本生成快速入门中的代码。

Python

您可以通过 google-genai 库访问 Gemini Developer API 和 Vertex AI 服务。如需了解如何安装 google-genai,请参阅页面。

Gemini Developer API

from google import genai

client = genai.Client()

response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Explain how AI works in a few words"
)
print(response.text)

Vertex AI Gemini API

from google import genai

client = genai.Client(
    vertexai=True, project='your-project-id', location='us-central1'
)

response = client.models.generate_content(
    model="gemini-2.0-flash", contents="Explain how AI works in a few words"
)
print(response.text)

JavaScript 和 TypeScript

您可以通过 @google/genai 库访问 Gemini Developer API 和 Vertex AI 服务。如需了解如何安装 @google/genai,请参阅页面。

Gemini Developer API

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents: "Explain how AI works in a few words",
  });
  console.log(response.text);
}

main();

Vertex AI Gemini API

import { GoogleGenAI } from '@google/genai';
const ai = new GoogleGenAI({
  vertexai: true,
  project: 'your_project',
  location: 'your_location',
});

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents: "Explain how AI works in a few words",
  });
  console.log(response.text);
}

main();

Go

您可以通过 google.golang.org/genai 库访问 Gemini Developer API 和 Vertex AI 服务。如需了解如何安装 google.golang.org/genai,请参阅页面。

Gemini Developer API

import (
  "context"
  "encoding/json"
  "fmt"
  "log"
  "google.golang.org/genai"
)

// Your Google API key
const apiKey = "your-api-key"

func main() {
  ctx := context.Background()
  client, err := genai.NewClient(ctx, nil)
  if err != nil {
      log.Fatal(err)
  }

  // Call the GenerateContent method.
  result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", genai.Text("Tell me about New York?"), nil)

}

Vertex AI Gemini API

import (
  "context"
  "encoding/json"
  "fmt"
  "log"
  "google.golang.org/genai"
)

// Your GCP project
const project = "your-project"

// A GCP location like "us-central1"
const location = "some-gcp-location"

func main() {
  ctx := context.Background()
  client, err := genai.NewClient(ctx, &genai.ClientConfig
  {
        Project:  project,
      Location: location,
      Backend:  genai.BackendVertexAI,
  })

  // Call the GenerateContent method.
  result, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", genai.Text("Tell me about New York?"), nil)

}

其他用例和平台

如需了解其他平台和使用情形,请参阅 Gemini Developer API 文档Vertex AI 文档中针对特定使用情形的指南。

迁移注意事项

迁移时:

  • 您需要使用 Google Cloud 服务账号进行身份验证。如需了解详情,请参阅 Vertex AI 文档

  • 您可以使用现有的 Google Cloud 项目(即用于生成 API 密钥的项目),也可以创建新的 Google Cloud 项目

  • Gemini Developer API 和 Vertex AI Gemini API 支持的区域可能会有所不同。请参阅 Google Cloud 上的生成式 AI 支持的区域列表。

  • 您在 Google AI Studio 中创建的任何模型都需要在 Vertex AI 中重新训练。

如果您不再需要使用 Gemini Developer API 的 Gemini API 密钥,请遵循安全性最佳实践并将其删除。

如需删除 API 密钥,请执行以下操作:

  1. 打开 Google Cloud API 凭据页面。

  2. 找到要删除的 API 密钥,然后点击操作图标。

  3. 选择删除 API 密钥

  4. 删除凭据模态框中,选择删除

    删除 API 密钥的操作需要几分钟时间才能生效。生效后,任何使用已删除的 API 密钥的流量都将遭到拒绝。

后续步骤