Spaces:
Paused
Paused
| import ast, operator, datetime | |
| def calculator(expression: str) -> str: | |
| ops = {ast.Add: operator.add, ast.Sub: operator.sub, | |
| ast.Mult: operator.mul, ast.Div: operator.truediv, | |
| ast.Pow: operator.pow, ast.USub: operator.neg} | |
| def ev(node): | |
| if isinstance(node, ast.Constant): | |
| return node.value | |
| if isinstance(node, ast.BinOp): | |
| return ops[type(node.op)](ev(node.left), ev(node.right)) | |
| if isinstance(node, ast.UnaryOp): | |
| return ops[type(node.op)](ev(node.operand)) | |
| raise ValueError("unsupported expression") | |
| try: | |
| return str(ev(ast.parse(expression, mode="eval").body)) | |
| except Exception as e: | |
| return f"error: {e}" | |
| def word_count(text: str) -> str: | |
| return str(len(text.split())) | |
| def get_time(_: str = "") -> str: | |
| return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
| TOOLS = {"calculator": calculator, "word_count": word_count, "get_time": get_time} | |
| TOOL_DESCRIPTIONS = """ | |
| - calculator(expression): evaluates a math expression, e.g. "12*7+1" | |
| - word_count(text): counts words in a string | |
| - get_time(): returns the current date and time | |
| """ |