langchain tool - how to create a lang chain tool
There are 3 options to create lang chain tool. The first option is using
1. @tool decorator. Let's say we are creating a simple tool and all we need to do is use tool decorator like this
from langchain_core.tools import tool
from pydantic import BaseModel, Field
class CalculatorInput(BaseModel):
a: int = Field(description="first number")
b: int = Field(description="second number")
@tool("multiplication tool", args_schema=CalculatorInput)
def multiply(a: int, b: int) -> int:
"""Multiply two numbers."""
return a * b
Then you can start binding the agent to this tool:
from cal import multiply
tools = [multiply]
model_with_tools = model.bind_tools(tools)
response = model_with_tools.invoke([HumanMessage(content="multiply 2 * 2")])
print(f"ContentString: {response.content}")
print(f"ToolCalls: {response.tool_calls}")
To get the agent to do its work
from langgraph.prebuilt import create_react_agent
agent_executor = create_react_agent(model, tools)
response = agent_executor.invoke(
{"messages": [HumanMessage(content="multiply 2 with 2")]}
)
Comments