@staticmethod def search_web(query): """模拟网络搜索工具(此处为模拟)"""
mock_results ={ "旅行目的地推荐": "巴黎、东京、马尔代夫、云南丽江...", "北京天气": "明天晴,气温 15-25°C,微风。", "Python 教程": "推荐菜鸟教程等网站。" } for key, value in mock_results.items(): if key in query: return f"[网络搜索] 关于 '{query}' 的结果:{value}" return f"[网络搜索] 未找到 '{query}' 的明确信息。"
@staticmethod def make_schedule(steps): """制定日程计划工具"""
schedule ="生成的日程计划:\n" for i, step inenumerate(steps,1):
schedule += f"{i}. {step}\n" return schedule
@staticmethod def get_current_time(): """获取当前时间工具"""
now =datetime.datetime.now() return f"[系统时间] 现在是:{now.strftime('%Y-%m-%d %H:%M:%S')}"
@staticmethod def calculate(expression): """简单计算器工具(安全版本,修复兼容性问题)""" try: # 1. 严格过滤非法字符(仅允许数字、基础运算符、括号、空格)
allowed_chars =set("0123456789+-*/(). ") ifnotall(c in allowed_chars for c in expression): return"[计算器] 表达式包含非法字符,拒绝计算。"
# 第二步:解析思考结果,提取工具和指令(鲁棒性优化)
lines =[line.strip()for line in initial_response.split('\n')if line.strip()]
tool_to_use ="无"
tool_instruction =""
for line in lines: if line.startswith("思考:"): continue# 跳过思考行 elif line.startswith("工具:"):
tool_to_use = line.replace("工具:","").strip() elif line.startswith("指令:"):
tool_instruction = line.replace("指令:","").strip()