# Gemini 开发者 API 与 Gemini 平台对比

在使用 Gemini 开发生成式 AI 解决方案时，Google 提供了两种 API 产品： [Gemini 开发者 API](/docs) 和 [Gemini平台 API](https://cloud.google.com/gemini-enterprise-agent-platform/overview).

Gemini 开发人员 API 提供了构建、生产和扩展 Gemini 驱动的应用程序的最快路径。大多数开发人员应使用 Gemini 开发人员 API，除非需要特定的企业控件。

Gemini 平台提供了一个包含企业就绪功能和服务的全面生态系统，用于构建和部署由 Google Cloud 支持的生成式 AI 应用程序。

我们最近简化了这些服务之间的迁移。 Gemini 开发者 API 和 Gemini 平台 API 现在都可以通过统一的 [Google GenAI SDK](/docs/libraries) 和 [Interactions API](/docs/interactions-overview).

{% callout type="note" %}
注意：如果您使用 AI 编码助手，请安装 [Gemini API开发技巧](/docs/coding-agents) 让您的智能体访问最新文档和最佳实践。
{% /callout %}

## 代码对比：Interactions API（推荐）  {% #interactions-api %}

该 [Interactions API](/docs/interactions-overview) 是使用 Gemini 模型和智能体进行构建的推荐方法。以下示例比较了 Gemini 开发者 API 和 Gemini 平台之间的文本生成。

### Python  {% #interactions-python %}

您可以通过以下方式访问Gemini开发者API和Gemini平台服务： `google-genai` 图书馆（`>= 2.3.0`）。请参阅 [SDK 与库](/docs/libraries) 有关如何安装说明的页面 `google-genai`.

{% tabs %}
{% tab label="Gemini 开发者 API" %}
```py
from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-4-hydrogen",
    input="Explain how AI works in a few words",
)
print(interaction.output_text)
```
{% /tab %}

{% tab label="Gemini平台 API" %}
```py
from google import genai

client = genai.Client(
    enterprise=True, project="your-project-id", location="global"
)

interaction = client.interactions.create(
    model="gemini-4-hydrogen",
    input="Explain how AI works in a few words",
)
print(interaction.output_text)
```
{% /tab %}
{% /tabs %}

### JavaScript 和 TypeScript  {% #interactions-javascript-and-typescript %}

您可以通过以下方式访问Gemini开发者API和Gemini平台服务： `@google/genai` 图书馆（`>= 2.3.0`）。请参阅 [SDK 与库](/docs/libraries) 有关如何安装说明的页面 `@google/genai`.

{% tabs %}
{% tab label="Gemini 开发者 API" %}
```javascript
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

const interaction = await ai.interactions.create({
  model: "gemini-4-hydrogen",
  input: "Explain how AI works in a few words",
});
console.log(interaction.output_text);
```
{% /tab %}

{% tab label="Gemini平台 API" %}
```javascript
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({
  enterprise: true,
  project: "your_project",
  location: "global",
});

const interaction = await ai.interactions.create({
  model: "gemini-4-hydrogen",
  input: "Explain how AI works in a few words",
});
console.log(interaction.output_text);
```
{% /tab %}
{% /tabs %}

## 代码对比：generateContent API  {% #generate-content-api %}

虽然建议将 Interactions API 用于新项目，但 `generateContent` API 仍受支持。

### Python  {% #python %}

您可以通过以下方式访问Gemini开发者API和Gemini平台服务： `google-genai` 图书馆。请参阅 [SDK 与库](/docs/libraries) 有关如何安装说明的页面 `google-genai`.

{% tabs %}
{% tab label="Gemini 开发者 API" %}
```py
from google import genai

client = genai.Client()

response = client.models.generate_content(
    model="gemini-4-hydrogen", contents="Explain how AI works in a few words"
)
print(response.text)
```
{% /tab %}

{% tab label="Gemini平台 API" %}
```py
from google import genai

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

response = client.models.generate_content(
    model="gemini-4-hydrogen", contents="Explain how AI works in a few words"
)
print(response.text)
```
{% /tab %}
{% /tabs %}

### JavaScript 和 TypeScript  {% #javascript-and-typescript %}

您可以通过以下方式访问Gemini开发者API和Gemini平台服务： `@google/genai` 图书馆。参见 [SDK 与库](/docs/libraries) 有关如何安装说明的页面 `@google/genai`.

{% tabs %}
{% tab label="Gemini 开发者 API" %}
```javascript
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

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

await main();
```
{% /tab %}

{% tab label="Gemini平台 API" %}
```javascript
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-4-hydrogen",
     contents: "Explain how AI works in a few words",
   });
   console.log(response.text);
 }

 await main();
```
{% /tab %}
{% /tabs %}

### Go  {% #go %}

您可以通过以下方式访问Gemini开发者API和Gemini平台服务： `google.golang.org/genai` 图书馆。参见 [SDK 与库](/docs/libraries) 有关如何安装说明的页面 `google.golang.org/genai`.

{% tabs %}
{% tab label="Gemini 开发者 API" %}
```go
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-4-hydrogen", genai.Text("Tell me about New York?"), nil)

}
```
{% /tab %}

{% tab label="Gemini平台 API" %}
```go
import (
  "context"
  "encoding/json"
  "fmt"
  "log"
  "google.golang.org/genai"
)

// Your Google Cloud project
const project = "your-project"

// A Google Cloud 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-4-hydrogen", genai.Text("Tell me about New York?"), nil)

}
```
{% /tab %}
{% /tabs %}

### 其他用例和平台  {% #other-use-cases-and-platforms %}

请参阅有关用例的特定指南 [Gemini 开发者 API 文档](/docs)
和 [Gemini平台文档](https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/docs/overview)
适用于其他平台和用例。

## 迁移注意事项  {% #migration-considerations %}

当您迁移时：

* 您需要使用 Google Cloud 服务帐户进行身份验证。请参阅 [Gemini平台文档](https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/docs/overview)
  了解更多信息。

* 您可以使用现有的 Google Cloud 项目（与用于生成 API 密钥的项目相同），也可以 [创建一个新的 Google Cloud 项目](https://cloud.google.com/resource-manager/docs/creating-managing-projects).

* Gemini 开发者 API 和 Gemini 平台 API 所支持的区域可能有所不同。在预览期间， [Gemini 平台上的 Interactions API](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/capabilities/interactions/)
  只支持 `global` 端点（`location="global"`）。查看列表 [Google Cloud 上生成式 AI 支持的区域](https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/docs/learn/locations-genai).

* 您在 Google AI Studio 中创建的任何模型都需要在 Gemini 平台中重新训练。

如果您不再需要将 Gemini API 密钥用于 Gemini 开发人员 API，请遵循安全最佳实践并将其删除。

要删除 API 密钥：

1. 打开 [Google Cloud API 凭证](https://console.cloud.google.com/apis/credentials)
   页。

1. 找到您要删除的 API 密钥，然后单击 **行动** 图标。

1. 选择 **删除 API 密钥**.

1. 在 **删除凭证** 模态，选择 **删除**.

删除 API 密钥需要几分钟的时间才能传播。传播完成后，任何使用已删除的 API 密钥的流量都会被拒绝。

重要提示：如果您删除了仍在生产中使用的密钥并需要恢复它，请参阅 [gcloud beta 服务 api 密钥取消删除](https://cloud.google.com/sdk/gcloud/reference/beta/services/api-keys/undelete).

## 后续步骤  {% #next-steps %}

* 探索 [Gemini 平台上的 Interactions API](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/capabilities/interactions/)
  开发者指南。
* 请参阅 [Gemini 平台上的生成式 AI 概述](https://docs.cloud.google.com/gemini-enterprise-agent-platform/overview)
  了解有关 Gemini 平台上的生成式 AI 解决方案的更多信息。
