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


LangChain 的內部查詢處理
"What is the area of a rectangle with sides 5 and 7?"input = " 5, 7"
定義你的工具函式
@tooldef rectangle_area(input: str) -> float:"""Calculates the area of a rectangle given the lengths of sides a and b."""sides = input.split(',')a = float(sides[0].strip()) b = float(sides[1].strip())return a * b
@tool 裝飾器.split() 分割輸入.strip() 去除空白並轉為 floata 與 b 相乘並回傳結果# 定義代理可使用的工具 tools = [rectangle_area]# 以自然語言建立查詢 query = "What is the area of a rectangle with sides 5 and 7?"# 傳入工具並啟動代理 app = create_react_agent(model, tools)
# 呼叫代理並印出回應
response = app.invoke({"messages": [("human", query)]})
print(response['messages'][-1].content)
The area of the rectangle with sides 5 and 7 is 35 square units.
![]()
Designing Agentic Systems with LangChain