-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathassistant.py
More file actions
387 lines (297 loc) · 11.1 KB
/
Copy pathmathassistant.py
File metadata and controls
387 lines (297 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
from langchain_ibm import ChatWatsonx
from langchain.agents import AgentType
from langchain_community.utilities import WikipediaAPIWrapper
from langchain.agents import Tool
from langchain_core.tools import tool
from typing import Dict, Union
from langgraph.prebuilt import create_react_agent
from typing import List
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
from langchain.agents import initialize_agent
import re
# A LLM of your choosing
openai_llm = ChatOpenAI(
model="gpt-4.1-nano",
api_key = "your openai api key here",
)
watsonx_llm = ChatWatsonx(
model_id="ibm/granite-3-2-8b-instruct",
url="https://us-south.ml.cloud.ibm.com",
project_id="your project id associated with the API key",
api_key="your watsonx.ai api key here",
)
# response = llm.invoke("What is tool calling in LangChain?")
# print("\nResponse content", response.content)
# using the @tool operator
@tool
def add_numbers(inputs:str) -> dict:
"""
Adds a list of numbers provided in the input string.
Parameters:
- inputs (str):
string, it should contain numbers that can be extracted and summed.
Returns:
- dict: A dictionary with a single key "result" containing the sum of the numbers.
Example Input:
"Add the numbers 10, 20, and 30."
Example Output:
{"result": 60}
"""
# Use regular expressions to extract all numbers from the input
numbers = [int(num) for num in re.findall(r'\d+', inputs)]
# numbers = [int(x) for x in inputs.replace(",", "").split() if x.isdigit()]
result = sum(numbers)
return {"result": result}
print("@tool Decorator Approach:")
@tool
def add_numbers_with_options(numbers: List[float], absolute: bool = False) -> float:
"""
Adds a list of numbers provided as input.
Parameters:
- numbers (List[float]): A list of numbers to be summed.
- absolute (bool): If True, use the absolute values of the numbers before summing.
Returns:
- float: The total sum of the numbers.
"""
if absolute:
numbers = [abs(n) for n in numbers]
return sum(numbers)
@tool
def sum_numbers_with_complex_output(inputs:str) -> Dict[str, Union[float, str]]:
"""
Extracts and sums all integers and decimal numbers from the input string.
Parameters:
- inputs (str) : string that may contain numerical values
Returns:
- dict: A dictionary with the key "result". If numbers are found, the value is their sum (float).
If no numbers are found or an error occurs, the value is a corresponding message (str).
Example Input:
"Add 10, 20.5, and -3."
Example Output:
{"result": 27.5}
"""
matches = re.findall(r'-?\d+(?:\.\d+)?', inputs)
if not matches:
return {"result": "No numbers found in input."}
try:
numbers = [float(num) for num in matches]
total = sum(numbers)
return {"result": total}
except Exception as e:
return {"result": f"Error during summation: {str(e)}"}
@tool
def sum_numbers_from_text(inputs:str) -> float:
"""
Adds a list of numbers provided in the input string.
Args:
text: A string containing numbers that should be extracted and summed.
Returns:
The sum of all numbers found in the input.
"""
# Use regular expressions to extract all numbers from the input
numbers = [int(num) for num in re.findall(r'\d+', inputs)]
result = sum(numbers)
return result
@tool
def subtract_numbers(inputs: str) -> dict:
"""
Extracts numbers from a string and performs subtraction sequentially, starting with the first number.
This function is designed to handle input in string format, where numbers may be separated by spaces,
commas, or other delimiters. It parses the input string, extracts numeric values, and calculates
the result by subtracting each subsequent number from the first. inputs[0]-inputs[1]-inputs[2]
Parameters:
- inputs (str):
A string containing numbers to subtract. The string can include spaces, commas, or other
delimiters between the numbers.
Returns:
- dict:
A dictionary containing the key "result" with the calculated difference as its value.
If no valid numbers are found in the input string, the result defaults to 0.
Example Usage:
- Input: "100, 20, 10"
- Output: {"result": 70}
Limitations:
- The function does not handle cases where numbers are formatted with decimals or other non-integer representations.
"""
# Extract numbers from the string
numbers = [int(num) for num in inputs.replace(",", "").split() if num.isdigit()]
# If no numbers are found, return 0
if not numbers:
return {"result": 0}
# Start with the first number
result = numbers[0]
# Subtract all subsequent numbers
for num in numbers[1:]:
result -= num
return {"result": result}
# Multiplication Tool
@tool
def multiply_numbers(inputs: str) -> dict:
"""
Extracts numbers from a string and calculates their product.
Parameters:
- inputs (str): A string containing numbers separated by spaces, commas, or other delimiters.
Returns:
- dict: A dictionary with the key "result" containing the product of the numbers.
Example Input:
"2, 3, 4"
Example Output:
{"result": 24}
Notes:
- If no numbers are found, the result defaults to 1 (neutral element for multiplication).
"""
# Extract numbers from the string
numbers = [int(num) for num in inputs.replace(",", "").split() if num.isdigit()]
print(numbers)
# If no numbers are found, return 1
if not numbers:
return {"result": 1}
# Calculate the product of the numbers
result = 1
for num in numbers:
result *= num
print(num)
return {"result": result}
# Division Tool
@tool
def divide_numbers(inputs: str) -> dict:
"""
Extracts numbers from a string and calculates the result of dividing the first number
by the subsequent numbers in sequence.
Parameters:
- inputs (str): A string containing numbers separated by spaces, commas, or other delimiters.
Returns:
- dict: A dictionary with the key "result" containing the quotient.
Example Input:
"100, 5, 2"
Example Output:
{"result": 10.0}
Notes:
- If no numbers are found, the result defaults to 0.
- Division by zero will raise an error.
"""
# Extract numbers from the string
numbers = [int(num) for num in inputs.replace(",", "").split() if num.isdigit()]
# If no numbers are found, return 0
if not numbers:
return {"result": 0}
# Calculate the result of dividing the first number by subsequent numbers
result = numbers[0]
for num in numbers[1:]:
result /= num
return {"result": result}
@tool
def search_wikipedia(query: str) -> str:
"""Search Wikipedia for factual information about a topic.
Parameters:
- query (str): The topic or question to search for on Wikipedia
Returns:
- str: A summary of relevant information from Wikipedia
"""
wiki = WikipediaAPIWrapper()
return wiki.run(query)
# # Testing multiply_tool
# multiply_test_input = "2, 3, and four "
# multiply_result = multiply_numbers.invoke(multiply_test_input)
# print("--- Testing MultiplyTool ---")
# print(f"Input: {multiply_test_input}")
# print(f"Output: {multiply_result}")
# # Testing divide_tool
# divide_test_input = "100, 5, two"
# divide_result = divide_numbers.invoke(divide_test_input)
# print("--- Testing DivideTool ---")
# print(f"Input: {divide_test_input}")
# print(f"Output: {divide_result}")
tools=[add_numbers, subtract_numbers, multiply_numbers, divide_numbers, search_wikipedia]
math_agent = create_react_agent(
model=llm_ai,
tools=tools,
prompt="You are a helpful mathematical assistant that can perform various operations. Use the tools precisely and explain your reasoning clearly."
)
# response=math_agent.invoke({
# "messages": [("human", "What is 25 divided by 4?")]
# })
# final_answer=response["messages"][-1].content
# print(final_answer)
# response2=math_agent.invoke({
# "messages" : [("human", "Subtract 100, 20, and 10.")]
# })
# final_answer_2 = response2["messages"][-2].content
# print(final_answer_2)
# print("\n----Testing multiply tool----")
# response3=math_agent.invoke({
# "messages" : [("human", "Multiply 2, 3, and four.")]
# })
# print("Agent Response:", response3["messages"][-1].content)
# print("\n--- Testing divide tool---")
# response = math_agent.invoke({
# "messages": [("human", "Divide 100 by 5 and then by 2.")]
# })
# print("Agent Response:", response["messages"][-1].content)
# Test Cases
test_cases = [
{
"query": "Subtract 100, 20, and 10.",
"expected": {"result": 70},
"description": "Testing subtraction tool with sequential subtraction."
},
{
"query": "Multiply 2, 3, and 4.",
"expected": {"result": 24},
"description": "Testing multiplication tool for a list of numbers."
},
{
"query": "Divide 100 by 5 and then by 2.",
"expected": {"result": 10.0},
"description": "Testing division tool with sequential division."
},
{
"query": "Subtract 50 from 20.",
"expected": {"result": -30},
"description": "Testing subtraction tool with negative results."
}
]
correct_tasks = []
# Corrected test execution
for index, test in enumerate(test_cases, start=1):
query = test["query"]
expected_result = test["expected"]["result"] # Extract just the value
print(f"\n--- Test Case {index}: {test['description']} ---")
print(f"Query: {query}")
# Properly format the input
response = math_agent.invoke({"messages": [("human", query)]})
# Find the tool message in the response
tool_message = None
for msg in response["messages"]:
if hasattr(msg, 'name') and msg.name in ['add_numbers', 'new_subtract_numbers', 'multiply_numbers', 'divide_numbers']:
tool_message = msg
break
if tool_message:
# Parse the tool result from its content
import json
tool_result = json.loads(tool_message.content)["result"]
print(f"Tool Result: {tool_result}")
print(f"Expected Result: {expected_result}")
if tool_result == expected_result:
print(f"✅ Test Passed: {test['description']}")
correct_tasks.append(test["description"])
else:
print(f"❌ Test Failed: {test['description']}")
else:
print("❌ No tool was called by the agent")
print("\nCorrectly passed tests:", correct_tasks)
print()
print()
query = "What is the population of Canada? Multiply it by 0.75"
response = math_agent.invoke({"messages": [("human", query)]})
print("\nMessage sequence:")
for i, msg in enumerate(response["messages"]):
print(f"\n--- Message {i+1} ---")
print(f"Type: {type(msg).__name__}")
if hasattr(msg, 'content'):
print(f"Content: {msg.content}")
if hasattr(msg, 'name'):
print(f"Name: {msg.name}")
if hasattr(msg, 'tool_calls') and msg.tool_calls:
print(f"Tool calls: {msg.tool_calls}")