Try   HackMD

AI / ML領域相關學習筆記入口頁面

Deeplearning.ai GenAI/LLM系列課程筆記

GenAI
RAG
AI Agents

原理可參考這篇綜論: How Agents for LLM Perform Task Planning。大型語言模型的代理如何進行任務規劃


Long-Term Agentic Memory With LangGraph


Baseline Email Assistant

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

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()
Response_Agent
IGNORE
NOTIFY
RESPOND
Agent Tools
Response Agent
Write Email
Schedule Meeting
Check Calendar
Start
Triage_Router Agent
End
Triage Prompt
Prompt Details
triage_system_prompt
triage_user_prompt

Define the first part of the agent - triage.

根據 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 的詳細工作流程:

    1. Start: 開始處理電子郵件。
    2. Extract Email Details: 提取電子郵件的詳細信息,包括作者、收件人、主題和郵件內容。
    3. Format System Prompt: 格式化系統提示,填充用戶的詳細信息和分類規則。
    4. Format User Prompt: 格式化用戶提示,填充郵件的詳細信息。
    5. Invoke LLM Router: 調用 LLM 路由器,傳遞系統提示和用戶提示。
    6. Classification Result: 根據 LLM 路由器的結果進行分類。
      • Respond: 如果分類為回應,則生成回應內容,更新消息並轉到回應代理。
      • Ignore: 如果分類為忽略,忽略消息並結束流程。
      • Notify: 如果分類為通知,通知消息並結束流程。
    7. End: 結束流程。

Main agent, define tools

根據程式碼段落 ## 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 流程圖:

Define Tools
Define write_email
Input: to, subject, content
Output: Email sent confirmation
Define schedule_meeting
Input: attendees, subject, duration_minutes, preferred_day
Output: Meeting scheduled confirmation
Define check_calendar_availability
Input: day
Output: Available times

#### Main agent, define tools 的詳細執行流程和參數:

  1. Define Tools: 定義工具。
  2. Define write_email: 定義 write_email 工具。
    • Input: 接受 to(收件人)、subject(主題)和 content(內容)作為輸入參數。
    • Output: 返回郵件發送確認信息。
  3. Define schedule_meeting: 定義 schedule_meeting 工具。
    • Input: 接受 attendees(參加者列表)、subject(主題)、duration_minutes(持續時間)和 preferred_day(首選日期)作為輸入參數。
    • Output: 返回會議安排確認信息。
  4. Define check_calendar_availability: 定義 check_calendar_availability 工具。
    • Input: 接受 day(日期)作為輸入參數。
    • Output: 返回可用時間信息。

prompts

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} 格式化的用戶提示,包含郵件詳細信息

Email Assistant with Semantic Memory

在Response_Agent中加入manage_memory_toolcreate_manage_memory_tool

tools= [
    write_email, 
    schedule_meeting,
    check_calendar_availability,
    manage_memory_tool,  # 新增記憶管理工具
    search_memory_tool   # 新增記憶搜尋工具
]
Response_Agent
IGNORE
NOTIFY
RESPOND
Agent Tools
Response Agent
Origin Tools
manage_memory
search_memory
Start
Triage_Router Agent
End
Triage Prompt
Prompt Details
triage_system_prompt
triage_user_prompt

create_manage_memory_tool()create_search_memory_tool()

🔹 create_manage_memory_tool()

  • 🔧 功能
    此工具的目的是將使用者的行為、互動、指示等資訊儲存在記憶系統中,使 Email Assistant 在未來的互動中可以參考這些記憶來提供更個人化的回應。`
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()

  • 🔧 功能
    此工具的目的是在記憶系統中搜尋與過往互動、郵件或行為相關的資料,幫助 Email Assistant 根據過去的資訊提供更智慧的回應。
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_promptagent_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,用於儲存使用者的背景、喜好與行為資訊。