カスタムツールの構築

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...