原理可參考這篇綜論: How Agents for LLM Perform Task Planning。大型語言模型的代理如何進行任務規劃
from langgraph.graph import StateGraph, START, END
from langgraph.types import Command
from typing import Literal
from IPython.display import Image, display
email_agent = StateGraph(State)
email_agent = email_agent.add_node(triage_router)
email_agent = email_agent.add_node("response_agent", agent)
email_agent = email_agent.add_edge(START, "triage_router")
email_agent = email_agent.compile()
根據 Lesson 2_Baseline Email Assistant.ipynb
中的段落 ## Define the first part of the agent - triage.
,以下是詳細的執行流程和參數的 mermaid 流程圖:
triage_router
def triage_router(state: State) -> Command[
Literal["response_agent", "__end__"]
]:
author = state['email_input']['author']
to = state['email_input']['to']
subject = state['email_input']['subject']
email_thread = state['email_input']['email_thread']
system_prompt = triage_system_prompt.format(
full_name=profile["full_name"],
name=profile["name"],
user_profile_background=profile["user_profile_background"],
triage_no=prompt_instructions["triage_rules"]["ignore"],
triage_notify=prompt_instructions["triage_rules"]["notify"],
triage_email=prompt_instructions["triage_rules"]["respond"],
examples=None
)
user_prompt = triage_user_prompt.format(
author=author,
to=to,
subject=subject,
email_thread=email_thread
)
result = llm_router.invoke(
[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
]
)
if result.classification == "respond":
print("📧 Classification: RESPOND - This email requires a response")
goto = "response_agent"
update = {
"messages": [
{
"role": "user",
"content": f"Respond to the email {state['email_input']}",
}
]
}
elif result.classification == "ignore":
print("🚫 Classification: IGNORE - This email can be safely ignored")
update = None
goto = END
elif result.classification == "notify":
# If real life, this would do something else
print("🔔 Classification: NOTIFY - This email contains important information")
update = None
goto = END
else:
raise ValueError(f"Invalid classification: {result.classification}")
return Command(goto=goto, update=update)
triage_router
的詳細工作流程:
根據程式碼段落 ## Main agent, define tools
,以下是詳細的執行流程和參數的 mermaid 流程圖:
@tool
def write_email(to: str, subject: str, content: str) -> str:
"""Write and send an email."""
# 實際應用中會真的發送郵件
return f"Email sent to {to} with subject '{subject}'"
@tool
def schedule_meeting(attendees: list[str], subject: str, duration_minutes: int, preferred_day: str) -> str:
"""Schedule a calendar meeting."""
# 實際應用中會確認行事曆並安排會議
return f"Meeting '{subject}' scheduled for {preferred_day} with {len(attendees)} attendees"
@tool
def check_calendar_availability(day: str) -> str:
"""Check calendar availability for a given day."""
# 實際應用中會檢查真實的行事曆
return f"Available times on {day}: 9:00 AM, 2:00 PM, 4:00 PM"
mermaid 流程圖:
#### Main agent, define tools
的詳細執行流程和參數:
write_email
工具。
to
(收件人)、subject
(主題)和 content
(內容)作為輸入參數。schedule_meeting
工具。
attendees
(參加者列表)、subject
(主題)、duration_minutes
(持續時間)和 preferred_day
(首選日期)作為輸入參數。check_calendar_availability
工具。
day
(日期)作為輸入參數。Prompt 名稱 | 差異 | 輸入 | 輸出 |
---|---|---|---|
agent_system_prompt |
基本代理提示,包含基本工具 | {full_name} , {name} , {instructions} |
格式化的系統提示 |
triage_system_prompt |
分類郵件的提示,包含分類規則 | {full_name} , {name} , {user_profile_background} , {triage_no} , {triage_notify} , {triage_email} , {examples} |
格式化的系統提示,包含分類規則和示例 |
triage_user_prompt |
用戶提示,用於分類郵件 | {author} , {to} , {subject} , {email_thread} |
格式化的用戶提示,包含郵件詳細信息 |
在Response_Agent中加入manage_memory_tool
與create_manage_memory_tool
tools= [
write_email,
schedule_meeting,
check_calendar_availability,
manage_memory_tool, # 新增記憶管理工具
search_memory_tool # 新增記憶搜尋工具
]
create_manage_memory_tool()
與 create_search_memory_tool()
create_manage_memory_tool()
def create_manage_memory_tool():
def manage_memory(memory_type, user, data_type, content):
"""
Stores user behavior, instructions, or profile data in memory.
"""
# 記憶儲存邏輯
memory_store[memory_type][user][data_type] = content
return f"Successfully stored {data_type} in {memory_type} for {user}"
return manage_memory
create_search_memory_tool()
def create_search_memory_tool():
def search_memory(memory_type, user, data_type, query):
"""
Searches stored memory for specific information.
"""
# 搜尋邏輯
results = memory_store[memory_type][user][data_type].get(query, "No matching record found.")
return results
return search_memory
功能/特性 | create_manage_memory_tool() |
create_search_memory_tool() |
---|---|---|
目的 | 儲存使用者資訊、行為或指示 | 搜尋使用者過去的資訊 |
輸入參數 (Parameters) | memory_type , user , data_type , content |
memory_type , user , data_type , query |
輸出 (Output) | 儲存成功訊息或錯誤提示 | 搜尋結果或「無結果」提示 |
應用場景 (Use Case) | 儲存指示、行為偏好、個人化資訊 | 檢索過往郵件、過往指示或互動記錄 |
agent_system_prompt
與 agent_system_prompt_memory
的差異
# Agent prompt baseline
agent_system_prompt = """
< Role >
You are {full_name}'s executive assistant. You are a top-notch executive assistant who cares about {name} performing as well as possible.
</ Role >
< Tools >
You have access to the following tools to help manage {name}'s communications and schedule:
1. write_email(to, subject, content) - Send emails to specified recipients
2. schedule_meeting(attendees, subject, duration_minutes, preferred_day) - Schedule calendar meetings
3. check_calendar_availability(day) - Check available time slots for a given day
</ Tools >
< Instructions >
{instructions}
</ Instructions >
"""
# Agent prompt semantic memory
agent_system_prompt_memory = """
...
< Tools >
...
4. manage_memory("email_assistant", user, "collection") - Store any relevant information about contacts, actions, discussion, etc. in memory for future reference
5. manage_memory("email_assistant", user, "user_profile") - Store any relevant information about the recipient, {name}, in the user profile for future reference the current user profile is shown below
6. search_memory("email_assistant", user, "collection") - Search memory for detail from previous emails
7. manage_memory("email_assistant", user, "instructions") - Update the instructions for agent tool usage based upon the user feedback
</ Tools >
...
特點/功能 | agent_system_prompt |
agent_system_prompt_memory |
---|---|---|
額外工具 (Memory Tools) | ❌ 無額外的記憶管理工具。 | ✅ 4 個額外工具: 4. manage_memory("collection") 5. manage_memory("user_profile") 6. search_memory("collection") 7. manage_memory("instructions") |
使用者檔案 (User Profile) | ❌ 未包含 profile (用戶偏好、背景等個人化資訊)。 |
✅ 包含 profile ,用於儲存使用者的背景、喜好與行為資訊。 |