Spaces:
Runtime error
Runtime error
abtsousa commited on
Commit ·
ffc544c
1
Parent(s): 2665628
Refactor tool imports and add calculator functions to the toolset
Browse files- tools/__init__.py +12 -4
- tools/calculator.py +118 -0
tools/__init__.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
from .wikipedia import wiki_fetch_article, wiki_parse_html
|
| 2 |
from .search import web_search
|
| 3 |
from .code_interpreter import execute_code_multilang
|
| 4 |
-
from .files import
|
|
|
|
| 5 |
from langchain_core.tools import BaseTool
|
|
|
|
| 6 |
|
| 7 |
def get_all_tools() -> list[BaseTool]:
|
| 8 |
"""
|
|
@@ -15,10 +17,16 @@ def get_all_tools() -> list[BaseTool]:
|
|
| 15 |
wiki_fetch_article,
|
| 16 |
wiki_parse_html,
|
| 17 |
web_search,
|
| 18 |
-
execute_code_multilang
|
| 19 |
]
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
return tools
|
| 24 |
|
|
|
|
| 1 |
from .wikipedia import wiki_fetch_article, wiki_parse_html
|
| 2 |
from .search import web_search
|
| 3 |
from .code_interpreter import execute_code_multilang
|
| 4 |
+
from .files import *
|
| 5 |
+
from .calculator import *
|
| 6 |
from langchain_core.tools import BaseTool
|
| 7 |
+
from . import calculator, files
|
| 8 |
|
| 9 |
def get_all_tools() -> list[BaseTool]:
|
| 10 |
"""
|
|
|
|
| 17 |
wiki_fetch_article,
|
| 18 |
wiki_parse_html,
|
| 19 |
web_search,
|
| 20 |
+
execute_code_multilang,
|
| 21 |
]
|
| 22 |
+
|
| 23 |
+
# Automatically add all calculator functions
|
| 24 |
+
for name in calculator.__all__:
|
| 25 |
+
tools.append(getattr(calculator, name))
|
| 26 |
+
|
| 27 |
+
# Automatically add all file management functions
|
| 28 |
+
for name in files.__all__:
|
| 29 |
+
tools.append(getattr(files, name))
|
| 30 |
|
| 31 |
return tools
|
| 32 |
|
tools/calculator.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
import wikipediaapi
|
| 3 |
+
import requests
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
+
|
| 6 |
+
__all__ = [
|
| 7 |
+
'add_numbers',
|
| 8 |
+
'subtract_numbers',
|
| 9 |
+
'multiply_numbers',
|
| 10 |
+
'divide_numbers',
|
| 11 |
+
'power_numbers',
|
| 12 |
+
'root_numbers',
|
| 13 |
+
'modulus_numbers'
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
@tool
|
| 17 |
+
def add_numbers(a: float, b: float) -> float:
|
| 18 |
+
"""
|
| 19 |
+
Adds two numbers.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
a (float): The first number.
|
| 23 |
+
b (float): The second number.
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
float: The sum of the two numbers.
|
| 27 |
+
"""
|
| 28 |
+
return a + b
|
| 29 |
+
|
| 30 |
+
@tool
|
| 31 |
+
def subtract_numbers(a: float, b: float) -> float:
|
| 32 |
+
"""
|
| 33 |
+
Subtracts the second number from the first.
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
a (float): The first number.
|
| 37 |
+
b (float): The second number.
|
| 38 |
+
|
| 39 |
+
Returns:
|
| 40 |
+
float: The result of the subtraction.
|
| 41 |
+
"""
|
| 42 |
+
return a - b
|
| 43 |
+
|
| 44 |
+
@tool
|
| 45 |
+
def multiply_numbers(a: float, b: float) -> float:
|
| 46 |
+
"""
|
| 47 |
+
Multiplies two numbers.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
a (float): The first number.
|
| 51 |
+
b (float): The second number.
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
float: The product of the two numbers.
|
| 55 |
+
"""
|
| 56 |
+
return a * b
|
| 57 |
+
|
| 58 |
+
@tool
|
| 59 |
+
def divide_numbers(a: float, b: float) -> float:
|
| 60 |
+
"""
|
| 61 |
+
Divides the first number by the second.
|
| 62 |
+
|
| 63 |
+
Args:
|
| 64 |
+
a (float): The first number.
|
| 65 |
+
b (float): The second number.
|
| 66 |
+
|
| 67 |
+
Returns:
|
| 68 |
+
float: The result of the division.
|
| 69 |
+
"""
|
| 70 |
+
if b == 0:
|
| 71 |
+
return float("inf") # Handle division by zero
|
| 72 |
+
return a / b
|
| 73 |
+
|
| 74 |
+
@tool
|
| 75 |
+
def modulus_numbers(a: float, b: float) -> float:
|
| 76 |
+
"""
|
| 77 |
+
Calculates the modulus of the first number by the second.
|
| 78 |
+
|
| 79 |
+
Args:
|
| 80 |
+
a (float): The first number.
|
| 81 |
+
b (float): The second number.
|
| 82 |
+
|
| 83 |
+
Returns:
|
| 84 |
+
float: The result of the modulus operation.
|
| 85 |
+
"""
|
| 86 |
+
if b == 0:
|
| 87 |
+
return float("inf") # Handle division by zero
|
| 88 |
+
return a % b
|
| 89 |
+
|
| 90 |
+
@tool
|
| 91 |
+
def power_numbers(a: float, b: float) -> float:
|
| 92 |
+
"""
|
| 93 |
+
Raises the first number to the power of the second.
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
a (float): The base number.
|
| 97 |
+
b (float): The exponent.
|
| 98 |
+
|
| 99 |
+
Returns:
|
| 100 |
+
float: The result of the exponentiation.
|
| 101 |
+
"""
|
| 102 |
+
return a ** b
|
| 103 |
+
|
| 104 |
+
@tool
|
| 105 |
+
def root_numbers(a: float, b: float) -> float:
|
| 106 |
+
"""
|
| 107 |
+
Calculates the nth root of a number.
|
| 108 |
+
|
| 109 |
+
Args:
|
| 110 |
+
a (float): The number.
|
| 111 |
+
b (float): The root.
|
| 112 |
+
|
| 113 |
+
Returns:
|
| 114 |
+
float: The result of the root operation.
|
| 115 |
+
"""
|
| 116 |
+
if b == 0:
|
| 117 |
+
return float("inf") # Handle division by zero
|
| 118 |
+
return a ** (1 / b)
|