Créer des outils personnalisés

Concevoir des systèmes agentiques avec LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Calculer la surface

Plan rectangulaire d’un studio

Concevoir des systèmes agentiques avec LangChain

Calculer la surface

Plan rectangulaire d’un studio avec la longueur et la largeur indiquées « côté a » et « côté b » respectivement.

Concevoir des systèmes agentiques avec LangChain

Créer un outil mathématique

Traitement interne des requêtes par LangChain

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

input = " 5, 7"

     

  • Entrée en langage naturel

         
  • Extraire les valeurs numériques comme chaînes

Concevoir des systèmes agentiques avec LangChain

Créer un outil mathématique

Définir la fonction d’outil

@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

 

  • Utiliser le décorateur @tool
  • Nommer la fonction
  • Créer une docstring
  • Scinder l’entrée avec .split()
  • Supprimer les espaces avec .strip() et convertir en float
  • Multiplier a et b et renvoyer le résultat
Concevoir des systèmes agentiques avec LangChain

Configuration des outils et de la requête

# 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)
Concevoir des systèmes agentiques avec LangChain

Configuration des outils et de la requête

# 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.
Concevoir des systèmes agentiques avec LangChain

Outils préconstruits et personnalisés

    Icône d’outils

 

Concevoir des systèmes agentiques avec LangChain

Passons à la pratique !

Concevoir des systèmes agentiques avec LangChain

Preparing Video For Download...