Tvorba vlastních nástrojů

Návrh agentních systémů s LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Výpočet plochy v m²

Obdélníkový půdorys garsoniéry

Návrh agentních systémů s LangChain

Výpočet plochy v m²

Obdélníkový půdorys garsoniéry s délkou a šířkou označenými „strana a“ a „strana b“.

Návrh agentních systémů s LangChain

Vytvoření matematického nástroje

Interní zpracování dotazu v LangChainu

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

input = " 5, 7"

     

  • Vstup v přirozeném jazyce

         
  • Extrakce čísel jako řetězců

Návrh agentních systémů s LangChain

Vytvoření matematického nástroje

Definujte funkci nástroje

@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

 

  • Použijte dekorátor @tool
  • Pojmenujte funkci
  • Vytvořte docstring
  • Rozdělte vstup pomocí .split()
  • Odstraňte mezery pomocí .strip() a převeďte na float
  • Vynásobte a a b a vraťte výsledek
Návrh agentních systémů s LangChain

Nastavení nástrojů a dotazu

# 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)
Návrh agentních systémů s LangChain

Nastavení nástrojů a dotazu

# 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.
Návrh agentních systémů s LangChain

Předpřipravené a vlastní nástroje

    Ikona nástrojů

 

Návrh agentních systémů s LangChain

Pojďme si to vyzkoušet!

Návrh agentních systémů s LangChain

Preparing Video For Download...