Vertex AI Gemini API

使用 Gemini 开发生成式 AI 解决方案时,开发者可以选择 Gemini Developer APIVertex AI Gemini API

Gemini Developer API 提供了一种简单的方式来构建、发布和扩缩 Gemini 应用。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 服务。 如需了解如何安装该库,请参阅页面。

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)
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 服务。 如需了解如何安装该库,请参阅页面。

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();
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 服务。 如需了解如何安装该库,请参阅页面。

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)

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

后续步骤