Gemini Developer API 與 Gemini platform¶
使用 Gemini 開發生成式 AI 解決方案時,Google 提供兩種 API 產品:Gemini Developer API 與 Gemini platform API。
Gemini Developer API 提供建置、投入生產與擴充 Gemini 應用程式的最快途徑。除非需要特定的企業級控管功能,否則大多數開發人員都應使用 Gemini Developer API。
Gemini platform 提供完整的企業級功能與服務生態系,可用於建置與部署由 Google Cloud 支援的生成式 AI 應用程式。
我們近期簡化了這兩項服務之間的遷移流程。Gemini Developer API 與 Gemini platform API 現在都可透過統一的 Google GenAI SDK 與 Interactions API 存取。
注意:如果你使用 AI 程式設計助理,請安裝 Gemini API 開發技能,讓你的代理能取得最新文件與最佳做法。
程式碼比較:Interactions API(建議)¶
Interactions API 是使用 Gemini 模型與代理進行建置的建議方式。以下範例比較 Gemini Developer API 與 Gemini platform 之間的文字生成寫法。
Python¶
你可以透過 google-genai 程式庫(>= 2.3.0)同時存取 Gemini Developer API 與 Gemini platform 服務。google-genai 的安裝說明請參閱 libraries 頁面。
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¶
你可以透過 @google/genai 程式庫(>= 2.3.0)同時存取 Gemini Developer API 與 Gemini platform 服務。@google/genai 的安裝說明請參閱 libraries 頁面。
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¶
你可以透過 google-genai 程式庫同時存取 Gemini Developer API 與 Gemini platform 服務。google-genai 的安裝說明請參閱 libraries 頁面。
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¶
你可以透過 @google/genai 程式庫同時存取 Gemini Developer API 與 Gemini platform 服務。@google/genai 的安裝說明請參閱 libraries 頁面。
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¶
你可以透過 google.golang.org/genai 程式庫同時存取 Gemini Developer API 與 Gemini platform 服務。google.golang.org/genai 的安裝說明請參閱 libraries 頁面。
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 Developer API 文件 與 Gemini platform 文件 中針對特定用途的指南。
遷移注意事項¶
遷移時請注意:
-
你需要使用 Google Cloud 服務帳戶進行驗證。詳情請參閱 Gemini platform 文件。
-
你可以使用現有的 Google Cloud 專案(與你用來產生 API 金鑰的同一個專案),也可以建立新的 Google Cloud 專案。
-
Gemini Developer API 與 Gemini platform API 支援的區域可能不同。在預覽期間,Gemini platform 上的 Interactions API 僅支援
global端點(location="global")。請參閱 Google Cloud 上生成式 AI 的支援區域清單。 -
你在 Google AI Studio 中建立的任何模型,都需要在 Gemini platform 中重新訓練。
如果你不再需要使用 Gemini API 金鑰來存取 Gemini Developer API,請遵循安全性最佳做法將其刪除。
刪除 API 金鑰的步驟如下:
-
開啟 Google Cloud API 憑證 頁面。
-
找到你要刪除的 API 金鑰,並點選 Actions 圖示。
-
選取 Delete API key。
-
在 Delete credential 對話框中,選取 Delete。
刪除 API 金鑰需要幾分鐘才會生效。生效完成後,任何使用已刪除 API 金鑰的流量都會遭到拒絕。
重要事項:如果你刪除的金鑰仍在正式環境中使用且需要復原,請參閱 gcloud beta services api-keys undelete。
後續步驟¶
- 探索 Gemini platform 上的 Interactions API 開發人員指南。
- 參閱 Gemini platform 上的生成式 AI 總覽,進一步了解 Gemini platform 上的生成式 AI 解決方案。