定义多个工具

使用 LangChain 设计 Agentic 系统

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

集成多个工具

配备多种工具的机器人

   

  • 添加多个自定义工具

  • 按查询自动选择工具

使用 LangChain 设计 Agentic 系统

增强教育聊天机器人

人们坐在巨大的书上阅读。

 

  • 查询历史事件

  • 检查回文:

     
    • level = level

    • top spot = tops pot

使用 LangChain 设计 Agentic 系统

构建工具的多种方式

 

  • 调用 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...
    
使用 LangChain 设计 Agentic 系统

历史事件工具

# 使用装饰器标注工具并将输入格式设为字符串
@tool

def date_checker(date: str) -> str:
"""为任意格式的日期提供重要历史事件列表。"""
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)}"
使用 LangChain 设计 Agentic 系统

回工具

@tool

# 将输入格式设为字符串 def check_palindrome(text: str):
"""检查单词或短语是否为回文。"""
# 去除非字母数字字符并转为小写 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."
使用 LangChain 设计 Agentic 系统

绑定多个工具

# 导入定义工具节点所需模块
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)
使用 LangChain 设计 Agentic 系统

Ayo berlatih!

使用 LangChain 设计 Agentic 系统

Preparing Video For Download...