定義多個工具

Designing Agentic Systems with LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

整合多個工具

具有多個工具的機器人

   

  • 新增多個自訂工具

  • 依查詢自動選工具

Designing Agentic Systems with LangChain

強化教育聊天機器人

坐在大書本上閱讀的人們。

 

  • 查詢歷史事件

  • 檢查回文:

     
    • level = level

    • top spot = tops pot

Designing Agentic Systems with LangChain

打造工具的多種方式

 

  • 呼叫 LLM

    用自然語言查詢歷史日期,例如「5th of November」
      Agent: The 5th of November is famous for the Gunpowder treason and 
      plot...
    
  • Python 程式碼

    直接比較字串以找回文,例如 string == string[::-1]
      Agent: Yes, "madam" is a palindrome...
    
Designing Agentic Systems with LangChain

歷史事件工具

# 使用裝飾器標記工具,並將輸入格式設為字串
@tool

def date_checker(date: str) -> str:
"""Provide a list of important historical events for a given date in any format."""
try: # 呼叫 LLM 解析日期並產生歷史事件 answer = llm.invoke(f"List important historical events that occurred on {date}.")
# 回傳回應 return answer.content
# 針對擷取錯誤設定例外處理 except Exception as e: return f"Error retrieving events: {str(e)}"
Designing Agentic Systems with LangChain

回文工具

@tool

# 將輸入格式設為字串 def check_palindrome(text: str):
"""Check if a word or phrase is a palindrome."""
# 去除非英數字元並轉為小寫 cleaned = ''.join(char.lower() for char in text if char.isalnum())
# 檢查反轉後是否與原字串相同 if cleaned == cleaned[::-1]: return f"The phrase or word '{text}' is a palindrome." else: return f"The phrase or word '{text}' is not a palindrome."
Designing Agentic Systems with LangChain

綁定多個工具

# 匯入定義工具節點所需的模組
from langgraph.prebuilt import ToolNode


# 工具清單 tools = [wikipedia_tool, date_checker, check_palindrome]
# 將工具傳入 ToolNode() tool_node = ToolNode(tools)
# 將工具綁定到 LLM model_with_tools = llm.bind_tools(tools)
Designing Agentic Systems with LangChain

一起來練習吧!

Designing Agentic Systems with LangChain

Preparing Video For Download...