Spaces:
Sleeping
Sleeping
File size: 12,392 Bytes
8a0cdd8 dde4764 0db692a 55d6080 0db692a 05dbca8 dde4764 0db692a dde4764 0db692a dde4764 da4e402 e5449c2 55d6080 dde4764 0db692a 737c21b 0db692a 737c21b 0db692a a2cf089 ad0b84b a9b6a0e ad0b84b 0db692a bd5c59c 0db692a bd5c59c 0db692a 737c21b 0db692a 737c21b 0db692a 737c21b 0db692a 737c21b 0db692a 737c21b 0db692a 05dbca8 26a59c8 05dbca8 26a59c8 05dbca8 0db692a |
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 |
from langchain_core.tools import tool as langchain_tool
from smolagents.tools import Tool, tool
from datetime import datetime
from typing import Literal, List, Union
from smolagents import VisitWebpageTool
from langchain_community.tools.tavily_search import TavilySearchResults
import pandas as pd
import os
@tool
def get_current_time(timezone: str = "America/New_York", format: str = "%Y-%m-%d %H:%M:%S")->str:
"""
Get the current time
Args:
timezone: The timezone to get the current time in. Example: "America/New_York"
format: The format to return the current time in. Example: "%Y-%m-%d %H:%M:%S"
Returns:
The current time
"""
return datetime.now(timezone).strftime(format)
@tool
def sort_list(my_list: List[int], order: Literal["asc", "desc", "alphabetize", "alphabetize_reverse"])->List[int]:
"""
Sort a list in ascending or descending order if the list contains numbers.
Sort it in alphabetically or alphabetically in reverse order if the list contains strings or mixed types.
Args:
my_list: The list to sort
order: The order to sort the list in. Must be one of the following:
- "asc": Sort the list in ascending order. Only for lists containing numbers.
- "desc": Sort the list in descending order. Only for lists containing numbers.
- "alphabetize": Sort the list alphabetically. Only for lists containing strings or mixed types.
- "alphabetize_reverse": Sort the list alphabetically in reverse order. Only for lists containing strings or mixed types.
Returns:
The sorted list
"""
if not isinstance(my_list, List):
raise ValueError("my_list must be a list")
else:
if all(isinstance(item, (int, float)) for item in my_list):
if order in ["asc", "desc"]:
return sorted(my_list, reverse=order == "desc")
elif order in ["alphabetize", "alphabetize_reverse"]:
how = {
"alphabetize": "asc",
"alphabetize_reverse": "desc"
}
return sorted(my_list, key=lambda x: str(x), reverse=how[order] == "desc")
else:
raise ValueError("order must be one of the following: asc, desc, alphabetize, alphabetize_reverse")
else:
print("This is a mixed list. Converting and sorting alphabetically.")
my_list = [str(item) for item in my_list]
how = {
"alphabetize": "asc",
"alphabetize_reverse": "desc"
}
return sorted(my_list, reverse=how[order] == "desc")
#smolagents tools
# visit_webpage_tool = VisitWebpageTool()
tavily_search_tool = Tool.from_langchain(TavilySearchResults(k=3))
@tool
def operate_two_numbers(num1: float, num2: float, operation: Literal["add", "subtract", "multiply", "divide", "power", "modulo"], decimal_places: int = 2)->float:
"""
Operate on two numbers
Args:
num1: The first number to operate on. Must be a float.
num2: The second number to operate on. Must be a float.
operation: The operation to perform. Must be one of the following:
- "add": Add the two numbers
- "subtract": Subtract the two numbers
- "multiply": Multiply the two numbers
- "divide": Divide the two numbers
- "power": Raise the first number to the power of the second number
- "modulo": Return the remainder of the division of the first number by the second number
decimal_places: The number of decimal places to round the result to. Default is 2.
Returns:
The result of the operation
"""
if operation == "add":
return round(num1 + num2, decimal_places)
elif operation == "subtract":
return round(num1 - num2, decimal_places)
elif operation == "multiply":
return round(num1 * num2, decimal_places)
elif operation == "divide":
return round(num1 / num2, decimal_places)
elif operation == "power":
return round(num1 ** num2, decimal_places)
elif operation == "modulo":
return round(num1 % num2, decimal_places)
else:
raise ValueError("operation must be one of the following: add, subtract, multiply, divide, power, modulo")
@tool
def convert_number(orig_num: any, operation: Literal["to_base", "type_cast"], new_base: Literal["binary", "octal", "hexadecimal", "int", "float"], decimal_places: int = 2)->any:
"""
Convert a number to a new base
Args:
orig_num: The number to convert. Must be a float or int.
operation: The operation to perform. Must be one of the following:
- "to_base": Convert the number to a new base.
- "type_cast": Convert the number to a new type.
new_base: The new base to convert the number to. Must be one of the following:
- "binary": Convert the number to binary.
- "octal": Convert the number to octal.
- "hexadecimal": Convert the number to hexadecimal.
- "int": Convert the number to an int.
- "float": Convert the number to a float.
decimal_places: The number of decimal places to round the result to. Default is 2. Only used if operation is "type_cast" and new_base is "float".
Returns:
The converted number. Can be float or int or str.
"""
if operation == "to_base":
if new_base == "binary":
return bin(orig_num)
elif new_base == "octal":
return oct(orig_num)
elif new_base == "hexadecimal":
return hex(orig_num)
else:
raise ValueError("new_base must be one of the following: binary, octal, hexadecimal, int, float")
elif operation == "type_cast":
if new_base == "int":
return int(orig_num)
elif new_base == "float":
return round(float(orig_num), decimal_places)
else:
raise ValueError("new_base must be one of the following: int, float")
else:
raise ValueError("operation must be one of the following: to_base, type_cast")
@tool
def load_dataframe_from_csv(file_path: str)->pd.DataFrame:
"""
Load a pandas DataFrame from a CSV file
Args:
file_path: The path to the CSV file to load.
Returns:
The pandas DataFrame
"""
return pd.read_csv(file_path)
@tool
def load_dataframe_from_excel(file_path: str)->pd.DataFrame:
"""
Load a pandas DataFrame from an Excel file
Args:
file_path: The path to the Excel file to load.
Returns:
The pandas DataFrame
"""
try:
df = pd.read_excel(file_path)
except Exception as e:
curr_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(curr_dir, file_path)
df = pd.read_excel(file_path)
return df
@tool
def to_dataframe(data: List[dict], columns: List[str])->pd.DataFrame:
"""
Convert a list of dictionaries to a pandas DataFrame
Args:
data: The list of dictionaries to convert to a pandas DataFrame.
columns: The columns of the pandas DataFrame.
Returns:
The pandas DataFrame
"""
return pd.DataFrame(data, columns=columns)
@tool
def to_json(data: pd.DataFrame)->str:
"""
Convert a pandas DataFrame to a JSON string
Args:
data: The pandas DataFrame to convert to a JSON string.
Returns:
The JSON string
"""
return data.to_json(orient="records")
@tool
def get_dataframe_data(data: pd.DataFrame, column: any, row: any)->any:
"""
Get a specific cell from a pandas DataFrame
Args:
data: The pandas DataFrame to get the data from.
column: The column to get the data from. Must be a string or int. If int then it is the index of the column.
row: The row to get the data from. Must be a string or int. If int then it is the index of the row.
Returns:
The data from the specified cell. Can be float or int or str.
"""
if isinstance(column, int):
column = data.iloc[:, column]
if isinstance(row, int):
row = data.iloc[row, :]
return data.loc[row, column]
@tool
def get_dataframe_column(data: pd.DataFrame, column: any)->pd.Series:
"""
Get a specific column from a pandas DataFrame
Args:
data: The pandas DataFrame to get the column from.
column: The column to get the data from. Must be a string or int. If int then it is the index of the column.
Returns:
The data from the specified column
"""
return data.iloc[:, column]
@tool
def get_dataframe_row(data: pd.DataFrame, row: any)->pd.Series:
"""
Get a specific row from a pandas DataFrame
Args:
data: The pandas DataFrame to get the row from.
row: The row to get the data from. Must be a string or int. If int then it is the index of the row.
Returns:
The data from the specified row
"""
return data.iloc[row, :]
@tool
def get_dataframe_groupby(data: pd.DataFrame, column: any, operation: Literal["mean", "sum", "count", "min", "max", "median", "std", "var"])->pd.DataFrame:
"""
Group a pandas DataFrame by a specific column and perform an operation on the grouped data
Args:
data: The pandas DataFrame to group.
column: The column to group the data by.
operation: The operation to perform on the grouped data. Must be one of the following:
- "mean": Calculate the mean of the grouped data.
- "sum": Calculate the sum of the grouped data.
- "count": Count the number of rows in the grouped data.
- "min": Calculate the minimum of the grouped data.
- "max": Calculate the maximum of the grouped data.
- "median": Calculate the median of the grouped data.
- "std": Calculate the standard deviation of the grouped data.
- "var": Calculate the variance of the grouped data.
Returns:
The grouped data
"""
if operation == "mean":
return data.groupby(column).mean()
elif operation == "sum":
return data.groupby(column).sum()
elif operation == "count":
return data.groupby(column).count()
elif operation == "min":
return data.groupby(column).min()
elif operation == "max":
return data.groupby(column).max()
elif operation == "median":
return data.groupby(column).median()
elif operation == "std":
return data.groupby(column).std()
elif operation == "var":
return data.groupby(column).var()
else:
raise ValueError("operation must be one of the following: mean, sum, count, min, max, median, std, var")
@tool
def read_python_file_from_path(file_path: str) -> str:
"""
Read and return the contents of a Python file from a given path.
Args:
file_path: Path to the Python file to read
Returns:
str: Contents of the Python file
"""
try:
# Check if file exists
# if not os.path.exists(file_path):
# raise FileNotFoundError(f"File not found: {file_path}")
# Check if it's a Python file
if not file_path.endswith('.py'):
raise ValueError(f"File is not a Python file: {file_path}")
# Try reading with absolute path first
try:
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
except Exception as e:
print(f"Failed to read with absolute path: {str(e)}")
# Try with adjusted path
current_file_path = os.path.abspath(__file__)
current_file_dir = os.path.dirname(current_file_path)
adjusted_path = os.path.join(current_file_dir, file_path)
print(f"Trying adjusted path: {adjusted_path}")
# if not os.path.exists(adjusted_path):
# raise FileNotFoundError(f"File not found at either {file_path} or {adjusted_path}")
with open(adjusted_path, 'r', encoding='utf-8') as f:
return f.read()
except Exception as e:
raise RuntimeError(f"Error reading Python file: {str(e)}")
|