Gemini 3 開發人員指南

Gemini 3 是我們迄今最聰明的模型系列,以最先進的推論技術為基礎建構而成。這項工具可掌握代理式工作流程、自主編碼和複雜的多模態工作,將任何想法化為現實。本指南將介紹 Gemini 3 模型系列的主要功能,以及如何充分發揮這些功能的作用。

歡迎瀏覽 Gemini 3 應用程式系列,瞭解模型如何處理進階推理、自主程式設計和複雜的多模態工作。

只要編寫幾行程式碼,即可開始使用:

Python

from google import genai

client = genai.Client()

response = client.models.generate_content(
    model="gemini-3.1-pro-preview",
    contents="Find the race condition in this multi-threaded C++ snippet: [code here]",
)

print(response.text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function run() {
  const response = await ai.models.generateContent({
    model: "gemini-3.1-pro-preview",
    contents: "Find the race condition in this multi-threaded C++ snippet: [code here]",
  });

  console.log(response.text);
}

run();

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-pro-preview:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [{
      "parts": [{"text": "Find the race condition in this multi-threaded C++ snippet: [code here]"}]
    }]
  }'

認識 Gemini 3 系列

Gemini 3 Pro 是新系列的第一個模型,最適合需要廣泛世界知識和跨模態進階推理的複雜任務。Gemini 3.1 Pro 是 3 Pro 系列的下一個迭代版本,在效能、行為和智慧方面都有所提升。

Gemini 3 Flash 是我們最新的 3 系列模型,具備 Pro 級智慧,但速度和價格與 Flash 相同。

Nano Banana Pro (又稱 Gemini 3 Pro Image) 是我們迄今品質最優的圖像生成模型。

所有 Gemini 3 模型目前皆為預先發布版。

模型 ID 背景期間 (進 / 出) 知識截點 定價 (輸入 / 輸出)*
gemini-3.1-pro-preview 100 萬次 / 6.4 萬次 2025 年 1 月 $2 美元 / $12 美元 (少於 20 萬個權杖)
$4 美元 / $18 美元 (超過 20 萬個權杖)
gemini-3-pro-preview 100 萬次 / 6.4 萬次 2025 年 1 月 $2 美元 / $12 美元 (少於 20 萬個權杖)
$4 美元 / $18 美元 (超過 20 萬個權杖)
gemini-3-flash-preview 100 萬次 / 6.4 萬次 2025 年 1 月 $0.50 / $3
gemini-3-pro-image-preview 65,000 / 32,000 2025 年 1 月 $2 美元 (文字輸入) / $0.134 美元 (圖片輸出)**

* 除非另有註明,否則價格以每 100 萬個權杖為單位。 ** 圖片價格會因解析度而異。詳情請參閱定價頁面

如需詳細的限制、定價和其他資訊,請參閱模型頁面

Gemini 3 的新 API 功能

Gemini 3 推出全新參數,讓開發人員進一步掌控延遲時間、成本和多模態準確度。

思考程度

Gemini 3 系列模型預設會使用動態思考功能,推論提示內容。您可以使用 thinking_level 參數,控制模型產生回覆前內部推理過程的最大深度。Gemini 3 會將這些層級視為思考的相對配額,而非嚴格的權杖保證。

如未指定 thinking_level,Gemini 3 會預設為 high。如果不需要複雜的推理,可以將模型思考層級限制為 low,加快回應速度並降低延遲。

思考程度 Gemini 3.1 Pro Gemini 3 Pro Gemini 3 Flash 說明
minimal 不支援 不支援 支援 針對大多數查詢,比照「不思考」設定。模型可能會以極簡思維處理複雜的程式碼編寫工作。盡量減少聊天或高處理量應用程式的延遲。請注意,minimal 無法保證思考功能已關閉。
low 支援 支援 支援 盡量縮短延遲時間並降低成本。適合簡單的指令遵循、即時通訊或高處理量應用程式。
medium 支援 不支援 支援 適合用於大多數工作。
high 支援 (預設、動態) 支援 (預設、動態) 支援 (預設、動態) 盡可能深入推理。模型可能需要較長時間才能產生第一個 (非思考) 輸出權杖,但輸出內容會經過更仔細的推理。

Python

from google import genai
from google.genai import types

client = genai.Client()

response = client.models.generate_content(
    model="gemini-3.1-pro-preview",
    contents="How does AI work?",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(thinking_level="low")
    ),
)

print(response.text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

const response = await ai.models.generateContent({
    model: "gemini-3.1-pro-preview",
    contents: "How does AI work?",
    config: {
      thinkingConfig: {
        thinkingLevel: "low",
      }
    },
  });

console.log(response.text);

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-pro-preview:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [{
      "parts": [{"text": "How does AI work?"}]
    }],
    "generationConfig": {
      "thinkingConfig": {
        "thinkingLevel": "low"
      }
    }
  }'

媒體解析度

Gemini 3 透過 media_resolution 參數,提供多模態視覺處理的精細控制項。解析度越高,模型就越能辨識細小文字或細節,但也會增加權杖用量和延遲時間。media_resolution 參數會決定每個輸入圖片或影片影格分配的詞元數量上限

現在可以為每個媒體部分或全域設定解析度 (透過 generation_config,全域不適用於超高解析度),解析度可設為 media_resolution_lowmedia_resolution_mediummedia_resolution_highmedia_resolution_ultra_high。如未指定,模型會根據媒體類型使用最佳預設值。

建議設定

媒體類型 建議設定 權杖數量上限 使用指南
圖片 media_resolution_high 1120 建議用於大多數圖片分析工作,確保最高品質。
PDF media_resolution_medium 560 最適合用於瞭解文件內容;品質通常會在 medium 達到飽和。增加到 high 很少能改善標準文件的 OCR 結果。
影片 (一般) media_resolution_low (或 media_resolution_medium) 70 (每格) 注意:對於影片,系統會將 lowmedium 設定視為相同 (70 個權杖),以最佳化情境使用情形。這足以應付大多數的動作辨識和描述工作。
影片 (文字內容較多) media_resolution_high 280 (每影格) 只有在用途涉及讀取密集文字 (OCR) 或影片影格中的細節時,才需要此功能。

Python

from google import genai
from google.genai import types
import base64

# The media_resolution parameter is currently only available in the v1alpha API version.
client = genai.Client(http_options={'api_version': 'v1alpha'})

response = client.models.generate_content(
    model="gemini-3.1-pro-preview",
    contents=[
        types.Content(
            parts=[
                types.Part(text="What is in this image?"),
                types.Part(
                    inline_data=types.Blob(
                        mime_type="image/jpeg",
                        data=base64.b64decode("..."),
                    ),
                    media_resolution={"level": "media_resolution_high"}
                )
            ]
        )
    ]
)

print(response.text)

JavaScript

import { GoogleGenAI } from "@google/genai";

// The media_resolution parameter is currently only available in the v1alpha API version.
const ai = new GoogleGenAI({ apiVersion: "v1alpha" });

async function run() {
  const response = await ai.models.generateContent({
    model: "gemini-3.1-pro-preview",
    contents: [
      {
        parts: [
          { text: "What is in this image?" },
          {
            inlineData: {
              mimeType: "image/jpeg",
              data: "...",
            },
            mediaResolution: {
              level: "media_resolution_high"
            }
          }
        ]
      }
    ]
  });

  console.log(response.text);
}

run();

REST

curl "https://generativelanguage.googleapis.com/v1alpha/models/gemini-3.1-pro-preview:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [{
      "parts": [
        { "text": "What is in this image?" },
        {
          "inlineData": {
            "mimeType": "image/jpeg",
            "data": "..."
          },
          "mediaResolution": {
            "level": "media_resolution_high"
          }
        }
      ]
    }]
  }'

溫度

對於所有 Gemini 3 模型,我們強烈建議將溫度參數維持在預設值 1.0

先前的模型通常會調整溫度,以控制創意與決定性,但 Gemini 3 的推理能力已針對預設設定進行最佳化。變更溫度 (設為低於 1.0) 可能會導致非預期的行為,例如迴圈或效能降低,特別是在複雜的數學或推理工作。

思想簽名

Gemini 3 會使用思維簽章,在 API 呼叫之間維持推理脈絡。這些簽章是模型內部思考過程的加密表示法。為確保模型維持推理能力,您必須在要求中將這些簽章傳回模型,且簽章內容必須與收到的完全一致:

  • 函式呼叫 (嚴格):API 會對「目前回合」強制執行嚴格驗證。如果缺少簽章,系統會顯示 400 錯誤。

  • 文字/對話:系統不會嚴格執行驗證,但如果省略簽章,模型的推理和回覆品質就會降低。

  • 圖像生成/編輯 (嚴格):API 會對所有模型部分 (包括 thoughtSignature) 進行嚴格驗證。如果缺少簽章,系統會顯示 400 錯誤。

函式呼叫 (嚴格驗證)

Gemini 生成 functionCall 時,會依據 thoughtSignature 在下一個回合中正確處理工具輸出內容。「目前回合」包含自上次標準「使用者」text 訊息以來,所有「模型」functionCall和「使用者」functionResponse步驟。

  • 單一函式呼叫:functionCall 部分包含簽章。你必須歸還。
  • 平行函式呼叫:清單中只有第一個 functionCall 部分會包含簽章。請務必按照收到零件時的順序退回。
  • 多步驟 (循序):如果模型呼叫工具、收到結果,並呼叫另一個工具 (在同一回合內),兩個函式呼叫都會有簽章。您必須退回歷史記錄中所有累積的簽名。

文字和串流

如果是標準的即時通訊或文字生成,系統不保證會顯示簽名。

  • 非串流:回應的最終內容部分可能包含 thoughtSignature,但並非一律如此。如果收到退回的裝置,請務必寄回,以維持最佳效能。
  • 串流:如果產生簽章,簽章可能會出現在含有空白文字部分的最後一個區塊。即使文字欄位空白,也請確保串流剖析器會檢查簽章。

生成及編輯圖像

對於gemini-3-pro-image-preview,思想簽章是會話式編輯的關鍵。要求模型修改圖片時,模型會根據前一輪的 thoughtSignature 瞭解原始圖片的構圖和邏輯。

  • 編輯:簽章保證會出現在回覆內容的第一部分 (textinlineData) 和後續的每個 inlineData 部分。您必須傳回所有這些簽章,以免發生錯誤。

程式碼範例

多步驟函式呼叫 (循序)

使用者在同一輪對話中提出需要兩個步驟的問題 (查看航班 -> 預訂計程車)。

步驟 1:模型呼叫 Flight Tool。
模型會傳回簽章 <Sig_A>

// Model Response (Turn 1, Step 1)
  {
    "role": "model",
    "parts": [
      {
        "functionCall": { "name": "check_flight", "args": {...} },
        "thoughtSignature": "<Sig_A>" // SAVE THIS
      }
    ]
  }

步驟 2:使用者傳送航班結果
我們必須傳回 <Sig_A>,才能維持模型的思路。

// User Request (Turn 1, Step 2)
[
  { "role": "user", "parts": [{ "text": "Check flight AA100..." }] },
  { 
    "role": "model", 
    "parts": [
      { 
        "functionCall": { "name": "check_flight", "args": {...} }, 
        "thoughtSignature": "<Sig_A>" // REQUIRED
      } 
    ]
  },
  { "role": "user", "parts": [{ "functionResponse": { "name": "check_flight", "response": {...} } }] }
]

步驟 3:模型呼叫 Taxi Tool
模型透過 <Sig_A> 記住航班延誤,現在決定預約計程車。系統會產生簽章 <Sig_B>

// Model Response (Turn 1, Step 3)
{
  "role": "model",
  "parts": [
    {
      "functionCall": { "name": "book_taxi", "args": {...} },
      "thoughtSignature": "<Sig_B>" // SAVE THIS
    }
  ]
}

步驟 4:使用者傳送 Taxi Result
如要完成回合,您必須傳回整個鏈結:<Sig_A><Sig_B>

// User Request (Turn 1, Step 4)
[
  // ... previous history ...
  { 
    "role": "model", 
    "parts": [
       { "functionCall": { "name": "check_flight", ... }, "thoughtSignature": "<Sig_A>" } 
    ]
  },
  { "role": "user", "parts": [{ "functionResponse": {...} }] },
  { 
    "role": "model", 
    "parts": [
       { "functionCall": { "name": "book_taxi", ... }, "thoughtSignature": "<Sig_B>" } 
    ]
  },
  { "role": "user", "parts": [{ "functionResponse": {...} }] }
]

平行函式呼叫

使用者詢問:「Check the weather in Paris and London」(查看巴黎和倫敦的天氣)。模型會在一個回覆中傳回兩個函式呼叫。

// User Request (Sending Parallel Results)
[
  {
    "role": "user",
    "parts": [
      { "text": "Check the weather in Paris and London." }
    ]
  },
  {
    "role": "model",
    "parts": [
      // 1. First Function Call has the signature
      {
        "functionCall": { "name": "check_weather", "args": { "city": "Paris" } },
        "thoughtSignature": "<Signature_A>" 
      },
      // 2. Subsequent parallel calls DO NOT have signatures
      {
        "functionCall": { "name": "check_weather", "args": { "city": "London" } }
      } 
    ]
  },
  {
    "role": "user",
    "parts": [
      // 3. Function Responses are grouped together in the next block
      {
        "functionResponse": { "name": "check_weather", "response": { "temp": "15C" } }
      },
      {
        "functionResponse": { "name": "check_weather", "response": { "temp": "12C" } }
      }
    ]
  }
]

文字/情境內推理 (無驗證)

使用者提出的問題需要根據情境推理,但不能使用外部工具。雖然不會嚴格驗證,但加入簽章有助於模型保留後續問題的推理鏈。

// User Request (Follow-up question)
[
  {
    "role": "user",
    "parts": [{ "text": "What are the risks of this investment?" }]
  },
  {
    "role": "model",
    "parts": [
      {
        "text": "I need to calculate the risk step-by-step. First, I'll look at volatility...",
        "thoughtSignature": "<Signature_C>" // Recommended to include
      }
    ]
  },
  {
    "role": "user",
    "parts": [{ "text": "Summarize that in one sentence." }]
  }
]

圖像生成和編輯

生成圖片時,系統會嚴格驗證簽章。浮水印會顯示在第一部分 (文字或圖片) 和所有後續圖片部分。所有卡片都必須在下一回合歸還。

// Model Response (Turn 1)
{
  "role": "model",
  "parts": [
    // 1. First part ALWAYS has a signature (even if text)
    {
      "text": "I will generate a cyberpunk city...",
      "thoughtSignature": "<Signature_D>"
    },
    // 2. ALL InlineData (Image) parts ALWAYS have signatures
    {
      "inlineData": { ... }, 
      "thoughtSignature": "<Signature_E>"
    },
  ]
}

// User Request (Turn 2 - Requesting an Edit)
{
  "contents": [
    // History must include ALL signatures received
    {
      "role": "user",
      "parts": [{ "text": "Generate a cyberpunk city" }]
    },
    {
      "role": "model",
      "parts": [
         { "text": "...", "thoughtSignature": "<Signature_D>" },
         { "inlineData": "...", "thoughtSignature": "<Signature_E>" },
      ]
    },
    // New User Prompt
    {
      "role": "user",
      "parts": [{ "text": "Make it daytime." }]
    }
  ]
}

從其他機型遷移

如要從其他模型 (例如 Gemini 1.0 Pro) 轉移對話記錄,2.5) 或插入非由 Gemini 3 生成的自訂函式呼叫,您將無法取得有效簽章。

如要在這些特定情境中略過嚴格驗證,請在欄位中填入這個特定虛擬字串:"thoughtSignature": "context_engineering_is_the_way to_go"

使用工具產生結構化輸出內容

Gemini 3 模型可讓您將結構化輸出內容與內建工具結合使用,包括以 Google 搜尋建立基準網址內容程式碼執行函式呼叫

Python

from google import genai
from google.genai import types
from pydantic import BaseModel, Field
from typing import List

class MatchResult(BaseModel):
    winner: str = Field(description="The name of the winner.")
    final_match_score: str = Field(description="The final match score.")
    scorers: List[str] = Field(description="The name of the scorer.")

client = genai.Client()

response = client.models.generate_content(
    model="gemini-3.1-pro-preview",
    contents="Search for all details for the latest Euro.",
    config={
        "tools": [
            {"google_search": {}},
            {"url_context": {}}
        ],
        "response_mime_type": "application/json",
        "response_json_schema": MatchResult.model_json_schema(),
    },  
)

result = MatchResult.model_validate_json(response.text)
print(result)

JavaScript

import { GoogleGenAI } from "@google/genai";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";

const ai = new GoogleGenAI({});

const matchSchema = z.object({
  winner: z.string().describe("The name of the winner."),
  final_match_score: z.string().describe("The final score."),
  scorers: z.array(z.string()).describe("The name of the scorer.")
});

async function run() {
  const response = await ai.models.generateContent({
    model: "gemini-3.1-pro-preview",
    contents: "Search for all details for the latest Euro.",
    config: {
      tools: [
        { googleSearch: {} },
        { urlContext: {} }
      ],
      responseMimeType: "application/json",
      responseJsonSchema: zodToJsonSchema(matchSchema),
    },
  });

  const match = matchSchema.parse(JSON.parse(response.text));
  console.log(match);
}

run();

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-pro-preview:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [{
      "parts": [{"text": "Search for all details for the latest Euro."}]
    }],
    "tools": [
      {"googleSearch": {}},
      {"urlContext": {}}
    ],
    "generationConfig": {
        "responseMimeType": "application/json",
        "responseJsonSchema": {
            "type": "object",
            "properties": {
                "winner": {"type": "string", "description": "The name of the winner."},
                "final_match_score": {"type": "string", "description": "The final score."},
                "scorers": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "The name of the scorer."
                }
            },
            "required": ["winner", "final_match_score", "scorers"]
        }
    }
  }'

圖像生成

Gemini 3 Pro Image 可讓你根據文字提示生成及編輯圖片。這項功能會運用推理能力「思考」提示,並擷取即時資料 (例如天氣預報或股票圖表),然後使用 Google 搜尋基礎模型生成高擬真圖片。

全新和改良功能:

  • 4K 和文字算繪:生成清晰易讀的文字和圖表,解析度最高可達 2K 和 4K。
  • 以事實為依據的生成內容:使用 google_search 工具驗證事實,並根據真實世界資訊生成圖像。
  • 對話式編輯:只要要求變更 (例如「將背景換成海灘」),即可多輪編輯圖像。「將背景設為日落」。這個工作流程會使用思維簽章,在對話輪次之間保留視覺情境。

如要進一步瞭解長寬比、編輯工作流程和設定選項,請參閱圖片生成指南

Python

from google import genai
from google.genai import types

client = genai.Client()

response = client.models.generate_content(
    model="gemini-3-pro-image-preview",
    contents="Generate an infographic of the current weather in Tokyo.",
    config=types.GenerateContentConfig(
        tools=[{"google_search": {}}],
        image_config=types.ImageConfig(
            aspect_ratio="16:9",
            image_size="4K"
        )
    )
)

image_parts = [part for part in response.parts if part.inline_data]

if image_parts:
    image = image_parts[0].as_image()
    image.save('weather_tokyo.png')
    image.show()

JavaScript

import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";

const ai = new GoogleGenAI({});

async function run() {
  const response = await ai.models.generateContent({
    model: "gemini-3-pro-image-preview",
    contents: "Generate a visualization of the current weather in Tokyo.",
    config: {
      tools: [{ googleSearch: {} }],
      imageConfig: {
        aspectRatio: "16:9",
        imageSize: "4K"
      }
    }
  });

  for (const part of response.candidates[0].content.parts) {
    if (part.inlineData) {
      const imageData = part.inlineData.data;
      const buffer = Buffer.from(imageData, "base64");
      fs.writeFileSync("weather_tokyo.png", buffer);
    }
  }
}

run();

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-pro-image-preview:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [{
      "parts": [{"text": "Generate a visualization of the current weather in Tokyo."}]
    }],
    "tools": [{"googleSearch": {}}],
    "generationConfig": {
        "imageConfig": {
          "aspectRatio": "16:9",
          "imageSize": "4K"
      }
    }
  }'

範例回應

東京天氣

使用圖片執行程式碼

Gemini 3 Flash 可將影像視為主動調查,而不只是靜態瀏覽。模型會結合推理和程式碼執行功能,制定計畫,然後編寫及執行 Python 程式碼,逐步縮放、裁剪、註解或以其他方式處理圖片,以便根據視覺內容提供答案。

用途:

  • 縮放及檢查:模型會隱含地偵測細節是否過小 (例如讀取遠處的儀表或序號),並編寫程式碼來裁剪及重新檢查該區域,提高解析度。
  • 視覺化數學和繪圖:模型可以使用程式碼執行多步驟計算 (例如加總收據上的項目,或從擷取的資料產生 Matplotlib 圖表)。
  • 圖片註解:模型可以直接在圖片上繪製箭頭、定界框或其他註解,回答「這個項目應該放在哪裡?」等空間問題。

如要啟用視覺化思考功能,請將「程式碼執行」設定為工具。模型會在必要時自動使用程式碼來處理圖片。

Python

from google import genai
from google.genai import types
import requests
from PIL import Image
import io

image_path = "https://goo.gle/instrument-img"
image_bytes = requests.get(image_path).content
image = types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg")

client = genai.Client()

response = client.models.generate_content(
    model="gemini-3-flash-preview",
    contents=[
        image,
        "Zoom into the expression pedals and tell me how many pedals are there?"
    ],
    config=types.GenerateContentConfig(
        tools=[types.Tool(code_execution=types.ToolCodeExecution)]
    ),
)

for part in response.candidates[0].content.parts:
    if part.text is not None:
        print(part.text)
    if part.executable_code is not None:
        print(part.executable_code.code)
    if part.code_execution_result is not None:
        print(part.code_execution_result.output)
    if part.as_image() is not None:
        display(Image.open(io.BytesIO(part.as_image().image_bytes)))

JavaScript

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});

async function main() {
  const imageUrl = "https://goo.gle/instrument-img";
  const response = await fetch(imageUrl);
  const imageArrayBuffer = await response.arrayBuffer();
  const base64ImageData = Buffer.from(imageArrayBuffer).toString("base64");

  const result = await ai.models.generateContent({
    model: "gemini-3-flash-preview",
    contents: [
      {
        inlineData: {
          mimeType: "image/jpeg",
          data: base64ImageData,
        },
      },
      {
        text: "Zoom into the expression pedals and tell me how many pedals are there?",
      },
    ],
    config: {
      tools: [{ codeExecution: {} }],
    },
  });

  for (const part of result.candidates[0].content.parts) {
    if (part.text) {
      console.log("Text:", part.text);
    }
    if (part.executableCode) {
      console.log("Code:", part.executableCode.code);
    }
    if (part.codeExecutionResult) {
      console.log("Output:", part.codeExecutionResult.output);
    }
  }
}

main();

REST

IMG_URL="https://goo.gle/instrument-img"
MODEL="gemini-3-flash-preview"

MIME_TYPE=$(curl -sIL "$IMG_URL" | grep -i '^content-type:' | awk -F ': ' '{print $2}' | sed 's/\r$//' | head -n 1)
if [[ -z "$MIME_TYPE" || ! "$MIME_TYPE" == image/* ]]; then
  MIME_TYPE="image/jpeg"
fi

if [[ "$(uname)" == "Darwin" ]]; then
  IMAGE_B64=$(curl -sL "$IMG_URL" | base64 -b 0)
elif [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
  IMAGE_B64=$(curl -sL "$IMG_URL" | base64)
else
  IMAGE_B64=$(curl -sL "$IMG_URL" | base64 -w0)
fi

curl "https://generativelanguage.googleapis.com/v1beta/models/$MODEL:generateContent" \
    -H "x-goog-api-key: $GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
      "contents": [{
        "parts":[
            {
              "inline_data": {
                "mime_type":"'"$MIME_TYPE"'",
                "data": "'"$IMAGE_B64"'"
              }
            },
            {"text": "Zoom into the expression pedals and tell me how many pedals are there?"}
        ]
      }],
      "tools": [{"code_execution": {}}]
    }'

如要進一步瞭解如何使用圖片執行程式碼,請參閱「程式碼執行」。

多模態函式回應

多模態函式呼叫 可讓使用者取得含有多模態物件的函式回應, 進而更充分運用模型的函式呼叫功能。標準函式呼叫功能僅支援以文字為基礎的函式回應:

Python

from google import genai
from google.genai import types

import requests

client = genai.Client()

# This is a manual, two turn multimodal function calling workflow:

# 1. Define the function tool
get_image_declaration = types.FunctionDeclaration(
  name="get_image",
  description="Retrieves the image file reference for a specific order item.",
  parameters={
      "type": "object",
      "properties": {
          "item_name": {
              "type": "string",
              "description": "The name or description of the item ordered (e.g., 'instrument')."
          }
      },
      "required": ["item_name"],
  },
)
tool_config = types.Tool(function_declarations=[get_image_declaration])

# 2. Send a message that triggers the tool
prompt = "Show me the instrument I ordered last month."
response_1 = client.models.generate_content(
  model="gemini-3-flash-preview",
  contents=[prompt],
  config=types.GenerateContentConfig(
      tools=[tool_config],
  )
)

# 3. Handle the function call
function_call = response_1.function_calls[0]
requested_item = function_call.args["item_name"]
print(f"Model wants to call: {function_call.name}")

# Execute your tool (e.g., call an API)
# (This is a mock response for the example)
print(f"Calling external tool for: {requested_item}")

function_response_data = {
  "image_ref": {"$ref": "instrument.jpg"},
}
image_path = "https://goo.gle/instrument-img"
image_bytes = requests.get(image_path).content
function_response_multimodal_data = types.FunctionResponsePart(
  inline_data=types.FunctionResponseBlob(
    mime_type="image/jpeg",
    display_name="instrument.jpg",
    data=image_bytes,
  )
)

# 4. Send the tool's result back
# Append this turn's messages to history for a final response.
history = [
  types.Content(role="user", parts=[types.Part(text=prompt)]),
  response_1.candidates[0].content,
  types.Content(
    role="tool",
    parts=[
        types.Part.from_function_response(
          name=function_call.name,
          response=function_response_data,
          parts=[function_response_multimodal_data]
        )
    ],
  )
]

response_2 = client.models.generate_content(
  model="gemini-3-flash-preview",
  contents=history,
  config=types.GenerateContentConfig(
      tools=[tool_config],
      thinking_config=types.ThinkingConfig(include_thoughts=True)
  ),
)

print(f"\nFinal model response: {response_2.text}")

JavaScript

import { GoogleGenAI, Type } from '@google/genai';

const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

// This is a manual, two turn multimodal function calling workflow:
// 1. Define the function tool
const getImageDeclaration = {
  name: 'get_image',
  description: 'Retrieves the image file reference for a specific order item.',
  parameters: {
    type: Type.OBJECT,
    properties: {
      item_name: {
        type: Type.STRING,
        description: "The name or description of the item ordered (e.g., 'instrument').",
      },
    },
    required: ['item_name'],
  },
};

const toolConfig = {
  functionDeclarations: [getImageDeclaration],
};

// 2. Send a message that triggers the tool
const prompt = 'Show me the instrument I ordered last month.';
const response1 = await client.models.generateContent({
  model: 'gemini-3-flash-preview',
  contents: prompt,
  config: {
    tools: [toolConfig],
  },
});

// 3. Handle the function call
const functionCall = response1.functionCalls[0];
const requestedItem = functionCall.args.item_name;
console.log(`Model wants to call: ${functionCall.name}`);

// Execute your tool (e.g., call an API)
// (This is a mock response for the example)
console.log(`Calling external tool for: ${requestedItem}`);

const functionResponseData = {
  image_ref: { $ref: 'instrument.jpg' },
};

const imageUrl = "https://goo.gle/instrument-img";
const response = await fetch(imageUrl);
const imageArrayBuffer = await response.arrayBuffer();
const base64ImageData = Buffer.from(imageArrayBuffer).toString('base64');

const functionResponseMultimodalData = {
  inlineData: {
    mimeType: 'image/jpeg',
    displayName: 'instrument.jpg',
    data: base64ImageData,
  },
};

// 4. Send the tool's result back
// Append this turn's messages to history for a final response.
const history = [
  { role: 'user', parts: [{ text: prompt }] },
  response1.candidates[0].content,
  {
    role: 'tool',
    parts: [
      {
        functionResponse: {
          name: functionCall.name,
          response: functionResponseData,
          parts: [functionResponseMultimodalData],
        },
      },
    ],
  },
];

const response2 = await client.models.generateContent({
  model: 'gemini-3-flash-preview',
  contents: history,
  config: {
    tools: [toolConfig],
    thinkingConfig: { includeThoughts: true },
  },
});

console.log(`\nFinal model response: ${response2.text}`);

REST

IMG_URL="https://goo.gle/instrument-img"

MIME_TYPE=$(curl -sIL "$IMG_URL" | grep -i '^content-type:' | awk -F ': ' '{print $2}' | sed 's/\r$//' | head -n 1)
if [[ -z "$MIME_TYPE" || ! "$MIME_TYPE" == image/* ]]; then
  MIME_TYPE="image/jpeg"
fi

# Check for macOS
if [[ "$(uname)" == "Darwin" ]]; then
  IMAGE_B64=$(curl -sL "$IMG_URL" | base64 -b 0)
elif [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
  IMAGE_B64=$(curl -sL "$IMG_URL" | base64)
else
  IMAGE_B64=$(curl -sL "$IMG_URL" | base64 -w0)
fi

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H 'Content-Type: application/json' \
  -X POST \
  -d '{
    "contents": [
      ...,
      {
        "role": "user",
        "parts": [
        {
            "functionResponse": {
              "name": "get_image",
              "response": {
                "image_ref": {
                  "$ref": "instrument.jpg"
                }
              },
              "parts": [
                {
                  "inlineData": {
                    "displayName": "instrument.jpg",
                    "mimeType":"'"$MIME_TYPE"'",
                    "data": "'"$IMAGE_B64"'"
                  }
                }
              ]
            }
          }
        ]
      }
    ]
  }'

從 Gemini 2.5 遷移

Gemini 3 是我們迄今最強大的模型系列,相較於 Gemini 2.5,各方面都有顯著提升。遷移時,請注意以下事項:

  • 思考型:如果你先前使用複雜的提示工程 (例如思緒鏈) 強迫 Gemini 2.5 推理,請試用 Gemini 3 和 thinking_level: "high",並簡化提示。
  • 溫度設定:如果現有程式碼明確設定溫度 (尤其是將溫度設為低值,以產生確定性輸出內容),建議您移除這個參數,並使用 Gemini 3 的預設值 1.0,以免發生潛在的迴圈問題,或導致複雜工作效能下降。
  • PDF 和文件理解: 如果您依賴特定行為來剖析密集文件,請測試新的 media_resolution_high 設定,確保準確度不受影響。
  • 符記用量:改用 Gemini 3 預設模型後,PDF 的符記用量可能會增加,但影片的符記用量會減少。如果預設解析度提高,導致要求超出脈絡窗口,建議您明確降低媒體解析度。
  • 圖片分割:Gemini 3 Pro 或 Gemini 3 Flash 不支援圖片分割功能 (傳回物件的像素層級遮罩)。如果工作負載需要原生圖像分割功能,建議繼續使用 Gemini 2.5 Flash (關閉思考功能) 或 Gemini Robotics-ER 1.5
  • 電腦用途:Gemini 3 Pro 和 Gemini 3 Flash 支援電腦用途。與 2.5 系列不同,您不需要使用其他模型來存取電腦使用工具。
  • 工具支援:Gemini 3 模型目前不支援地圖基礎功能,因此不會遷移。此外,目前還無法將內建工具與函式呼叫功能搭配使用。

OpenAI 相容性

如果使用者採用 OpenAI 相容層,系統會自動將標準參數 (OpenAI 的 reasoning_effort) 對應至 Gemini (thinking_level) 的對等項目。

提示最佳做法

Gemini 3 是推論模型,因此提示方式有所不同。

  • 明確的指令:輸入提示時請簡潔扼要。Gemini 3 最能根據直接明確的指令回覆。如果使用舊版模型,系統可能會過度分析冗長或過於複雜的提示工程技術。
  • 輸出內容詳細程度:Gemini 3 預設會提供較簡潔的回覆,並偏好直接提供有效率的答案。如果您的用途需要更具對話感或「健談」的風格,您必須在提示中明確引導模型 (例如「以親切健談的助理身分說明這件事」。
  • 脈絡管理:處理大型資料集 (例如整本書、程式碼庫或長篇影片) 時,請將具體指令或問題放在提示結尾的資料脈絡之後。在問題開頭使用「根據上述資訊...」等詞組,讓模型根據提供的資料進行推論。

如要進一步瞭解提示設計策略,請參閱提示工程指南

常見問題

  1. Gemini 3 的知識截止日期為何?Gemini 3 模型可提供 2025 年 1 月以前的資訊。如要取得最新資訊,請使用搜尋基礎工具。

  2. 脈絡窗口的限制為何?Gemini 3 模型支援 100 萬個詞元的輸入脈絡窗口,以及最多 6 萬 4 千個詞元的輸出。

  3. Gemini 3 是否提供免費方案?Gemini 3 Flash gemini-3-flash-preview 在 Gemini API 中提供免費方案。您可以在 Google AI Studio 免費試用 Gemini 3、3.1 Pro 和 3 Flash,但 Gemini API 中的 gemini-3-pro-previewgemini-3.1-pro-preview 沒有免費方案。

  4. 舊的 thinking_budget 程式碼是否仍可運作?可以,thinking_budget 仍支援回溯相容性,但建議遷移至 thinking_level,以獲得更可預測的成效。請勿在同一項要求中同時使用這兩者。

  5. Gemini 3 是否支援 Batch API?可以,Gemini 3 支援 Batch API

  6. 是否支援脈絡快取?是,Gemini 3 支援脈絡快取

  7. Gemini 3 支援哪些工具?Gemini 3 支援 Google 搜尋檔案搜尋程式碼執行網址脈絡。此外,這項功能也支援標準的函式呼叫,可搭配自訂工具使用 (但無法搭配內建工具)。請注意,目前不支援使用 Google 地圖進行基礎搜尋

  8. 什麼是 gemini-3.1-pro-preview-customtools如果您使用 gemini-3.1-pro-preview,但模型忽略自訂工具,改用 Bash 指令,請改用 gemini-3.1-pro-preview-customtools 模型。詳情請參閱這篇文章

後續步驟

  • 開始使用 Gemini 3 食譜
  • 請參閱專屬的 Cookbook 指南,瞭解思考層級,以及如何從思考預算遷移至思考層級。