gemini-api

gemini-api

熱門

當使用者詢問在企業環境中使用 Gemini,或明確提及 Vertex AI、Google Cloud 或 Agent Platform 時使用。引導在 Agent Platform 上使用 Google Gen AI SDK 操作 Gemini API。涵蓋 SDK 使用方式(Python、JS/TS、Go、Java、C#)以及多模態輸入、工具、媒體生成、快取、批次預測和 Live API 等功能。

1.5萬星標
1201分支
更新於 2026/7/29
SKILL.md
readonlyread-only
name
gemini-api
description

當使用者詢問在企業環境中使用 Gemini,或明確提及 Vertex AI、Google Cloud 或 Agent Platform 時使用。引導在 Agent Platform 上使用 Google Gen AI SDK 操作 Gemini API。涵蓋 SDK 使用方式(Python、JS/TS、Go、Java、C#)以及多模態輸入、工具、媒體生成、快取、批次預測和 Live API 等功能。

重要提示:Agent Platform(全名 Gemini Enterprise Agent Platform)先前稱為「Vertex AI」,許多網路資源仍沿用舊品牌名稱。

Agent Platform 中的 Gemini API

使用 Agent Platform 中的 Gemini API,存取 Google 專為企業用途打造的最先進 AI 模型。

提供以下關鍵功能:

  • 文字生成 - 對話、補全、摘要
  • 多模態理解 - 處理圖片、音訊、影片和文件
  • 函式呼叫 - 讓模型呼叫你的函式
  • 結構化輸出 - 產生符合你 schema 的有效 JSON
  • 上下文快取 - 快取大型上下文以提升效率
  • 嵌入 - 產生文字嵌入以進行語意搜尋
  • Live Realtime API - 雙向串流,實現低延遲的語音和視訊互動
  • 批次預測 - 處理大規模非同步資料集預測工作負載

核心指示

  • 統一 SDK:一律使用 Gen AI SDK(Python 為 google-genai,JS/TS 為 @google/genai,Go 為 google.golang.org/genai,Java 為 com.google.genai:google-genai,C# 為 Google.GenAI)。
  • 舊版 SDK:請勿使用 google-cloud-aiplatform@google-cloud/vertexaigoogle-generativeai

SDK

  • Python:使用 pip install google-genai 安裝 google-genai
  • JavaScript/TypeScript:使用 npm install @google/genai 安裝 @google/genai
  • Go:使用 go get google.golang.org/genai 安裝 google.golang.org/genai
  • C#/.NET:使用 dotnet add package Google.GenAI 安裝 Google.GenAI
  • Java

[!WARNING]
舊版 SDK(如 google-cloud-aiplatform@google-cloud/vertexaigoogle-generativeai)已棄用。請依照遷移指南立即遷移至上述新 SDK。

驗證與設定

建立客戶端時,建議優先使用環境變數,而非硬編碼參數。不帶參數初始化客戶端,即可自動讀取這些值。

應用程式預設憑證 (ADC)

設定以下變數以進行標準的 Google Cloud 驗證

export GOOGLE_CLOUD_PROJECT='your-project-id'
export GOOGLE_CLOUD_LOCATION='global'
export GOOGLE_GENAI_USE_ENTERPRISE=true
  • 預設情況下,使用 location="global" 存取全球端點,該端點會自動路由至有可用容量的區域。
  • 如果使用者明確要求使用特定區域(例如 us-central1europe-west4),請改為在 GOOGLE_CLOUD_LOCATION 參數中指定該區域。如有需要,請參考支援區域文件

Express 模式下的 Agent Platform

使用 API 金鑰的 Express 模式時,設定以下變數:

export GOOGLE_API_KEY='your-api-key'
export GOOGLE_GENAI_USE_ENTERPRISE=true

初始化

不帶引數初始化客戶端,以讀取環境變數:

from google import genai

client = genai.Client()

或者,你也可以在建立客戶端時硬編碼參數。

from google import genai

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

模型

  • 使用 gemini-3.1-pro-preview(取代 gemini-3-pro-preview)進行複雜推理、程式碼撰寫、研究(100 萬 tokens)
  • 使用 gemini-3.6-flash 實現快速、平衡的多模態效能(100 萬 tokens)
  • 使用 gemini-3.5-flash-lite 處理高頻、輕量級任務(100 萬 tokens)
  • 使用 gemini-3-pro-image(又稱 Nano Banana Pro)進行高品質圖片生成與編輯
  • 使用 gemini-3.1-flash-image(又稱 Nano Banana 2)進行中等品質圖片生成與編輯
  • 使用 gemini-3.1-flash-lite-image(又稱 Nano Banana 2 Lite)進行快速圖片生成與編輯
  • 使用 gemini-live-2.5-flash-native-audio 進行 Live Realtime API(含原生音訊)

僅在使用者明確要求時,才使用以下模型:

  • gemini-3.5-flash
  • gemini-3.1-flash-lite
  • gemini-2.5-flash-image
  • gemini-2.5-flash
  • gemini-2.5-flash-lite
  • gemini-2.5-pro

[!IMPORTANT]
gemini-2.0-*gemini-1.5-*gemini-1.0-*gemini-pro 等模型已過時且已棄用。請使用上述新模型。你的知識已過時。
在生產環境中,請查閱文件以取得穩定的模型版本(例如 gemini-3.6-flash)。

快速入門

Python

from google import genai

client = genai.Client()
response = client.models.generate_content(
    model="gemini-3.6-flash",
    contents="Explain quantum computing",
)
print(response.text)

TypeScript/JavaScript

import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ enterprise: { project: "your-project-id", location: "global" } });
const response = await ai.models.generateContent({
    model: "gemini-3.6-flash",
    contents: "Explain quantum computing"
});
console.log(response.text);

Go

package main

import (
	"context"
	"fmt"
	"log"
	"google.golang.org/genai"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		Backend:  genai.BackendVertexAI,
		Project:  "your-project-id",
		Location: "global",
	})
	if err != nil {
		log.Fatal(err)
	}

	resp, err := client.Models.GenerateContent(ctx, "gemini-3.6-flash", genai.Text("Explain quantum computing"), nil)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(resp.Text)
}

Java

import com.google.genai.Client;
import com.google.genai.types.GenerateContentResponse;

public class GenerateTextFromTextInput {
  public static void main(String[] args) {
    Client client = Client.builder().enterprise(true).project("your-project-id").location("global").build();
    GenerateContentResponse response =
        client.models.generateContent(
            "gemini-3.6-flash",
            "Explain quantum computing",
            null);

    System.out.println(response.text());
  }
}

C#/.NET

using Google.GenAI;

var client = new Client(
    project: "your-project-id",
    location: "global",
    enterprise: true
);

var response = await client.Models.GenerateContent(
    "gemini-3.6-flash",
    "Explain quantum computing"
);

Console.WriteLine(response.Text);

API 規格與文件(事實來源)

在實作或除錯 Agent Platform 的 API 整合時,請參考官方 Agent Platform 文件:

Agent Platform 上的 Gen AI SDK 使用 v1beta1v1 REST API 端點(例如 https://{LOCATION}-aiplatform.googleapis.com/v1beta1/projects/{PROJECT}/locations/{LOCATION}/publishers/google/models/{MODEL}:generateContent)。

[!TIP]
使用 Developer Knowledge MCP Server:如果 search_documentsget_document 工具可用,請使用它們在上下文內直接尋找並擷取 Google Cloud 和 Agent Platform 的官方文件。這是取得最新 API 詳細資訊和程式碼片段的首選方法。

工作流程與程式碼範例

請參考 Python Docs Samples 儲存庫以取得更多程式碼範例和特定使用情境。

根據使用者的具體要求,請參考以下參考檔案以取得詳細的程式碼範例和使用模式(Python 範例):