Spaces:
Sleeping
Sleeping
mchinea
commited on
Commit
·
952df75
1
Parent(s):
d5411e4
add new tools
Browse files- agent_smolagent.py +2 -25
- tools_smolagent.py +115 -1
agent_smolagent.py
CHANGED
@@ -14,20 +14,7 @@ from smolagents.default_tools import (DuckDuckGoSearchTool,
|
|
14 |
SpeechToTextTool,
|
15 |
PythonInterpreterTool)
|
16 |
#from final_answer import FinalAnswerTool, check_reasoning, ensure_formatting
|
17 |
-
from tools_smolagent import
|
18 |
-
extract_text_from_image,
|
19 |
-
analyze_csv_file,
|
20 |
-
analyze_excel_file,
|
21 |
-
youtube_transcribe,
|
22 |
-
transcribe_audio,
|
23 |
-
wikipedia_search)
|
24 |
-
|
25 |
-
#(use_vision_model, youtube_frames_to_images,
|
26 |
-
# read_file,
|
27 |
-
# extract_text_from_image, analyze_csv_file,
|
28 |
-
# analyze_excel_file, youtube_transcribe,
|
29 |
-
# transcribe_audio, review_youtube_video)
|
30 |
-
#from model_provider import create_react_model
|
31 |
|
32 |
load_dotenv()
|
33 |
|
@@ -62,17 +49,7 @@ def build_agent():
|
|
62 |
VisitWebpageTool(max_output_length=500000),
|
63 |
#WikipediaSearchTool(extract_format='HTML'),
|
64 |
SpeechToTextTool(),
|
65 |
-
|
66 |
-
extract_text_from_image,
|
67 |
-
analyze_csv_file,
|
68 |
-
analyze_excel_file,
|
69 |
-
youtube_transcribe,
|
70 |
-
transcribe_audio,
|
71 |
-
wikipedia_search
|
72 |
-
#youtube_transcribe,
|
73 |
-
# use_vision_model,
|
74 |
-
# youtube_frames_to_images,
|
75 |
-
#transcribe_audio,
|
76 |
],
|
77 |
managed_agents=[],
|
78 |
additional_authorized_imports=['os', 'pandas', 'numpy', 'PIL', 'tempfile', 'PIL.Image'],
|
|
|
14 |
SpeechToTextTool,
|
15 |
PythonInterpreterTool)
|
16 |
#from final_answer import FinalAnswerTool, check_reasoning, ensure_formatting
|
17 |
+
from tools_smolagent import level1_tools
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
load_dotenv()
|
20 |
|
|
|
49 |
VisitWebpageTool(max_output_length=500000),
|
50 |
#WikipediaSearchTool(extract_format='HTML'),
|
51 |
SpeechToTextTool(),
|
52 |
+
level1_tools,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
],
|
54 |
managed_agents=[],
|
55 |
additional_authorized_imports=['os', 'pandas', 'numpy', 'PIL', 'tempfile', 'PIL.Image'],
|
tools_smolagent.py
CHANGED
@@ -346,4 +346,118 @@ def wikipedia_search(query: str) -> dict:
|
|
346 |
return {"wiki_results": formatted}
|
347 |
|
348 |
except Exception as e:
|
349 |
-
return {"wiki_results": f"Error during Wikipedia search: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
return {"wiki_results": formatted}
|
347 |
|
348 |
except Exception as e:
|
349 |
+
return {"wiki_results": f"Error during Wikipedia search: {str(e)}"}
|
350 |
+
|
351 |
+
|
352 |
+
#Mathematical tools
|
353 |
+
@tool
|
354 |
+
def multiply(a: float, b: float) -> float:
|
355 |
+
"""Multiply two numbers.
|
356 |
+
Args:
|
357 |
+
a: first number
|
358 |
+
b: second number
|
359 |
+
Returns:
|
360 |
+
Multiplication result
|
361 |
+
"""
|
362 |
+
return a * b
|
363 |
+
|
364 |
+
|
365 |
+
@tool
|
366 |
+
def add(a: float, b: float) -> float:
|
367 |
+
"""Add two numbers.
|
368 |
+
Args:
|
369 |
+
a: first number
|
370 |
+
b: second number
|
371 |
+
Returns:
|
372 |
+
Addition result
|
373 |
+
"""
|
374 |
+
return a + b
|
375 |
+
|
376 |
+
|
377 |
+
@tool
|
378 |
+
def subtract(a: float, b: float) -> float:
|
379 |
+
"""Subtract two numbers.
|
380 |
+
Args:
|
381 |
+
a: first number
|
382 |
+
b: second number
|
383 |
+
Returns:
|
384 |
+
Subtraction result
|
385 |
+
"""
|
386 |
+
return a - b
|
387 |
+
|
388 |
+
|
389 |
+
@tool
|
390 |
+
def divide(a: float, b: float) -> float:
|
391 |
+
"""Divide two numbers.
|
392 |
+
Args:
|
393 |
+
a: first number
|
394 |
+
b: second number
|
395 |
+
Returns:
|
396 |
+
Division result
|
397 |
+
"""
|
398 |
+
if b == 0:
|
399 |
+
raise ValueError("Cannot divide by zero.")
|
400 |
+
return a / b
|
401 |
+
|
402 |
+
|
403 |
+
@tool
|
404 |
+
def modulus(a: int, b: int) -> int:
|
405 |
+
"""Get the modulus of two numbers.
|
406 |
+
Args:
|
407 |
+
a: first number
|
408 |
+
b: second number
|
409 |
+
Returns:
|
410 |
+
Modulus result
|
411 |
+
"""
|
412 |
+
return a % b
|
413 |
+
|
414 |
+
from langchain_core.tools import tool
|
415 |
+
|
416 |
+
|
417 |
+
@tool
|
418 |
+
def convert_units(value: float, from_unit: str, to_unit: str) -> float:
|
419 |
+
"""
|
420 |
+
Converts a value from one unit to another.
|
421 |
+
|
422 |
+
Args:
|
423 |
+
value: The numerical value to convert.
|
424 |
+
from_unit: The original unit (e.g. 'miles', 'kg', 'celsius').
|
425 |
+
to_unit: The target unit (e.g. 'kilometers', 'lb', 'fahrenheit').
|
426 |
+
|
427 |
+
Supported conversions:
|
428 |
+
- miles <-> kilometers
|
429 |
+
- kilograms <-> pounds
|
430 |
+
- celsius <-> fahrenheit
|
431 |
+
|
432 |
+
Returns:
|
433 |
+
The converted value result.
|
434 |
+
"""
|
435 |
+
conversions = {
|
436 |
+
("miles", "kilometers"): lambda v: v * 1.60934,
|
437 |
+
("kilometers", "miles"): lambda v: v / 1.60934,
|
438 |
+
("kilograms", "pounds"): lambda v: v * 2.20462,
|
439 |
+
("pounds", "kilograms"): lambda v: v / 2.20462,
|
440 |
+
("celsius", "fahrenheit"): lambda v: (v * 9/5) + 32,
|
441 |
+
("fahrenheit", "celsius"): lambda v: (v - 32) * 5/9,
|
442 |
+
}
|
443 |
+
|
444 |
+
key = (from_unit.lower(), to_unit.lower())
|
445 |
+
if key not in conversions:
|
446 |
+
raise ValueError(f"Conversion from {from_unit} to {to_unit} not supported.")
|
447 |
+
|
448 |
+
return conversions[key](value)
|
449 |
+
|
450 |
+
level1_tools = [
|
451 |
+
multiply,
|
452 |
+
add,
|
453 |
+
subtract,
|
454 |
+
divide,
|
455 |
+
modulus,
|
456 |
+
read_file,
|
457 |
+
extract_text_from_image,
|
458 |
+
analyze_csv_file,
|
459 |
+
analyze_excel_file,
|
460 |
+
youtube_transcribe,
|
461 |
+
transcribe_audio,
|
462 |
+
wikipedia_search
|
463 |
+
]
|