根據 Google 官方 Firebase AI Logic: Remotely change model name 技術規範,本實作將於 MockExam-CCNA 導入 Firebase Remote Config 機制,使 App 在不需重新打包送審的情況下,即可從遠端動態調整雲端 AI 模型(如升級至 gemini-2.5-flash、gemini-3.5-flash、gemini-2.5-pro 等)、備用模型以及生成參數(Temperature、Max Tokens)。
firebase_remote_config 套件,與現有 firebase_core (v4.2.1) 及 firebase_auth、firebase_database 完美相容。gemini_model_name: 主要雲端 AI 模型名稱(預設:gemini-2.5-flash)gemini_fallback_model: 備用雲端 AI 模型名稱(預設:gemini-2.5-pro)ai_temperature: 生成多樣性參數(預設:0.4)ai_max_tokens: 最大生成 Token 數(預設:2048)dependencies 中加入 firebase_remote_config: ^5.3.0。RemoteConfigService 單例類別:
initialize(): 設定預設值映射表(Defaults Map)、抓取逾時(10 秒)與最小抓取間隔。onConfigUpdated 串流,支援 Remote Config 即時動態熱更新。geminiModelName, fallbackModelName, aiTemperature, aiMaxTokens。fetchAndActivate()。getSelectedModel() 方法:
auto、default 或空值,則自動向 RemoteConfigService().geminiModelName 索取遠端最新模型名稱。generateLearnEnglishForQuestion 與 tutorChatWithKeys 中整合 Remote Config 所設定的模型、ai_temperature 與 ai_max_tokens。RemoteConfigService().fallbackModelName 進行重試。Firebase.initializeApp() 之後非同步調用 RemoteConfigService().initialize(),確保在 App 啟動階段完成參數加載。firebase remoteconfig:set 或 REST API) 一鍵發布 Remote Config 設定的 PowerShell 腳本。flutter pub get 解析依賴關係。flutter analyze lib 確保無任何 Lint 錯誤或編譯問題。flutter test 驗證既有測試皆順利通過。test/services/remote_config_service_test.dart 驗證預設值與降級邏輯。LogService。💡 這是一份專為學習者與初學者編寫的完整技術筆記。即使完全沒有程式背景,跟著這份指南也能清楚理解「如何在不停機、不重新安裝 App 的情況下,隨時更換與微調 App 內的 AI 大腦」!
想像一下您買了一台智慧電視(App):
在我們的 App 裡,這個「節目」就是 Google Gemini AI 模型(例如從 gemini-2.5-flash 升級成最新的 gemini-3.7-flash)。
gemini-3.7-flash、gemini-3.5-flash、gemini-2.5-pro 或任何未來的最新型號。gemini-2.5-pro),使用者完全不會感受到錯誤。VLAN、OSPF),系統會自動從數千頁的教材 PDF 中精準抓取對應章節進行生成。當 App 需要向 AI 發問時,它是如何決定要用哪一個模型的?我們設計了嚴謹的「優先順序金字塔」:
┌──────────────────────────────────────────────────────────┐
│ 第 1 優先(最高):本機管理員測試覆蓋 (Local Override) │ 👈 管理員在 App 內選「僅限本機測試」
├──────────────────────────────────────────────────────────┤
│ 第 2 優先:全域 RTDB 設定 (Global Broadcast) │ 👈 管理員在 App 內選「全域同步設定」
├──────────────────────────────────────────────────────────┤
│ 第 3 優先:Firebase Remote Config 雲端參數 │ 👈 Firebase 網頁後台 / 腳本發布
├──────────────────────────────────────────────────────────┤
│ 第 4 優先(最低):App 內建靜態預設值 (Built-in Default) │ 👈 離線或無網路時的最後保險 (gemini-2.5-flash)
└──────────────────────────────────────────────────────────┘
您可以選擇以下任何一種最適合您的操作方式:
這是最直覺的方式,不需要打開任何網頁後台或終端機:
gemini-3.7-flash)。gemini-2.5-pro)。8192。如果您習慣在瀏覽器中管理雲端:
gemini_model_name:主要模型名稱(預設:gemini-2.5-flash,可修改為 gemini-3.7-flash)gemini_fallback_model:備用模型名稱(預設:gemini-2.5-pro)ai_temperature:溫度(預設:0.4)ai_max_tokens:最大輸出字數(預設:8192)如果您在電腦前開發,我們已經為您寫好了一鍵自動化 PowerShell 腳本:
powershell -ExecutionPolicy Bypass -File scripts\publish_remote_config.ps1
remoteconfig.template.json 並推送到 Firebase 雲端,看到 [SUCCESS] 即代表發布完成!如果您想深入學習程式碼結構,以下是相關檔案的職責清單:
| 檔案路徑 | 語言 | 角色與職責 |
|---|---|---|
lib/services/remote_config_service.dart |
Dart | Remote Config 核心服務:負責與 Firebase 連線、設定預設值、拉取最新雲端設定、開啟即時熱更新監聽器 (onConfigUpdated)。 |
lib/services/local_cache_service.dart |
Dart | 模型決定調度員:按照「本機自訂 > 全域 RTDB > Remote Config > 內建預設」的優先順序回傳最終該採用的模型名稱。 |
lib/services/ai_generator_service.dart |
Dart | AI 呼叫核心:負責將考題、圖片、對話組合後向 Google API 發送請求,支援雙模型自動容錯重試(Primary -> Fallback)。 |
lib/screens/admin_ai_model_screen.dart |
Dart | 管理員 UI 畫面:提供視覺化下拉選單、滑桿、即時拉取測試按鈕與廣播控制開關。 |
lib/services/notebooklm_service.dart |
Dart | NotebookLM 工作區服務:負責多本 PDF 教材的切片 (Chunking)、倒排索引、智慧主題檢索 (TopK: 25) 與 Studio 5+1 大工具產出。 |
remoteconfig.template.json |
JSON | 雲端參數定義範本:定義參數名稱、資料型態、預設值與欄位說明。 |
scripts/publish_remote_config.ps1 |
PowerShell | 一鍵發布腳本:一鍵將 JSON 範本部署至 Firebase 雲端。 |
test/remote_config_service_test.dart |
Dart | 自動化單元測試:確保服務單例、預設參數、快取機制 100% 穩定無虞。 |
gemini-4.0-ultra),App 沒有預設列在選單裡怎麼辦?A:我們已經實作了 「Google 官方 API 自動聯網查詢」 機制,您有兩種超簡單的解決方式:
自動一鍵更新(推薦):
https://generativelanguage.googleapis.com/v1beta/models)發送查詢。gemini-4.0-ultra),該模型就會自動出現在下拉選單中(標註 GOOGLE 或 LATEST 標籤),點選即可使用!手動輸入自訂名稱:
gemini-4.0-ultra,點擊儲存即可立刻啟用!A:
activate()。A:不會!
AiGeneratorService 內建了自動備援機制。當主要模型回傳 429 時,系統會在後台自動改用備用模型 (gemini-2.5-pro) 重新發送請求,使用者端體驗完全無縫。筆記建立時間:2026-08-28
專案名稱:MockExam-CCNA
publish_remote_config.ps1
param (
[string]$ProjectId = "YouProjectName",
[string]$TemplatePath = "$PSScriptRoot\remote_config_template.json"
)
Write-Host "=========================================================" -ForegroundColor Cyan
Write-Host " [Firebase Remote Config] Deploying AI Parameters Template" -ForegroundColor Green
Write-Host "=========================================================" -ForegroundColor Cyan
if (-not (Test-Path $TemplatePath)) {
Write-Error "Error: Cannot find template file: $TemplatePath"
exit 1
}
Write-Host "Target Project: $ProjectId" -ForegroundColor Yellow
Write-Host "Template File: $TemplatePath" -ForegroundColor Yellow
$firebaseCli = Get-Command "firebase" -ErrorAction SilentlyContinue
if ($null -ne $firebaseCli) {
Write-Host "Firebase CLI detected. Deploying remoteconfig..." -ForegroundColor Cyan
try {
# Deploy using firebase deploy command
$rootPath = Resolve-Path "$PSScriptRoot.."
firebase deploy --only remoteconfig --project $ProjectId
if ($LASTEXITCODE -eq 0) {
Write-Host ""
Write-Host "=========================================================" -ForegroundColor Green
Write-Host " [SUCCESS] Remote Config parameters successfully deployed!" -ForegroundColor Green
Write-Host " Project Console: https://console.firebase.google.com/project/$ProjectId/config" -ForegroundColor Cyan
Write-Host "=========================================================" -ForegroundColor Green
exit 0
} else {
Write-Host "Firebase CLI returned exit code $LASTEXITCODE." -ForegroundColor Yellow
}
} catch {
Write-Host "Deployment error: $_" -ForegroundColor Yellow
}
} else {
Write-Host "Firebase CLI is not found in PATH." -ForegroundColor Gray
}
Write-Host ""
Write-Host "=========================================================" -ForegroundColor Cyan
Write-Host " Manual Firebase Console Setup Instructions:" -ForegroundColor Green
Write-Host "=========================================================" -ForegroundColor Cyan
Write-Host "1. Visit Console: https://console.firebase.google.com/project/$ProjectId/config"
Write-Host "2. Click 'Remote Config' -> Add or edit the parameters below:"
Write-Host " - gemini_model_name: gemini-2.5-flash (String)"
Write-Host " - gemini_fallback_model: gemini-2.5-pro (String)"
Write-Host " - ai_temperature: 0.4 (Number)"
Write-Host " - ai_max_tokens: 2048 (Number)"
Write-Host "3. Click 'Publish changes' to sync immediately across all apps."
Write-Host "=========================================================" -ForegroundColor Cyan
remote_config_template.json
{
"parameters": {
"gemini_model_name": {
"defaultValue": {
"value": "gemini-2.5-flash"
},
"description": "Primary Gemini model name used for AI explanations, question classification, and AI Tutor chat.",
"valueType": "STRING"
},
"gemini_fallback_model": {
"defaultValue": {
"value": "gemini-2.5-pro"
},
"description": "Secondary fallback Gemini model name if the primary model fails or encounters rate limits.",
"valueType": "STRING"
},
"ai_temperature": {
"defaultValue": {
"value": "0.4"
},
"description": "Temperature parameter for AI Tutor generation (0.0 to 1.0).",
"valueType": "NUMBER"
},
"ai_max_tokens": {
"defaultValue": {
"value": "8192"
},
"description": "Maximum tokens for AI Tutor chat and English learning generation.",
"valueType": "NUMBER"
}
}
}