iT邦幫忙

2025 iThome 鐵人賽

DAY 24
0
生成式 AI

agent-brain: 從 0 開始打造一個 python package系列 第 24

Day 24: Todo List memory structure

  • 分享至 

  • xImage
  •  

今天來處理 Todo List 記憶結構
前幾天我們在嘗試 ReAct + Reflection 的流程,已經有一個不錯的「推理-行動-反思」循環。但光有行動和反思還不夠,我希望能讓 Agent 的目標與任務更明確、更結構化。

所以今天來設計 Todo List Memory,讓 LLM 自己管理一份「待辦清單」。

Pros?

todo list 形式記錄下來。
有幾個好處:

  • 透明:什麼完成了、什麼沒完成一目了然
  • 可追蹤:就算對話很長,也能回頭看到整個任務的進度
  • 長度:LLM 的輸出被嚴格限制在規則內,相比於 messages append list,更能控制 context 的長度

設計

這個 todo list 設計時有幾個 rule:

  1. 有一個總目標 [goal]

    • 所有任務都要圍繞在這個目標上
  2. 任務有三種狀態

    • [todo] 待辦
    • [done] 已完成(必須加上結果)
    • [cancel] 取消(必須加上原因)
  3. 狀態轉換規則 (呼叫 update 的時候)

    • todo → done → 要補結果
    • todo → cancel → 要補原因

Code

實作上我做了一個 TodoListMemory 類別,
它負責:

  • set_goal():設定任務總目標
  • update():把最近訊息丟進 LLM,讓它更新 todo list
  • dump():輸出最新的 todo list

而更新的邏輯就是靠 LLM 的系統提示詞(system prompt)限制格式,
再搭配 user prompt(把當前 todo 與歷史訊息丟進去),
最後直接用 LLM 輸出的結果覆蓋掉 _todos


Example

這邊用一個實際範例:

問題

What is the result of subtracting 8 from the reverse of when the publisher of Qst was founded?

初始

[goal]: What is the result of subtracting 8 from the reverse of when the publisher of Qst was founded?
---
[todo]: Find the founding year of the publisher of Qst magazine 
[todo]: Reverse the founding year number 
[todo]: Subtract 8 from the reversed year 
[todo]: Format and provide the final answer as required

找到創立年份後

[done]: Find the founding year of the American Radio Relay League (publisher of Qst magazine) → result: ARRL was founded in 1914 
[todo]: Reverse the founding year number 
[todo]: Subtract 8 from the reversed year 
[todo]: Format and provide the final answer as required

反轉 & 減法

[done]: Find the founding year of the American Radio Relay League (publisher of Qst magazine) → result: ARRL was founded in 1914 
[done]: Reverse the founding year number → result: 4191 
[done]: Subtract 8 from the reversed year → result: 4183 
[todo]: Format and provide the final answer as required

這樣就很清楚地看到 agent 的推理過程。


心得

在讀完昨天的 context engineering 後,在設計 todo memory 架構時,單純只想著要簡潔清楚,所以就直接用了一個 text file 來 maintain todo list。
明天跑一下實驗看一下效果。

目前 memory update() 是放在 reflection state 之後
所以等於「行動 → 反思 → 更新任務」。
感覺後面可以再試試看:如果 reasoning 沒有 action,也能自動觸發一次 todo list 更新。


上一篇
Day 23: Context Engineering (二)
系列文
agent-brain: 從 0 開始打造一個 python package24
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言