Gemini 中文指南
获取 API 密钥 ↗

Gemini 开发者 API 与 Gemini 平台对比

↓ 下载 MarkdownInteractions API

在使用 Gemini 开发生成式 AI 解决方案时,Google 提供了两种 API 产品: Gemini 开发者 API 和 Gemini平台 API.

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

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

我们最近简化了这些服务之间的迁移。 Gemini 开发者 API 和 Gemini 平台 API 现在都可以通过统一的 Google GenAI SDK 和 Interactions API.

代码对比:Interactions API(推荐) #

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

Python #

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

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)

JavaScript 和 TypeScript #

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

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);

代码对比:generateContent API #

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

Python #

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

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)

JavaScript 和 TypeScript #

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

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();

Go #

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

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)

}

其他用例和平台 #

请参阅有关用例的特定指南 Gemini 开发者 API 文档 和 Gemini平台文档 适用于其他平台和用例。

迁移注意事项 #

当您迁移时:

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

要删除 API 密钥:

  1. 打开 Google Cloud API 凭证 页。

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

  3. 选择 删除 API 密钥.

  4. 在 删除凭证 模态,选择 删除.

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

重要提示:如果您删除了仍在生产中使用的密钥并需要恢复它,请参阅 gcloud beta 服务 api 密钥取消删除.

后续步骤 #

搜索标题、API 名称或正文内容