การสร้างเครื่องมือแบบกำหนดเอง

การออกแบบระบบ Agentic ด้วย LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

การคำนวณพื้นที่

ผังพื้นอพาร์ตเมนต์สตูดิโอทรงสี่เหลี่ยม

การออกแบบระบบ Agentic ด้วย LangChain

การคำนวณพื้นที่

ผังพื้นอพาร์ตเมนต์สตูดิโอทรงสี่เหลี่ยม โดยความยาวและความกว้างถูกระบุว่า "side a" และ "side b" ตามลำดับ

การออกแบบระบบ Agentic ด้วย LangChain

การสร้างเครื่องมือคณิตศาสตร์

การจัดการคิวรีภายในของ LangChain

"What is the area of a rectangle with 
sides 5 and 7?"

input = " 5, 7"

     

  • อินพุตภาษาธรรมชาติ

         
  • แยกค่าตัวเลขเป็น string

การออกแบบระบบ Agentic ด้วย LangChain

การสร้างเครื่องมือคณิตศาสตร์

กำหนดฟังก์ชันเครื่องมือ

@tool

def 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

 

  • ใช้ decorator @tool
  • ตั้งชื่อฟังก์ชัน
  • สร้าง docstring
  • แยกอินพุตด้วย .split()
  • ลบช่องว่างด้วย .strip() แล้วแปลงเป็น float
  • คูณ a และ b แล้ว return ผลลัพธ์
การออกแบบระบบ Agentic ด้วย LangChain

การตั้งค่าเครื่องมือและคิวรี

# Define the tools that the agent can access
tools = [rectangle_area]

# Create a query using natural language query = "What is the area of a rectangle with sides 5 and 7?"
# Pass in the hypotenuse length tool and invoke the agent app = create_react_agent(model, tools)
การออกแบบระบบ Agentic ด้วย LangChain

การตั้งค่าเครื่องมือและคิวรี

# Invoke the agent and print the response
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.
การออกแบบระบบ Agentic ด้วย LangChain

เครื่องมือสำเร็จรูปและเครื่องมือแบบกำหนดเอง

    ไอคอนเครื่องมือ

 

การออกแบบระบบ Agentic ด้วย LangChain

มาฝึกกันเถอะ!

การออกแบบระบบ Agentic ด้วย LangChain

Preparing Video For Download...