Criando ferramentas personalizadas

Projetando Sistemas Agentes com LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Calculando metragem quadrada

Planta retangular de um estúdio

Projetando Sistemas Agentes com LangChain

Calculando metragem quadrada

Planta retangular de um estúdio com comprimento e largura marcados como "lado a" e "lado b".

Projetando Sistemas Agentes com LangChain

Criando uma ferramenta de matemática

Tratamento interno de consultas do LangChain

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

input = " 5, 7"

     

  • Entrada em linguagem natural

         
  • Extrair valores numéricos como strings

Projetando Sistemas Agentes com LangChain

Criando uma ferramenta de matemática

Defina sua função de ferramenta

@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

 

  • Use o decorador @tool
  • Dê um nome à função
  • Crie uma docstring
  • Divida a entrada com .split()
  • Remova espaços com .strip() e converta para float
  • Multiplique a e b e retorne o resultado
Projetando Sistemas Agentes com LangChain

Configuração de ferramentas e consulta

# 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)
Projetando Sistemas Agentes com LangChain

Configuração de ferramentas e consulta

# 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.
Projetando Sistemas Agentes com LangChain

Ferramentas prontas e personalizadas

    Ícone de ferramentas

 

Projetando Sistemas Agentes com LangChain

Vamos praticar!

Projetando Sistemas Agentes com LangChain

Preparing Video For Download...