From basic chat to react agent with langchain-ibm
Written by: Lukasz Cmielowski, PhD & Mateusz Szewczyk
Latest release of langchain-ibm==0.3.0
introduces support for Chat API with function calling. If we add the langgraph
to it we can start building agents with ReAct logic. In this example the goal of agent is to make simple arithmetic calculations by calling right set of tools. Let’s get started.
Setup
Install required packages and initialize the ChatWatsonx
class (use mistral-large
model).
Note: the apikey
is required — more details can be found here.
!pip install -U "langgraph>0.2,<0.3"
!pip install -U "langchain_ibm>=0.3"
from langchain_ibm import ChatWatsonx
model_id = "mistralai/mistral-large"
chat = ChatWatsonx(
url="https://us-south.ml.cloud.ibm.com",
apikey="*****",
model_id=model_id,
project_id=project_id,
)
Assistant’s tools
As a next step let’s define set of tools that will be used by the Assistant.
from langchain_core.tools import tool
@tool
def add(a: float, b: float) -> float:
"""Adds a and b."""
return a + b
@tool
def subtract(a: float, b: float) -> float:
"""Subtracts a and b."""
return a - b
@tool
def multiply(a: float, b: float) -> float:
"""Multiplies a and b."""
return a * b
@tool
def divide(a: float, b: float) -> float:
"""Divide a and b."""
return a / b
tools = [add, subtract, multiply, divide]
As we can see it is basic set of tools to perform arithmetic operations like: add, subtract, multiply and divide.
The Graph
Define the flow graph.
from langgraph.prebuilt import create_react_agent
from IPython.display import Image, display
graph = create_react_agent(chat, tools=tools)
display(Image(graph.get_graph().draw_mermaid_png()))
The Assistant (agent) decides on using the right tool for the question asked.
Let’s add a helper function to pretty print the messages from model:
def print_stream(stream):
for s in stream:
message = s["messages"][-1]
if isinstance(message, tuple):
print(message)
else:
message.pretty_print()
Ask the question
Let’s start simple: “What is the result of subtracting 81 from 100 ?” (Human Message)
inputs = {"messages": [("user", "What is the result when 81 is subtracted from 100?")]}
print_stream(graph.stream(inputs, stream_mode="values"))
================================ Human Message =================================
What is the result of subtracting 81 from 100?
================================== Ai Message ==================================
Tool Calls:
subtract (chatcmpl-tool-3bbbf1408c124ee6b6c233a4597bfd3b)
Call ID: chatcmpl-tool-3bbbf1408c124ee6b6c233a4597bfd3b
Args:
a: 100
b: 81
================================= Tool Message =================================
Name: subtract
19.0
================================== Ai Message ==================================
The result of subtracting 81 from 100 is 19.
The answer is correct. We can see that Assistant (Ai Message) made the decision to use following tool: subtract
with following Args: a=100, b=81
:
================================== Ai Message ==================================
Tool Calls:
subtract (chatcmpl-tool-3bbbf1408c124ee6b6c233a4597bfd3b)
Call ID: chatcmpl-tool-3bbbf1408c124ee6b6c233a4597bfd3b
Args:
a: 100
b: 81
Next, there is a Tool Message with function call result: 19
.
================================= Tool Message =================================
Name: subtract
19.0
Let’s ask one more question: “What is the result when the sum of 2 and 3 is multiplied by 6 and then divided by 10 ?”
inputs = {"messages": [("user", "What is the result when the sum of 2 and 3 is multiplied by 6 and then divided by 10?")]}
print_stream(graph.stream(inputs, stream_mode="values"))
================================ Human Message =================================
What is the result when the sum of 2 and 3 is multiplied by 6 and then divided by 10?
================================== Ai Message ==================================
Tool Calls:
add (chatcmpl-tool-6f50afd17e884d8d919ec0f02886e2e6)
Call ID: chatcmpl-tool-6f50afd17e884d8d919ec0f02886e2e6
Args:
a: 2
b: 3
multiply (chatcmpl-tool-3c4b220ab3e74a868d102f31c70f6a77)
Call ID: chatcmpl-tool-3c4b220ab3e74a868d102f31c70f6a77
Args:
a: 5
b: 6
divide (chatcmpl-tool-759fe375c3f74744b390466afa118c00)
Call ID: chatcmpl-tool-759fe375c3f74744b390466afa118c00
Args:
a: 30
b: 10
================================= Tool Message =================================
Name: divide
3.0
================================== Ai Message ==================================
The result is 3.
More examples can be found in the sample notebook.
That’s all — Enjoy building your agent.