使用 Gemini 開發生成式 AI 解決方案時,開發人員可以選擇使用 Gemini Developer API 或 Vertex AI Gemini API。
Gemini Developer API 可讓您輕鬆建構、正式發布及擴充 Gemini 應用程式。Vertex AI 提供完整的企業級功能和服務生態系統,可協助您建構及部署 Google Cloud Platform 支援的生成式 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 金鑰的做法如下:
開啟 Google Cloud API 憑證頁面。
找出要刪除的 API 金鑰,然後按一下「動作」圖示。
選取「Delete API key」(刪除 API 金鑰)。
在「刪除憑證」對話方塊中,選取「刪除」。
刪除 API 金鑰需要幾分鐘的時間才會生效。作業完畢後,凡是使用已刪除 API 金鑰的流量都會遭拒。
後續步驟
- 請參閱 Vertex AI 生成式 AI 總覽,進一步瞭解 Vertex AI 的生成式 AI 解決方案。