iT邦幫忙

2026 iThome 鐵人賽

DAY 14
0
自我挑戰組

和AI學習gcp - 建立對話代理系列 第 15

Day15 Conversational Agent 架構設計

  • 分享至 

  • xImage
  •  

今天將使用 Google Cloud CX Agent Studio 建立對話系統,並透過 BFF API 方式整合,而非直接部署給終端使用者。

多層級 Agent 架構

— root agent
|-general agent
|-security agent

意圖分配邏輯

  • OWASP、資安相關查詢 → Security Agent
  • 其他一般性查詢 → General Agent
  • Root Agent 負責判斷並路由請求

🔄 整合方式

Client → BFF → CX Agent (API 模式)

  • ✅ 不直接將 Agent 部署給終端使用者(Web Widget)
  • ✅ 透過 BFF 的 /query endpoint 呼叫 CX Agent Studio API
  • ✅ 使用 runSession 或 BidiRunSession 進行對話管理
  • ✅ BFF 負責身分驗證、Session 管理與回應處理

agent的意識分流 和general agent的回答主要就來自gemini 的fast model, 而security agent則是透過抓下OWASP的內容在gcp建立data store當作知識庫

另外CA的設定會是在gcp的console上進行不會像之前的設定有tf檔可以留下設定, 考量到lab環境會定期被重置, 我會定期把console export產生的 .zip 檔備份到repo中 重置後再import

本日實作

建立agent 與agnet簡介

建立root agent進行分流 分流到sub agent

透過preview快速測試agent運作狀況

收費

pricing doc

文字來說 一個session可以長達30分鐘 或是 50個turn 超出時間或turn則是新的session計費

在測試階段的preview也會收費 ‘start a new session’ 開新對話就會開啟新的session計費

文字一個session 0.5USD

https://ithelp.ithome.com.tw/upload/images/20260816/20154359bP0pzCzV8G.png

每個billing帳戶有600美元的免費額度 啟用後一年有效 不重置

所以多個開發者開發也是共享這600美元額度

建立OWASP agent (Security agent)

v1. 使用LLM回答

建立agent, 在instruction讓agent回覆owasp相關問題
此時答案來自LLM理解的答案 可能不是最新的

https://ithelp.ithome.com.tw/upload/images/20260816/20154359JIt0LPWLAW.png

截圖可見回答來自LLM

v2. 知識來源改為網站 與搜尋

事先準備好indexing的owasp網站內容 當作資料來源
google search當作備援來源
搜尋結果再透過LLM整合回傳

website indexing 的失敗嘗試

設定owasp top10的markdown為data store

^ 先在gcp設定好上方的data stroe, 並在agent studio新增為tool

串聯tool到agent, 在security agent add tool

https://ithelp.ithome.com.tw/upload/images/20260816/20154359Cim2Tz4ga7.png

選擇tool

https://ithelp.ithome.com.tw/upload/images/20260816/20154359tNxsTp3jYc.png

tool新增到agent中了

https://ithelp.ithome.com.tw/upload/images/20260816/20154359VyHKdseUez.png

把使用tool的提示詞加到instruction

<tool_usage>
1. 當使用者詢問任何關於「OWASP Top 10」、「2025 排名」、「漏洞防禦建議」或「特定 A01-A10 類別」時,你「必須」優先呼叫 {@TOOL: search_owasp_2025}。
2. 嚴禁在未執行工具的情況下,憑空回答 2025 年的具體變動。
3. 回答時請標註:『根據您提供的 OWASP 2025 官方文件清單...』。
</tool_usage>

加入後點 ‘structure’ 會自動更新 instruction

https://ithelp.ithome.com.tw/upload/images/20260816/20154359rJ9HB9JKIC.png

使用preview測試agent, 在詢問相關的問題 可以看到tool被使用

https://ithelp.ithome.com.tw/upload/images/20260816/20154359YZazAIxFvr.png

不過要觸發tool 看起來constraint中的提示詞很重要 目前是一定要有 ‘2025 owasp top10’ 才會觸發使用tool, 不然又會跳回LLM查詢

沒觸發tool, 使用LLM

https://ithelp.ithome.com.tw/upload/images/20260816/201543597yeQbiGCVF.png

有觸發 根據文件回答

https://ithelp.ithome.com.tw/upload/images/20260816/20154359vx9cOXfAzw.png

best practice doc

  • 由簡入繁:先從簡單用例著手,待運作穩定後再逐步建構複雜功能。
  • 指令具體化:確保指令明確且無歧義,並按主題分組以利人類與模型理解。
  • 結構化指令:將編寫好的指令轉換為 XML 格式,能顯著提升代理程式的可靠性。
  • Python 工具封裝:使用 Python 封裝外部 API 以過濾無關參數,提供模型更精簡的上下文。
  • 優化工具鏈接:避免要求模型按順序調用多個工具,應改用一個 Python 工具統一發起調用,以減少幻覺。
  • 規範化工具定義:工具名稱應具區隔性,參數採蛇形命名並維持扁平化結構。
  • 頻繁建立版本:每 10-15 次重大更改後即儲存不可變的版本快照,方便隨時回滾。
  • 降低感知延遲:利用靜態訊息或設定 partial = True 提供即時回應,消除模型處理時的等待感。
  • 強制內容檢查:透過回調函數 (Callback) 確保回應中包含法律聲明等必要資訊。
  • 完善錯誤處理:針對工具失敗設定自動轉接或優雅終止,防止會話陷入死迴圈。
  • 自動化變數傳遞:使用 x-ces-session-context 傳遞 Session ID 等數據,而非依賴模型手動記憶。

設定API access

這個專案目標不是把cx agent studio的agent 直接發佈給客戶互動

而是透過我們在gcp的服務(BFF)作為橋樑 來與agent互動

因此要設定API access

https://ithelp.ithome.com.tw/upload/images/20260816/20154359YmwQAIgaPI.png

命名, 選擇agent version(對agent的版控) 就可以建立channel

https://ithelp.ithome.com.tw/upload/images/20260816/20154359e1m9D0rASF.png

如此就會獲得channel的deployment id 與 test command, 這些資料之後還可以叫出來

接著把deployment id加入專案中

更新bff/main.py 把來自 /query的請求 丟往agent

https://ithelp.ithome.com.tw/upload/images/20260816/20154359Qfs2qp0Rdb.png

這麼一來 我們暫時的串聯就完成
client ↔ bff ↔ agent 的溝通, 不過當前 client, bff是都跑在local的環境

api access doc


上一篇
Day 14 Firebase Auth與身份驗證整合
下一篇
Day 16 BFF整合agent API, 實現多輪對話
系列文
和AI學習gcp - 建立對話代理17
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言