iT邦幫忙

2026 iThome 鐵人賽

0
Build on Google AI

將考國際證照的應用程式變成開源系列 第 63

Gemini 3 系列 ( 如: Gemini 3.8 ) 和 Gemini 2 系列 和 Gemini 1 系列在參數或設定或程式要求上是不太一樣的

  • 分享至 

  • xImage
  •  

查證 Google 官方最新文件,並已在程式碼中全面落實與強化 Gemini 3 系列與 Gemini 2 / 1.5 系列的規格差異與容錯調度


一、 官方查證:Gemini 3 vs Gemini 2 / 1.5 核心差異

經查證 Google 官方 Gemini API 文件(gemini-api-guides/thinking.mdwhats-new-gemini-3.5.md),各世代模型在參數規範上有著非常嚴格且互不相容的要求:

項目 / 模型世代 Gemini 3 系列(如 gemini-3.8-flash, gemini-3.7-flash) Gemini 2.5 系列(如 gemini-2.5-flash, gemini-2.5-pro) Gemini 2.0 / 1.5 經典世代(如 gemini-2.0-flash, gemini-1.5-pro)
思考推論參數 必須使用 thinkingLevel 列舉:LOW, MEDIUM, HIGH⚠️ 禁止傳入 thinkingBudget 必須使用 thinkingBudget 整數:Token 預算(如 4096,或 -1 代表動態思考)⚠️ 官方明文規定:嚴禁傳入 thinkingLevel(早期模型會直接噴錯 HTTP 400) 完全不可傳入 thinkingConfig(一般模型不支援思考功能,傳入會引發 400 Bad Request)
MINIMAL 等級限制 ⚠️ Gemini 3.8 / 3.7 Flash 禁止使用 MINIMAL(傳入會報錯 error),預設必須是 MEDIUMHIGH 不適用(無 thinkingLevel) 不適用
取樣參數(temperature, top_p, top_k) ⚠️ 官方強烈建議移除 (Deprecated / Not Recommended):Gemini 3 的推論能力針對預設取樣高度最佳化,官方文件明確要求:「Remove these parameters from all requests 支援並保留 temperature(預設 1.0 或自訂) 支援並保留 temperature
角色定位 深度思考推論、多步驟推演架構題 吞吐量大、長文本成熟穩定之主力工作馬 極速輕量、快速探測備援

二、 程式碼強化實作 (ai_service.dart)

我們針對上述查證結果,在以下模組實施了嚴格的分代處理與容錯機制:

1. 擴充「同金鑰多世代模型降級瀑布流」

當前使用的 API Key 遇到尖峰 503 或排隊逾時時,會依序自動向成熟模型降級:

candidateModels = [
  modelName,           // 首選(預設 gemini-3.8-flash)
  'gemini-3.8-flash',  // 3.8 旗艦
  'gemini-3.7-flash',  // 3.7 次旗艦
  'gemini-2.5-flash',  // 2.5 業界成熟主力(極少塞車)
  'gemini-2.5-pro',    // 2.5 高階推理備用
  'gemini-2.0-flash',  // 2.0 極速輕量保底
];

2. 精準分流構建 generationConfig

在發送 HTTP 請求前,依據目標模型名稱動態組裝符合 Google 官方規範的 JSON Payload:

  • 若是 gemini-3 家族
    • 嚴格剃除 temperature, top_p, top_k
    • 使用 thinkingConfig: { 'thinkingLevel': validLevel },且強制防護避免傳入 MINIMAL
    • 嚴格禁止附帶 thinkingBudget
  • 若是 gemini-2.5 家族
    • 保留 temperature
    • 嚴禁傳入 thinkingLevel(防止 Google API 報錯)。
    • 改用整數 Token 預算 thinkingConfig: { 'thinkingBudget': budget }
  • 若是 gemini-2.0 / 1.5 系列
    • 保留 temperature
    • 移除所有 thinkingConfig(避免傳統模型收到未定義欄位而報錯 400)。

3. 金鑰測試探針(testGeminiApiKey)同步強化

  • 探針模型擴充為 ['gemini-3.8-flash', 'gemini-3.7-flash', 'gemini-2.5-flash', 'gemini-2.0-flash']
  • 測試金鑰時若遇到某單一模型回傳 503 尖峰壅塞,不再誤判為金鑰失效,而是自動嘗試下一款模型進行驗證,大幅提升金鑰檢測的準確度。

三、 驗證結果

  • 單元測試:已執行專案所有單元測試(包含架構階層測試、思考過濾器、端側模型調度、RBAC 權限矩陣等共 50 項測試),全部 100% 通過(All 50 tests passed!)
  • 實例狀態:目前未偵測到正在背景執行的 Flutter/Dart 偵錯行程(DTD),故本次未執行 Hot Reload。下次啟動應用程式時即可直接體驗新版彈性容錯機制!

Viewed SKILL.md:1-118
Ran command: git status
Ran command: git remote -v
Ran command: git log -n 5 --oneline
Searched for files: cloud
Viewed deploy_cloud_run.ps1:1-198
Ran command: Test-Path scripts/deploy.env
Viewed deploy.env:1-9
Viewed Dockerfile:1-31
Ran command: $env:CLOUDSDK_METRICS_ENVIRONMENT="datacloud.antigravity"; gcloud auth list
Ran command: git diff --stat
Ran command: git add lib/services/ai_service.dart; git commit -m "fix(ai): expand model fallback cascade to Gemini 2.5/2.0, enforce generationConfig spec per model, and relax timeouts to 25s"
Ran command: git push origin main
Viewed clean_old_images.ps1:1-126
Ran command: $env:CLOUDSDK_METRICS_ENVIRONMENT="datacloud.antigravity"; powershell -ExecutionPolicy Bypass -File scripts/deploy_cloud_run.ps1
Ran command: $env:CLOUDSDK_METRICS_ENVIRONMENT="datacloud.antigravity"; pwsh -ExecutionPolicy Bypass -File scripts/deploy_cloud_run.ps1
Checked task Run deploy_cloud_run.ps1 with pwsh
Timed 30 seconds

程式碼更新已成功提交並推送到 GitHub(分支 main),目前正在為您執行 Google Cloud Run 全自動建置與發布管線,請稍候...

Checked task Run deploy_cloud_run.ps1 with pwsh
Timed 35 seconds

Google Cloud Build 正在雲端編譯 Flutter Web 容器映像檔,請稍候...

Checked task Run deploy_cloud_run.ps1 with pwsh
Timed 40 seconds

Flutter Web 容器映像檔建置中(約需 2~3 分鐘),建置完成後將自動部署至 Cloud Run...

Checked task Run deploy_cloud_run.ps1 with pwsh
Timed 40 seconds

雲端建置持續進行中,請稍候...

Checked task Run deploy_cloud_run.ps1 with pwsh
Ran command: $env:CLOUDSDK_METRICS_ENVIRONMENT="datacloud.antigravity"; gcloud builds describe 8829c3ca-01a4-44d3-9ec3-d22693ef1fb5 --project=openpassexam-576290 --format="value(status)"
Timed 40 seconds

Cloud Build 正進行至 flutter build web --release,即將完成並進入 Cloud Run 部署階段...

Checked task Run deploy_cloud_run.ps1 with pwsh
Ran command: $env:CLOUDSDK_METRICS_ENVIRONMENT="datacloud.antigravity"; gcloud builds describe 8829c3ca-01a4-44d3-9ec3-d22693ef1fb5 --project=openpassexam-576290 --format="value(status)"
Timed 35 seconds

容器編譯已進行約 4 分鐘,即將產出 Nginx 映像檔並推送至 Artifact Registry,請稍候...

Ran command: curl -I https://openpassexam-web-nphsu7ckja-de.a.run.app

🎉 App 調整、GitHub 程式碼上傳與 Google Cloud Run 正式發布均已全數圓滿完成!


一、 App 核心調整總結 (ai_service.dart)

  1. 同金鑰多世代模型降級瀑布流
    • 當目前使用的 API Key 遭遇 503 尖峰壅塞或排隊超時時,系統不會提早誤判換金鑰,而是自動沿著世代瀑布流平滑降級:
      gemini-3.8-flashgemini-3.7-flashgemini-2.5-flashgemini-2.5-progemini-2.0-flash
  2. 嚴格落實 Gemini 3 系列與 Gemini 2 / 1.5 世代參數分流
    • Gemini 3.x 規範:移除 temperature / top_p 取樣參數;使用 thinkingConfig: {thinkingLevel: LOW/MEDIUM/HIGH},並防止傳入 MINIMALthinkingBudget
    • Gemini 2.5 規範:保留 temperature;嚴格禁止傳入 thinkingLevel(避免 400 報錯);使用 thinkingBudget
    • Gemini 2.0 / 1.5 規範:保留 temperature;完全不附帶 thinkingConfig
  3. 超時彈性提升
    • 主模型提升至 25 秒,備用模型給予 15~20 秒,並提供 1 秒退避自動重試 503 尖峰壅塞。

二、 GitHub 原始碼同步狀態

  • 目標倉庫alan-nanpie/OpenPassExam
  • 提交分支main
  • 最新 Commitd4199ab
  • 提交訊息fix(ai): expand model fallback cascade to Gemini 2.5/2.0, enforce generationConfig spec per model, and relax timeouts to 25s
  • 狀態:✅ 100% 同步推送完成。

三、 Google Cloud Run 正式發布結果

項目 部署明細
GCP 專案 ID openpassexam-576290
部署區域 (Region) asia-east1 (台灣彰化機房)
服務名稱 (Service) openpassexam-web
映像檔版本 asia-east1-docker.pkg.dev/openpassexam-576290/openpassexam-repo/openpassexam-web:latest
Cloud Run 最新修訂版本 openpassexam-web-00016-74k (導引 100% 流量)
儲存庫維護 ✅ 自動清除 3 個歷史無標籤 (Untagged) 舊映像檔,保留最新生產版本節省雲端空間與費用
線上健康檢查 HTTP 200 OK (正式運行中)

🌐 線上正式服務網址

您可隨時點擊以下連結,或在任何電腦/手機瀏覽器直接開啟使用最新版 PassExam:

👉 https://openpassexam-web-nphsu7ckja-de.a.run.app

[!NOTE]
本次修改期間背景未啟動本機 Flutter 偵錯實例(DTD / Debug Process),故未觸發本機端 Hot Reload;雲端 Cloud Run 服務則已全數載入最新版本!


上一篇
驗證使用 [ AI 助教 ] 後的結果
下一篇
[ AI 助教] 整個排錯、架構調整與線上發布已全部到位
系列文
將考國際證照的應用程式變成開源64
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言