사용자 정의 도구 만들기

LangChain으로 에이전트형 시스템 설계하기

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

면적 계산하기

원룸 아파트의 직사각형 평면도

LangChain으로 에이전트형 시스템 설계하기

면적 계산하기

길이와 너비가 각각 "side a", "side b"로 표시된 원룸의 직사각형 평면도.

LangChain으로 에이전트형 시스템 설계하기

수학 도구 만들기

LangChain의 내부 쿼리 처리

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

input = " 5, 7"

     

  • 자연어 입력

         
  • 숫자 값을 문자열로 추출

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

 

  • @tool 데코레이터 사용
  • 함수 이름 지정
  • 독스트링 작성
  • .split()으로 입력 분리
  • .strip()으로 공백 제거 후 float 변환
  • ab를 곱해 반환
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)
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.
LangChain으로 에이전트형 시스템 설계하기

사전 구축 도구와 사용자 정의 도구

    도구 아이콘

 

LangChain으로 에이전트형 시스템 설계하기

연습해 봅시다!

LangChain으로 에이전트형 시스템 설계하기

Preparing Video For Download...