from flask import Flask, request, jsonify, send_from_directory, render_template, redirect, url_for, session from flask_cors import CORS import os import time import traceback import json import re import sys import io import threading import queue import contextlib import signal import psutil from dotenv import load_dotenv # 导入模块路由 from modules.knowledge_base.routes import knowledge_bp from modules.code_executor.routes import code_executor_bp from modules.visualization.routes import visualization_bp from modules.agent_builder.routes import agent_builder_bp # 加载环境变量 load_dotenv() app = Flask(__name__) CORS(app) # 设置session密钥 app.secret_key = os.getenv("SECRET_KEY", "your_secret_key_here") # 注册蓝图 app.register_blueprint(knowledge_bp, url_prefix='/api/knowledge') app.register_blueprint(code_executor_bp, url_prefix='/api/code') app.register_blueprint(visualization_bp, url_prefix='/api/visualization') app.register_blueprint(agent_builder_bp, url_prefix='/api/agent') # 确保目录存在 os.makedirs('static', exist_ok=True) os.makedirs('uploads', exist_ok=True) os.makedirs('agents', exist_ok=True) # 用于代码执行的上下文 execution_contexts = {} # 硬编码用户(仅用于演示) users = { "teachers": [ {"username": "teacher", "password": "123456", "name": "李志刚"}, {"username": "admin", "password": "admin123", "name": "管理员"} ], "students": [ {"username": "student1", "password": "123456", "name": "张三"}, {"username": "student2", "password": "123456", "name": "李四"} ] } # 学生活动记录存储(实际应用中应使用数据库) student_activities = {} def get_memory_usage(): """获取当前进程的内存使用情况""" process = psutil.Process(os.getpid()) return f"{process.memory_info().rss / 1024 / 1024:.1f} MB" class CustomStdin: def __init__(self, input_queue): self.input_queue = input_queue self.buffer = "" def readline(self): if not self.buffer: self.buffer = self.input_queue.get() + "\n" result = self.buffer self.buffer = "" return result class InteractiveExecution: """管理Python代码的交互式执行""" def __init__(self, code): self.code = code self.context_id = str(time.time()) self.is_complete = False self.is_waiting_for_input = False self.stdout_buffer = io.StringIO() self.last_read_position = 0 self.input_queue = queue.Queue() self.error = None self.thread = None self.should_terminate = False def run(self): """在单独的线程中启动执行""" self.thread = threading.Thread(target=self._execute) self.thread.daemon = True self.thread.start() # 给执行一点时间开始 time.sleep(0.1) return self.context_id def _execute(self): """执行代码,处理标准输入输出""" try: # 保存原始的stdin/stdout orig_stdin = sys.stdin orig_stdout = sys.stdout # 创建自定义stdin custom_stdin = CustomStdin(self.input_queue) # 重定向stdin和stdout sys.stdin = custom_stdin sys.stdout = self.stdout_buffer try: # 检查终止的函数 self._last_check_time = 0 def check_termination(): if self.should_terminate: raise KeyboardInterrupt("Execution terminated by user") # 设置一个模拟__main__模块的命名空间 shared_namespace = { "__builtins__": __builtins__, "_check_termination": check_termination, "time": time, "__name__": "__main__" } # 在这个命名空间中执行用户代码 try: exec(self.code, shared_namespace) except KeyboardInterrupt: print("\nExecution terminated by user") except Exception as e: self.error = { "error": str(e), "traceback": traceback.format_exc() } finally: # 恢复原始stdin/stdout sys.stdin = orig_stdin sys.stdout = orig_stdout # 标记执行完成 self.is_complete = True except Exception as e: self.error = { "error": str(e), "traceback": traceback.format_exc() } self.is_complete = True def terminate(self): """终止执行""" self.should_terminate = True # 如果在等待输入,放入一些内容以解除阻塞 if self.is_waiting_for_input: self.input_queue.put("\n") # 给执行一点时间终止 time.sleep(0.2) # 标记为完成 self.is_complete = True return True def provide_input(self, user_input): """为运行的代码提供输入""" self.input_queue.put(user_input) self.is_waiting_for_input = False return True def get_output(self): """获取stdout缓冲区的当前内容""" output = self.stdout_buffer.getvalue() return output def get_new_output(self): """只获取自上次读取以来的新输出""" current_value = self.stdout_buffer.getvalue() if self.last_read_position < len(current_value): new_output = current_value[self.last_read_position:] self.last_read_position = len(current_value) return new_output return "" # 记录活动函数(可在各个操作点调用) def record_student_activity(username, activity_type, title, agent_id=None, agent_name=None): """记录学生活动""" if username not in student_activities: student_activities[username] = [] # 创建活动记录 activity = { "type": activity_type, # 'chat', 'code', 'viz', 'mindmap' "title": title, "timestamp": int(time.time()), "agent_id": agent_id, "agent_name": agent_name } # 添加到用户活动列表(最多保存20条记录) student_activities[username].insert(0, activity) if len(student_activities[username]) > 20: student_activities[username] = student_activities[username][:20] return activity # 登录相关路由 @app.route('/login.html') def login_page(): """登录页面""" return render_template('login.html') @app.route('/api/auth/login', methods=['POST']) def login(): """处理登录请求""" data = request.json username = data.get('username') password = data.get('password') user_type = data.get('type', 'teacher') # 默认为教师 if user_type == 'teacher': user_list = users['teachers'] else: user_list = users['students'] for user in user_list: if user['username'] == username and user['password'] == password: # 设置session session['logged_in'] = True session['username'] = username session['user_type'] = user_type session['user_name'] = user['name'] return jsonify({ 'success': True, 'user': { 'name': user['name'], 'type': user_type } }) return jsonify({ 'success': False, 'message': '用户名或密码错误' }), 401 @app.route('/api/auth/logout', methods=['POST']) def logout(): """处理登出请求""" session.clear() return jsonify({ 'success': True }) @app.route('/api/auth/check', methods=['GET']) def check_auth(): """检查用户是否已登录""" if session.get('logged_in'): return jsonify({ 'success': True, 'user': { 'name': session.get('user_name'), 'type': session.get('user_type') } }) return jsonify({ 'success': False }), 401 # 登录验证装饰器 def login_required(f): def decorated_function(*args, **kwargs): if not session.get('logged_in'): return redirect(url_for('login_page')) return f(*args, **kwargs) decorated_function.__name__ = f.__name__ return decorated_function # 教师角色验证装饰器 def teacher_required(f): def decorated_function(*args, **kwargs): if not session.get('logged_in') or session.get('user_type') != 'teacher': return jsonify({ 'success': False, 'message': '需要教师权限' }), 403 return f(*args, **kwargs) decorated_function.__name__ = f.__name__ return decorated_function # 学生角色验证装饰器 def student_required(f): def decorated_function(*args, **kwargs): if not session.get('logged_in') or session.get('user_type') != 'student': return jsonify({ 'success': False, 'message': '需要学生权限' }), 403 return f(*args, **kwargs) decorated_function.__name__ = f.__name__ return decorated_function # 首页路由 @app.route('/') def root(): """重定向到登录页面或主界面""" if session.get('logged_in'): if session.get('user_type') == 'teacher': return redirect('/index.html') else: return redirect('/student_portal.html') return redirect('/login.html') @app.route('/index.html') @login_required def index(): """教师端主界面""" if session.get('user_type') != 'teacher': return redirect('/student_portal.html') return render_template('index.html') @app.route('/student_portal.html') @login_required def student_portal(): """学生端门户""" if session.get('user_type') != 'student': return redirect('/index.html') return render_template('student_portal.html') @app.route('/code_execution.html') def code_execution_page(): """代码执行页面""" return send_from_directory(os.path.dirname(os.path.abspath(__file__)), 'templates/code_execution.html') @app.route('/verify_token.html') def verify_token_page(): """令牌验证页面""" return render_template('token_verification.html') @app.route('/api/progress/', methods=['GET']) def get_progress(task_id): """获取文档处理进度""" try: # 从知识库模块访问处理任务 from modules.knowledge_base.routes import processing_tasks progress_data = processing_tasks.get(task_id, { 'progress': 0, 'status': '未找到任务', 'error': True }) return jsonify({"success": True, "data": progress_data}) except Exception as e: traceback.print_exc() return jsonify({"success": False, "message": str(e)}), 500 @app.route('/student/') def student_view(agent_id): """学生访问Agent界面""" token = request.args.get('token', '') # 验证Agent存在 agent_path = os.path.join('agents', f"{agent_id}.json") if not os.path.exists(agent_path): return render_template('error.html', message="找不到指定的Agent", error_code=404) # 加载Agent配置 with open(agent_path, 'r', encoding='utf-8') as f: try: agent_config = json.load(f) except: return render_template('error.html', message="Agent配置无效", error_code=500) # 验证访问令牌 if token: valid_token = False if "distributions" in agent_config: for dist in agent_config["distributions"]: if dist.get("token") == token: valid_token = True break if not valid_token: return render_template('token_verification.html', message="访问令牌无效", error_code=403) # 更新使用统计 if "distributions" in agent_config: for dist in agent_config["distributions"]: if dist.get("token") == token: # 更新分发使用次数 dist["usage_count"] = dist.get("usage_count", 0) + 1 # 更新Agent使用统计 if "stats" not in agent_config: agent_config["stats"] = {} agent_config["stats"]["usage_count"] = agent_config["stats"].get("usage_count", 0) + 1 agent_config["stats"]["last_used"] = int(time.time()) # 保存更新后的Agent配置 with open(agent_path, 'w', encoding='utf-8') as f: json.dump(agent_config, f, ensure_ascii=False, indent=2) break # 渲染学生页面 return render_template('student.html', agent_id=agent_id, agent_name=agent_config.get('name', 'AI学习助手'), agent_description=agent_config.get('description', ''), token=token) @app.route('/api/student/chat/', methods=['POST']) def student_chat(agent_id): """学生与Agent聊天的API""" try: data = request.json message = data.get('message', '') token = data.get('token', '') if not message: return jsonify({"success": False, "message": "消息不能为空"}), 400 # 验证Agent和令牌 agent_path = os.path.join('agents', f"{agent_id}.json") if not os.path.exists(agent_path): return jsonify({"success": False, "message": "Agent不存在"}), 404 with open(agent_path, 'r', encoding='utf-8') as f: agent_config = json.load(f) # 验证令牌(如果提供) if token and "distributions" in agent_config: valid_token = False for dist in agent_config["distributions"]: if dist.get("token") == token: valid_token = True # 更新使用计数 dist["usage_count"] = dist.get("usage_count", 0) + 1 break if not valid_token: return jsonify({"success": False, "message": "访问令牌无效"}), 403 # 更新Agent使用统计 if "stats" not in agent_config: agent_config["stats"] = {} agent_config["stats"]["usage_count"] = agent_config["stats"].get("usage_count", 0) + 1 agent_config["stats"]["last_used"] = int(time.time()) # 保存更新后的Agent配置 with open(agent_path, 'w', encoding='utf-8') as f: json.dump(agent_config, f, ensure_ascii=False, indent=2) # 获取Agent关联的知识库和插件 knowledge_bases = agent_config.get('knowledge_bases', []) plugins = agent_config.get('plugins', []) # 获取学科和指导者信息 subject = agent_config.get('subject', agent_config.get('name', '通用学科')) instructor = agent_config.get('instructor', '教师') # 创建Generator实例,传入学科和指导者信息 from modules.knowledge_base.generator import Generator generator = Generator(subject=subject, instructor=instructor) # 检测需要使用的插件 suggested_plugins = [] # 检测是否需要代码执行插件 if 'code' in plugins and ('代码' in message or 'python' in message.lower() or '编程' in message or 'code' in message.lower() or 'program' in message.lower()): suggested_plugins.append('code') # 检测是否需要3D可视化插件 if 'visualization' in plugins and ('3d' in message.lower() or '可视化' in message or '图形' in message): suggested_plugins.append('visualization') # 检测是否需要思维导图插件 if 'mindmap' in plugins and ('思维导图' in message or 'mindmap' in message.lower()): suggested_plugins.append('mindmap') # 记录活动(添加此部分代码) if session.get('logged_in'): username = session.get('username') # 记录对话活动 record_student_activity( username=username, activity_type='chat', title=f'与 {agent_config.get("name", "AI助手")} 进行了对话', agent_id=agent_id, agent_name=agent_config.get('name') ) # 如果使用了插件,记录相应的插件活动 if 'code' in suggested_plugins: record_student_activity( username=username, activity_type='code', title='执行了Python代码', agent_id=agent_id, agent_name=agent_config.get('name') ) if 'visualization' in suggested_plugins: record_student_activity( username=username, activity_type='viz', title='查看了3D可视化图形', agent_id=agent_id, agent_name=agent_config.get('name') ) if 'mindmap' in suggested_plugins: record_student_activity( username=username, activity_type='mindmap', title='生成了思维导图', agent_id=agent_id, agent_name=agent_config.get('name') ) # 检查是否有配置知识库 if not knowledge_bases: # 没有知识库,直接使用模型进行回答 print(f"\n=== 处理查询: {message} (无知识库) ===") # 使用空的文档列表调用生成器进行回答 final_response = "" for chunk in generator.generate_stream(message, []): if isinstance(chunk, dict): continue # 跳过处理数据 final_response += chunk # 返回生成的回答 return jsonify({ "success": True, "message": final_response, "tools": suggested_plugins }) # 有知识库配置,执行知识库查询流程 try: # 导入RAG系统组件 from modules.knowledge_base.retriever import Retriever from modules.knowledge_base.reranker import Reranker retriever = Retriever() reranker = Reranker() # 构建工具定义 - 将所有知识库作为工具 tools = [] # 创建工具名称到索引的映射 tool_to_index = {} for i, index in enumerate(knowledge_bases): display_name = index[4:] if index.startswith('rag_') else index # 判断是否是视频知识库 is_video = "视频" in display_name or "video" in display_name.lower() # 根据内容类型生成适当的工具名称 if is_video: tool_name = f"video_knowledge_base_{i+1}" description = f"在'{display_name}'视频知识库中搜索,返回带时间戳的视频链接。适用于需要视频讲解的问题。" else: tool_name = f"knowledge_base_{i+1}" description = f"在'{display_name}'知识库中搜索专业知识、概念和原理。适用于需要文本说明的问题。" # 添加工具名到索引的映射 tool_to_index[tool_name] = index tools.append({ "type": "function", "function": { "name": tool_name, "description": description, "parameters": { "type": "object", "properties": { "keywords": { "type": "array", "items": {"type": "string"}, "description": "搜索的关键词列表" } }, "required": ["keywords"], "additionalProperties": False }, "strict": True } }) # 第一阶段:工具选择决策 print(f"\n=== 处理查询: {message} ===") tool_calls = generator.extract_keywords_with_tools(message, tools) # 如果不需要调用工具,直接回答 if not tool_calls: print("未检测到需要使用知识库,直接回答") final_response = "" for chunk in generator.generate_stream(message, []): if isinstance(chunk, dict): continue # 跳过处理数据 final_response += chunk return jsonify({ "success": True, "message": final_response, "tools": suggested_plugins }) # 收集来自工具执行的所有文档 all_docs = [] # 执行每个工具调用 for tool_call in tool_calls: try: tool_name = tool_call["function"]["name"] actual_index = tool_to_index.get(tool_name) if not actual_index: print(f"找不到工具名称 '{tool_name}' 对应的索引") continue print(f"\n执行工具 '{tool_name}' -> 使用索引 '{actual_index}'") arguments = json.loads(tool_call["function"]["arguments"]) keywords = " ".join(arguments.get("keywords", [])) if not keywords: print("没有提供关键词,跳过检索") continue print(f"检索关键词: {keywords}") # 执行检索 retrieved_docs, _ = retriever.retrieve(keywords, specific_index=actual_index) print(f"检索到 {len(retrieved_docs)} 个文档") # 重排序文档 reranked_docs = reranker.rerank(message, retrieved_docs, actual_index) print(f"重排序完成,排序后有 {len(reranked_docs)} 个文档") # 添加结果 all_docs.extend(reranked_docs) except Exception as e: print(f"执行工具 '{tool_call.get('function', {}).get('name', '未知')}' 调用时出错: {str(e)}") import traceback traceback.print_exc() # 如果没有检索到任何文档,直接回答 if not all_docs: print("未检索到任何相关文档,直接回答") final_response = "" for chunk in generator.generate_stream(message, []): if isinstance(chunk, dict): continue # 跳过处理数据 final_response += chunk return jsonify({ "success": True, "message": final_response, "tools": suggested_plugins }) # 按相关性排序 all_docs.sort(key=lambda x: x.get('rerank_score', 0), reverse=True) print(f"\n最终收集到 {len(all_docs)} 个文档用于生成回答") # 提取参考信息 references = [] for i, doc in enumerate(all_docs[:3], 1): # 只展示前3个参考来源 file_name = doc['metadata'].get('file_name', '未知文件') content = doc['content'] # 提取大约前100字符作为摘要 summary = content[:100] + ('...' if len(content) > 100 else '') references.append({ 'index': i, 'file_name': file_name, 'content': content, 'summary': summary }) # 第二阶段:生成最终答案 final_response = "" for chunk in generator.generate_stream(message, all_docs): if isinstance(chunk, dict): continue # 跳过处理数据 final_response += chunk # 构建回复 return jsonify({ "success": True, "message": final_response, "tools": suggested_plugins, "references": references }) except Exception as e: import traceback traceback.print_exc() return jsonify({ "success": False, "message": f"处理查询时出错: {str(e)}" }), 500 except Exception as e: import traceback traceback.print_exc() return jsonify({"success": False, "message": str(e)}), 500 # API端点:获取学生活动记录 @app.route('/api/student/activities', methods=['GET']) @student_required def get_student_activities(): """获取学生活动记录""" try: username = session.get('username') # 获取该学生的活动记录 activities = student_activities.get(username, []) # 格式化输出 formatted_activities = [] for activity in activities: # 格式化时间显示 timestamp = activity['timestamp'] current_time = int(time.time()) if current_time - timestamp < 86400: # 24小时内 if current_time - timestamp < 3600: # 1小时内 time_text = f"{(current_time - timestamp) // 60}分钟前" else: time_text = f"今天 {time.strftime('%H:%M', time.localtime(timestamp))}" elif current_time - timestamp < 172800: # 48小时内 time_text = f"昨天 {time.strftime('%H:%M', time.localtime(timestamp))}" else: time_text = time.strftime('%m月%d日 %H:%M', time.localtime(timestamp)) formatted_activities.append({ "type": activity['type'], "title": activity['title'], "time": time_text, "agent_id": activity.get('agent_id'), "agent_name": activity.get('agent_name') }) return jsonify({ "success": True, "activities": formatted_activities }) except Exception as e: import traceback traceback.print_exc() return jsonify({ "success": False, "message": str(e) }), 500 # API端点:验证访问令牌 @app.route('/api/verify_token', methods=['POST']) def verify_token(): """验证访问令牌有效性""" try: data = request.json token = data.get('token', '') agent_id = data.get('agent_id', '') if not token: return jsonify({ "success": False, "message": "未提供访问令牌" }), 400 # 如果提供了agent_id,验证特定Agent的令牌 if agent_id: agent_path = os.path.join('agents', f"{agent_id}.json") if not os.path.exists(agent_path): return jsonify({ "success": False, "message": "Agent不存在" }), 404 with open(agent_path, 'r', encoding='utf-8') as f: agent_config = json.load(f) # 验证令牌 if "distributions" in agent_config: for dist in agent_config["distributions"]: if dist.get("token") == token: # 检查是否过期 if dist.get("expires_at", 0) > 0 and dist.get("expires_at", 0) < time.time(): return jsonify({ "success": False, "message": "访问令牌已过期" }) return jsonify({ "success": True, "agent": { "id": agent_id, "name": agent_config.get('name', 'AI学习助手'), "description": agent_config.get('description', ''), "subject": agent_config.get('subject', ''), "instructor": agent_config.get('instructor', '教师') } }) return jsonify({ "success": False, "message": "访问令牌无效" }) # 如果没有提供agent_id,搜索所有Agent valid_agent = None for filename in os.listdir('agents'): if filename.endswith('.json'): agent_path = os.path.join('agents', filename) with open(agent_path, 'r', encoding='utf-8') as f: agent_config = json.load(f) # 验证令牌 if "distributions" in agent_config: for dist in agent_config["distributions"]: if dist.get("token") == token: # 检查是否过期 if dist.get("expires_at", 0) > 0 and dist.get("expires_at", 0) < time.time(): continue valid_agent = { "id": agent_config.get('id'), "name": agent_config.get('name', 'AI学习助手'), "description": agent_config.get('description', ''), "subject": agent_config.get('subject', ''), "instructor": agent_config.get('instructor', '教师') } break if valid_agent: break if valid_agent: return jsonify({ "success": True, "agent": valid_agent }) return jsonify({ "success": False, "message": "未找到匹配的访问令牌" }) except Exception as e: import traceback traceback.print_exc() return jsonify({ "success": False, "message": f"验证访问令牌时出错: {str(e)}" }), 500 # API端点:获取学生的Agent列表 @app.route('/api/student/agents', methods=['GET']) @student_required def get_student_agents(): """获取学生可访问的Agent列表""" try: # 实际应用中应根据学生ID过滤 # 这里简化为获取所有Agent agents = [] for filename in os.listdir('agents'): if filename.endswith('.json'): agent_path = os.path.join('agents', filename) with open(agent_path, 'r', encoding='utf-8') as f: agent_config = json.load(f) # 简化信息 agent_info = { "id": agent_config.get('id'), "name": agent_config.get('name', 'AI学习助手'), "description": agent_config.get('description', ''), "subject": agent_config.get('subject', ''), "instructor": agent_config.get('instructor', '教师'), "plugins": agent_config.get('plugins', []), "last_used": agent_config.get('stats', {}).get('last_used') } # 添加TOKEN(实际应用中应严格控制令牌访问) if "distributions" in agent_config and agent_config["distributions"]: # 仅添加第一个分发的令牌 agent_info["token"] = agent_config["distributions"][0].get("token") agents.append(agent_info) # 按最后使用时间排序 agents.sort(key=lambda x: x.get('last_used', 0) or 0, reverse=True) return jsonify({ "success": True, "agents": agents }) except Exception as e: import traceback traceback.print_exc() return jsonify({ "success": False, "message": str(e) }), 500 if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=7860)