Skip to content

Commit eab1695

Browse files
Pouyanpitgasser-nv
authored andcommitted
docs(langgraph): clarify tool examples and replace calculate_math with multiply (#1439)
1 parent 52103d6 commit eab1695

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

docs/user-guides/langchain/langgraph-integration.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ To enhance the functionality of your LangGraph agents, you can combine tool call
156156
157157
### Tool Definition
158158
159-
Define the following tools.
159+
Define the following simplified example tools that demonstrate the integration pattern with NeMo Guardrails.
160160
161161
The first tool, `search_knowledge`, searches a predefined knowledge base for information matching the user's query. It performs matching against keywords like `"capital"`, `"weather"`, and `"python"`, returning relevant information or a generic response if no match is found.
162162
163-
The second tool, `calculate_math`, safely evaluates mathematical expressions by first validating that only allowed characters such as digits, operators, parentheses, and spaces are present. Then, it uses the `eval()` function from Python to calculate the result. It includes error handling to catch and report any calculation errors.
163+
The second tool, `multiply`, performs multiplication of two integers.
164164
165165
```python
166166
from langchain_core.tools import tool
@@ -182,17 +182,9 @@ def search_knowledge(query: str) -> str:
182182
return f"General information about: {query}"
183183
184184
@tool
185-
def calculate_math(expression: str) -> str:
186-
"""Calculate a mathematical expression safely."""
187-
try:
188-
allowed_chars = set("0123456789+-*/(). ")
189-
if not all(c in allowed_chars for c in expression):
190-
return "Expression contains invalid characters"
191-
192-
result = eval(expression)
193-
return f"The result of {expression} is {result}"
194-
except Exception as e:
195-
return f"Could not calculate: {expression}. Error: {str(e)}"
185+
def multiply(a: int, b: int) -> int:
186+
"""Multiply a and b."""
187+
return a * b
196188
```
197189
198190
### Tool Calling Graph with Guardrails
@@ -204,7 +196,7 @@ from langgraph.prebuilt import ToolNode, tools_condition
204196
205197
def create_tool_calling_agent():
206198
llm = ChatOpenAI(model="gpt-4o")
207-
tools = [search_knowledge, calculate_math]
199+
tools = [search_knowledge, multiply]
208200
llm_with_tools = llm.bind_tools(tools)
209201
210202
config = RailsConfig.from_path(config_path)

0 commit comments

Comments
 (0)