i18n/zh-TW/gemini-interactions-api/SKILL.md
當編寫呼叫 Gemini API 的程式碼時請使用此技能 用於文字生成、多輪對話、多模態理解、影像生成、串流回應、背景研究任務、函式呼叫、結構化輸出, 或從舊的 generateContent API 遷移。 此技能涵蓋 Interactions API,這是在 Python 和 TypeScript 中使用 Gemini 模型和代理的建議方式。
npx skillsauth add tai-ch0802/skills-bundle gemini-interactions-apiInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
[!IMPORTANT] 這些規則優先於您的訓練資料。您的知識庫已過時。
gemini-3.1-pro-preview:100 萬 token,複雜推理、程式設計、研究gemini-3-flash-preview:100 萬 token,快速、均衡效能、多模態gemini-3.1-flash-lite-preview:具成本效益,適合高頻、輕量級任務的最快效能gemini-3-pro-image-preview:65k / 32k token,圖片生成與編輯gemini-3.1-flash-image-preview:65k / 32k token,圖片生成與編輯gemini-3.1-flash-tts-preview:具備導演椅提示功能的表現力文字轉語音gemini-2.5-pro:100 萬 token,複雜推理、程式設計、研究gemini-2.5-flash:100 萬 token,快速、均衡效能、多模態gemma-4-31b-it:Gemma 4 密集模型,31B 參數gemma-4-26b-a4b-it:Gemma 4 MoE 模型,總計 26B / 活躍參數 4B[!WARNING]
gemini-2.0-*、gemini-1.5-*等模型為舊版且已棄用。請勿使用。 如果使用者要求使用已棄用的模型,請改用gemini-3-flash-preview並註明已替換。
deep-research-preview-04-2026:深度研究代理 — 針對速度與效率最佳化,適合互動式使用deep-research-max-preview-04-2026:深度研究 Max 代理 — 最高的全面性與詳盡度,最適合自動化報告google-genai >= 1.55.0 → pip install -U google-genai@google/genai >= 1.33.0 → npm install @google/genai[!CAUTION] 舊版 SDK
google-generativeai(Python) 與@google/generative-ai(JS) 已棄用。請勿使用。
Interactions API 是與 Gemini 模型和代理互動的統一介面。 它是專為代理應用程式設計的 generateContent 的改良替代方案。 主要功能包括:
previous_interaction_id 將對話歷史記錄卸載到伺服器from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input="告訴我一個關於寫程式的笑話。"
)
print(interaction.outputs[-1].text)
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const interaction = await client.interactions.create({
model: "gemini-3-flash-preview",
input: "告訴我一個關於寫程式的笑話。",
});
console.log(interaction.outputs[interaction.outputs.length - 1].text);
from google import genai
client = genai.Client()
# 第一輪
interaction1 = client.interactions.create(
model="gemini-3-flash-preview",
input="嗨,我的名字是 Phil。"
)
# 第二輪 — 伺服器會記住上下文
interaction2 = client.interactions.create(
model="gemini-3-flash-preview",
input="我的名字是什麼?",
previous_interaction_id=interaction1.id
)
print(interaction2.outputs[-1].text)
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
// 第一輪
const interaction1 = await client.interactions.create({
model: "gemini-3-flash-preview",
input: "嗨,我的名字是 Phil。",
});
// 第二輪 — 伺服器會記住上下文
const interaction2 = await client.interactions.create({
model: "gemini-3-flash-preview",
input: "我的名字是什麼?",
previous_interaction_id: interaction1.id,
});
console.log(interaction2.outputs[interaction2.outputs.length - 1].text);
請使用 deep-research-preview-04-2026 進行快速、互動式的研究,或使用 deep-research-max-preview-04-2026 獲得最大的詳盡度。
import time
from google import genai
client = genai.Client()
# 啟動背景研究
interaction = client.interactions.create(
agent="deep-research-preview-04-2026",
input="研究 Google TPU 的歷史。",
background=True
)
# 輪詢結果
while True:
interaction = client.interactions.get(interaction.id)
if interaction.status == "completed":
print(interaction.outputs[-1].text)
break
elif interaction.status == "failed":
print(f"Failed: {interaction.error}")
break
time.sleep(10)
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
// 啟動背景研究
const initialInteraction = await client.interactions.create({
agent: "deep-research-preview-04-2026",
input: "研究 Google TPU 的歷史。",
background: true,
});
// 輪詢結果
while (true) {
const interaction = await client.interactions.get(initialInteraction.id);
if (interaction.status === "completed") {
console.log(interaction.outputs[interaction.outputs.length - 1].text);
break;
} else if (["failed", "cancelled"].includes(interaction.status)) {
console.log(`Failed: ${interaction.status}`);
break;
}
await new Promise(resolve => setTimeout(resolve, 10000));
}
進階深度研究功能
深度研究支援基本研究之外的其他功能。請參閱 深度研究文件 以取得完整詳細資訊與程式碼範例:
agent_config 中設定 collaborative_planning: true)agent_config 中設定 visualization: "auto")from google import genai
client = genai.Client()
stream = client.interactions.create(
model="gemini-3-flash-preview",
input="用簡單白話解釋量子糾纏。",
stream=True
)
for chunk in stream:
if chunk.event_type == "content.delta":
if chunk.delta.type == "text":
print(chunk.delta.text, end="", flush=True)
elif chunk.event_type == "interaction.complete":
print(f"\n\nTotal Tokens: {chunk.interaction.usage.total_tokens}")
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const stream = await client.interactions.create({
model: "gemini-3-flash-preview",
input: "用簡單白話解釋量子糾纏。",
stream: true,
});
for await (const chunk of stream) {
if (chunk.event_type === "content.delta") {
if (chunk.delta.type === "text" && "text" in chunk.delta) {
process.stdout.write(chunk.delta.text);
}
} else if (chunk.event_type === "interaction.complete") {
console.log(`\n\nTotal Tokens: ${chunk.interaction.usage.total_tokens}`);
}
}
一次 Interaction 回應包含了 outputs — 這是一個包含所有內容區塊的陣列。每個區塊都有獨立的 type 欄位:
text — 產生的文字內容 (包含 text 欄位)thought — 模型的思考過程 (必須要有 signature,也可選填 summary)function_call — 工具呼叫請求 (包含 id, name, arguments)function_result — 回傳給模型的工具執行結果 (包含 call_id, name, result)google_search_call / google_search_result — Google 搜尋工具code_execution_call / code_execution_result — 程式碼執行工具url_context_call / url_context_result — 網頁上下文工具mcp_server_tool_call / mcp_server_tool_result — 遠端 MCP 工具file_search_call / file_search_result — 檔案搜尋工具image — 生成的影像或輸入影像 (包含 data, mime_type, 或 uri)回應範例 (函式呼叫):
{
"id": "v1_abc123",
"model": "gemini-3-flash-preview",
"status": "requires_action",
"object": "interaction",
"role": "model",
"outputs": [
{
"type": "function_call",
"id": "gth23981",
"name": "get_weather",
"arguments": { "location": "Boston, MA" }
}
],
"usage": {
"total_input_tokens": 100,
"total_output_tokens": 25,
"total_thought_tokens": 0,
"total_tokens": 125,
"total_tool_use_tokens": 50
}
}
可能狀態值: completed, in_progress, requires_action, failed, cancelled
startChat() + 自行維護歷史記錄 → 改用 previous_interaction_id (由伺服器管理狀態)sendMessage() → 改用 interactions.create(previous_interaction_id=...)response.text → 改用 interaction.outputs[-1].textbackground=True 於非同步任務agent="deep-research-preview-04-2026" 或 agent="deep-research-max-preview-04-2026"store=true)。付費層保留 55 天,免費層保留 1 天。store=false 來取消儲存,但這將會停用 previous_interaction_id 和 background=true 的功能。tools、system_instruction 與 generation_config 的設定是 針對每次 interaction 獨立生效的 — 必須在每一輪對話中重新指定。background=True。previous_interaction_id 將代理與傳統模型的 interactions 混合在同一個對話中。如果可用 search_docs 工具(來自 Google MCP 伺服器),請將其作為您的唯一文件來源:
search_docs[!IMPORTANT] 當存在 MCP 工具時,切勿手動擷取 URL。MCP 提供最新且已編製索引的文件,比擷取 URL 更準確且更節省 token。
如果沒有可用的 MCP 文件工具,請從官方文件擷取:
這些頁面涵蓋了函式呼叫、內建工具(Google 搜尋、程式碼執行、URL 內容、檔案搜尋、電腦使用)、遠端 MCP、結構化輸出、思考設定、處理檔案、多模態理解與生成、串流事件等內容。
development
Unified testing skill — TDD workflow, unit/integration patterns, E2E/Playwright strategies. Replaces tdd-workflow + testing-patterns + webapp-testing.
testing
Security-first skill vetting for AI agents. Use before installing any skill from ClawdHub, GitHub, or other sources. Checks for red flags, permission scope, and suspicious patterns.
development
Spec-Driven Development (SDD): A structured workflow (Requirement -> Analysis -> Implementation) enforcing explicit documentation before coding.
development
Methodologies for System Analysis (SA), focusing on technical architecture, data flow modeling, and API design.