Designing Agentic Systems with LangChain
Dilini K. Sumanapala, PhD
Founder & AI Engineer, Genverv, Ltd.


Agent: The 5th of November is famous for the Gunpowder treason and
plot...
string == string[::-1] Agent: Yes, "madam" is a palindrome...
# 使用裝飾器標記工具,並將輸入格式設為字串 @tooldef 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)}"
@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."
# 匯入定義工具節點所需的模組 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