Gemini Developer API 与 Vertex AI

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

Gemini Developer API 提供了构建、发布和扩展 Gemini 赋能的应用的最快途径。除非需要特定的企业控制功能,否则大多数开发者都应使用 Gemini 开发者 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 开发者 API 和 Vertex AI 服务。如需了解如何安装 google-genai,请参阅页面。

Gemini Developer API

from google import genai

client = genai.Client(api_key="YOUR_API_KEY")

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({ apiKey: "YOUR_API_KEY" });

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, &genai.ClientConfig{
    APIKey:  apiKey,
    Backend: genai.BackendGeminiAPI,
  })

  // 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 开发者 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 密钥的流量都将遭到拒绝。

后续步骤