Merge 855c3804ce into 512eed0b65
commit
04f5fd0e8c
|
|
@ -1,6 +1,8 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||||
|
import ast
|
||||||
|
import operator
|
||||||
import re
|
import re
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
@ -26,8 +28,22 @@ TOOLS = [
|
||||||
{"type": "function", "function": {"name": "translate_text", "description": "将文本翻译成目标语言", "parameters": {"type": "object", "properties": {"text": {"type": "string", "description": "要翻译的文本"}, "target_language": {"type": "string", "description": "目标语言,如english、chinese、japanese、french"}}, "required": ["text", "target_language"]}}},
|
{"type": "function", "function": {"name": "translate_text", "description": "将文本翻译成目标语言", "parameters": {"type": "object", "properties": {"text": {"type": "string", "description": "要翻译的文本"}, "target_language": {"type": "string", "description": "目标语言,如english、chinese、japanese、french"}}, "required": ["text", "target_language"]}}},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def _safe_eval(expr: 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,
|
||||||
|
ast.UAdd: operator.pos, ast.Mod: operator.mod, ast.FloorDiv: operator.floordiv}
|
||||||
|
def _eval(node):
|
||||||
|
if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)):
|
||||||
|
return node.value
|
||||||
|
if isinstance(node, ast.BinOp) and type(node.op) in _ops:
|
||||||
|
return _ops[type(node.op)](_eval(node.left), _eval(node.right))
|
||||||
|
if isinstance(node, ast.UnaryOp) and type(node.op) in _ops:
|
||||||
|
return _ops[type(node.op)](_eval(node.operand))
|
||||||
|
raise ValueError(f"Unsupported expression: {ast.dump(node)}")
|
||||||
|
return _eval(ast.parse(expr, mode='eval').body)
|
||||||
|
|
||||||
MOCK_RESULTS = {
|
MOCK_RESULTS = {
|
||||||
"calculate_math": lambda args: {"result": str(eval(str(args.get("expression", "0")).replace("^", "**").replace("×", "*").replace("÷", "/").replace("−", "-").replace("²", "**2").replace("³", "**3").replace("(", "(").replace(")", ")")))},
|
"calculate_math": lambda args: {"result": str(_safe_eval(str(args.get("expression", "0")).replace("^", "**").replace("×", "*").replace("÷", "/").replace("−", "-").replace("²", "**2").replace("³", "**3").replace("(", "(").replace(")", ")")))},
|
||||||
"get_current_time": lambda args: {"datetime": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "timezone": args.get("timezone", "Asia/Shanghai")},
|
"get_current_time": lambda args: {"datetime": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "timezone": args.get("timezone", "Asia/Shanghai")},
|
||||||
"random_number": lambda args: {"result": random.randint(int(args.get("min", 0)), int(args.get("max", 100)))},
|
"random_number": lambda args: {"result": random.randint(int(args.get("min", 0)), int(args.get("max", 100)))},
|
||||||
"text_length": lambda args: {"characters": len(args.get("text", "")), "words": len(args.get("text", "").split())},
|
"text_length": lambda args: {"characters": len(args.get("text", "")), "words": len(args.get("text", "").split())},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue