iT邦幫忙

2026 iThome 鐵人賽

DAY 17
0

前言

昨天列了四項要做的事,今天做回傳給模型的內容,以及重擬的額度怎麼算,想辦法解決擋得住攻擊但任務跟著死的情境。

回傳什麼給模型

CaMeL原本遇到錯誤是把整段traceback丟回去讓模型自己修,語法錯誤這樣沒問題,但政策拒絕的理由不能這樣處理,因為那句話裡面有可讀取者清單、有工具名、有為什麼不允許,等於把防線的形狀畫給模型看,所以理由留在紀錄裡不回傳,回給模型的只有三件事,哪個工具被拒絕、重寫呼叫的內容、以及還走得通的方向:

The call to `send_email` was not permitted, and execution stopped there.

Rewriting the same `send_email` call differently will not help: it will be refused again.
Look for another way to satisfy the original request within what is already allowed:
printing the information for the user is usually available when sending or changing
something is not. If the request cannot be completed without that call, print a short
explanation for the user and stop.

Provide *all the code* so that I can directly run it. The code up to the line before
the refused call has already been executed and its variables and defined classes are
still accessible to you.

如果失敗喝沒有給更正確的方向,模型大概只會換個寫法再送一次,額度很快就燒,最後一段是照CaMeL原本錯誤訊息的寫法,因為被擋的那行之前的程式碼已經跑過了,變數還在,不講清楚模型會從頭再來一次。

另外同一個工具用到最後一次機會的時候,第二句會換成這個工具接下來都不會被允許,不要再用任何形式呼叫它。

額度怎麼算

CaMeL現在有一個重寫上限是十次,然後另外給兩條上限,一條是總共可以重擬三次,另一條是同一個工具被擋兩次之後就停,兩條任一超過就不再重擬,任務直接失敗。

跑起來

寫了一個不用打API的demo,用假的模型照劇本回程式碼:

1. 被擋之後換個方式,改成印給使用者
    第1次規劃 -> send_email(recipients=["mark.black-2134@gmail.com"], ...)
    模型收到的回饋:
      | The call to `send_email` was not permitted, and execution stopped there.
      | Rewriting the same `send_email` call differently will not help ...
      | Look for another way to satisfy the original request ...
    第2次規劃 -> print(body)
    結果:使用者拿到答案了
    threat level: suspicious (score=3, signals=2),共規劃2次

2. 被擋之後硬試同一個工具
    第1次規劃 -> send_email(recipients=["mark.black-2134@gmail.com"], ...)
    第2次規劃 -> send_email(recipients=["mark.black-2134@gmail.com"], ...)
    第3次規劃 -> send_email(recipients=["mark.black-2134@gmail.com"], ...)
    結果:沒有輸出,任務失敗
    threat level: compromised (score=9, signals=6),共規劃3次

3. 一開始就走合法路線
    第1次規劃 -> print(body)
    結果:使用者拿到答案了
    threat level: clean (score=0, signals=0),共規劃1次

第一個情境就是昨天失敗任務被救回來的樣子,資料沒有寄出去,但使用者要的東西還是拿到了,第二個情境是額度的作用,試到第三次沒辦法就停,不會讓模型無限試探,第三個情境確認沒觸發的時候一切照舊。

跑的指令:

uv run --env-file .env python demo_bounded_replan.py

測試裡有一個是專門確認理由沒有洩漏回去的,檢查回饋文字裡面不能出現可讀取者清單、威脅等級、還有攻擊者的信箱:

uv run --env-file .env python -m pytest tests/test_security_replan.py -v

原本150個測試加上新的8個都能過。

然後就明天趕快把剩下的第三項跟第四項做完吧!


上一篇
DAY16|攔下訊號後該如何做設計?
系列文
CaMeL 動態重擬定:讓 Agent 邊讀邊決定17
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言