Gemini 开发者 API 与 Gemini 平台对比
在使用 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.
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)
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)
JavaScript 和 TypeScript #
您可以通过以下方式访问Gemini开发者API和Gemini平台服务: @google/genai 图书馆(>= 2.3.0)。请参阅 SDK 与库 有关如何安装说明的页面 @google/genai.
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);
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);
代码对比:generateContent API #
虽然建议将 Interactions API 用于新项目,但 generateContent API 仍受支持。
Python #
您可以通过以下方式访问Gemini开发者API和Gemini平台服务: google-genai 图书馆。请参阅 SDK 与库 有关如何安装说明的页面 google-genai.
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)
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)
JavaScript 和 TypeScript #
您可以通过以下方式访问Gemini开发者API和Gemini平台服务: @google/genai 图书馆。参见 SDK 与库 有关如何安装说明的页面 @google/genai.
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();
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();
Go #
您可以通过以下方式访问Gemini开发者API和Gemini平台服务: google.golang.org/genai 图书馆。参见 SDK 与库 有关如何安装说明的页面 google.golang.org/genai.
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)
}
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)
}
其他用例和平台 #
请参阅有关用例的特定指南 Gemini 开发者 API 文档 和 Gemini平台文档 适用于其他平台和用例。
迁移注意事项 #
当您迁移时:
您需要使用 Google Cloud 服务帐户进行身份验证。请参阅 Gemini平台文档 了解更多信息。
您可以使用现有的 Google Cloud 项目(与用于生成 API 密钥的项目相同),也可以 创建一个新的 Google Cloud 项目.
Gemini 开发者 API 和 Gemini 平台 API 所支持的区域可能有所不同。在预览期间, Gemini 平台上的 Interactions API 只支持
global端点(location="global")。查看列表 Google Cloud 上生成式 AI 支持的区域.您在 Google AI Studio 中创建的任何模型都需要在 Gemini 平台中重新训练。
如果您不再需要将 Gemini API 密钥用于 Gemini 开发人员 API,请遵循安全最佳实践并将其删除。
要删除 API 密钥:
打开 Google Cloud API 凭证 页。
找到您要删除的 API 密钥,然后单击 行动 图标。
选择 删除 API 密钥.
在 删除凭证 模态,选择 删除.
删除 API 密钥需要几分钟的时间才能传播。传播完成后,任何使用已删除的 API 密钥的流量都会被拒绝。
重要提示:如果您删除了仍在生产中使用的密钥并需要恢复它,请参阅 gcloud beta 服务 api 密钥取消删除.
后续步骤 #
- 探索 Gemini 平台上的 Interactions API 开发者指南。
- 请参阅 Gemini 平台上的生成式 AI 概述 了解有关 Gemini 平台上的生成式 AI 解决方案的更多信息。