Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/charlemagnefeel_chat-proxy.json
T

1 line
6.8 KiB
JSON
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{"repo": "charlemagnefeel/chat-proxy", "files": {"app.py": {"0d87b7f2b768cc8d3d6946bd5c0eff5a65078c10": {"t": 1785674052.8993745, "n": 5797, "h": "84e3af75bf18b2ae", "c": "from flask import Flask, request, Response\r\nimport requests\r\nimport re\r\n\r\napp = Flask(__name__)\r\n\r\nAPI_URL = \"https://xiaoai.plus/v1/chat/completions\"\r\nAPI_KEY = \"sk-wbdkdE8VNbd8d0TqEItWrHxGlWRyPZbyzrc6v71OwYvEUM2x\"\r\n\r\nconversation_history = {}\r\n\r\ndef is_reset_command(text):\r\n text = text.lower().strip()\r\n return any(text.startswith(trigger) for trigger in [\"/reset\", \"重置对话\", \"清空对话\", \"reset\"])\r\n\r\ndef remove_think_content(text):\r\n return re.sub(r\"<think>.*?</think>\", \"\", text, flags=re.DOTALL)\r\n\r\ndef handle_chat(user_id, message, model, session_id, user_name=None):\r\n # ✅ deepseek-r1 和 deepseek-v3 都带修饰\r\n if model in [\"deepseek-r1\", \"deepseek-v3\"]:\r\n message = message.lstrip(\"sS\")\r\n message += \"。尽量缩短思考时间,正文限制在800字以内。\"\r\n elif model == \"gpt-4o\":\r\n message = message.lstrip(\"wW\")\r\n\r\n # 构建上下文 key\r\n if session_id.startswith(\"1-\"):\r\n key = f\"{session_id}_{user_id}_{model}\"\r\n else:\r\n key = f\"{session_id}_{model}\"\r\n\r\n if is_reset_command(message):\r\n conversation_history.pop(key, None)\r\n return \"✅ 对话已重置(触发关键词成功)\"\r\n\r\n if key not in conversation_history:\r\n if session_id.startswith(\"2-\"):\r\n conversation_history[key] = [{\r\n \"role\": \"system\",\r\n \"content\": \"当前为群聊场景,消息内容开头[]内的内容是群聊成员昵称,仅供你识别不同消息的发送者身份用,回答中无需包含该昵称。\"\r\n }]\r\n else:\r\n conversation_history[key] = []\r\n\r\n match = re.search(r\"设定:(.*)\", message)\r\n if match:\r\n setting_content = match.group(1).strip()\r\n if model in [\"deepseek-r1\", \"deepseek-v3\"]: # ✅ 支持 deepseek-v3\r\n if session_id.startswith(\"2-\") and user_name:\r\n setting_text = f\"收到来自 [{user_name}] 的设定指令:{setting_content}。该设定将应用于之后的对话。\"\r\n else:\r\n setting_text = f\"设定指令:{setting_content}。该设定将应用于之后的对话。\"\r\n conversation_history[key].append({\r\n \"role\": \"assistant\",\r\n \"content\": setting_text,\r\n \"preserve\": True\r\n })\r\n else:\r\n conversation_history[key].append({\r\n \"role\": \"system\",\r\n \"content\": setting_content,\r\n \"preserve\": True\r\n })\r\n return \"✅ 设定已保存(不会触发当前回复)\"\r\n\r\n if session_id.startswith(\"2-\") and user_name:\r\n content = f\"[{user_name}]:{message}\"\r\n else:\r\n content = message\r\n conversation_history[key].append({\"role\": \"user\", \"content\": content})\r\n\r\n max_len = 20\r\n preserved = []\r\n normal = []\r\n\r\n for msg in conversation_history[key]:\r\n if msg.get(\"preserve\"):\r\n preserved.append(msg)\r\n else:\r\n normal.append(msg)\r\n\r\n max_normal_len = max_len - len(preserved)\r\n normal = normal[-max_normal_len:] if max_normal_len > 0 else []\r\n conversation_history[key] = preserved + normal\r\n\r\n headers = {\r\n \"Content-Type\": \"application/json\",\r\n \"Authorization\": f\"Bearer {API_KEY}\"\r\n }\r\n\r\n payload = {\r\n \"model\": model,\r\n \"messages\": conversation_history[key]\r\n }\r\n\r\n try:\r\n response = requests.post(API_URL, json=payload, headers=headers, timeout=300)\r\n response.raise_for_status()\r\n result = response.json()\r\n reply = result[\"choices\"][0][\"message\"][\"content\"]\r\n\r\n if \"xiaoai\" in reply.lower():\r\n return \"服务器开小差了\"\r\n\r\n # ✅ 仅对 deepseek-r1 去除 think 标签\r\n if model == \"deepseek-r1\":\r\n reply = remove_think_content(reply)\r\n\r\n except Exception as e:\r\n return f\"❌ 发生错误:{str(e)}\"\r\n\r\n conversation_history[key].append({\"role\": \"assistant\", \"content\": reply})\r\n return reply\r\n\r\n# ✅ 支持 gpt-4o\r\n@app.route(\"/chat\", methods=[\"POST\"])\r\ndef chat():\r\n data = request.get_json()\r\n user_id = data.get(\"message_from\")\r\n message = data.get(\"message\")\r\n message_type = data.get(\"message_type\")\r\n message_to = data.get(\"message_to\")\r\n session_id = f\"{message_type}-{message_to}\" if message_type and message_to else user_id\r\n user_name = data.get(\"message_from_name\")\r\n\r\n if not user_id or not message:\r\n return Response(\"Missing user_id or message\", status=400)\r\n\r\n reply = handle_chat(user_id, message, \"gpt-4o\", session_id, user_name)\r\n return Response(reply, content_type=\"text/plain; charset=utf-8\")\r\n\r\n# ✅ 支持 deepseek-r1\r\n@app.route(\"/deepseek\", methods=[\"POST\"])\r\ndef deepseek():\r\n data = request.get_json()\r\n user_id = data.get(\"message_from\")\r\n message = data.get(\"message\")\r\n message_type = data.get(\"message_type\")\r\n message_to = data.get(\"message_to\")\r\n session_id = f\"{message_type}-{message_to}\" if message_type and message_to else user_id\r\n user_name = data.get(\"message_from_name\")\r\n\r\n if not user_id or not message:\r\n return Response(\"Missing user_id or message\", status=400)\r\n\r\n reply = handle_chat(user_id, message, \"deepseek-r1\", session_id, user_name)\r\n return Response(reply, content_type=\"text/plain; charset=utf-8\")\r\n\r\n# ✅ 新增 deepseek-v3 路由\r\n@app.route(\"/deepseekv3\", methods=[\"POST\"])\r\ndef deepseekv3():\r\n data = request.get_json()\r\n user_id = data.get(\"message_from\")\r\n message = data.get(\"message\")\r\n message_type = data.get(\"message_type\")\r\n message_to = data.get(\"message_to\")\r\n session_id = f\"{message_type}-{message_to}\" if message_type and message_to else user_id\r\n user_name = data.get(\"message_from_name\")\r\n\r\n if not user_id or not message:\r\n return Response(\"Missing user_id or message\", status=400)\r\n\r\n reply = handle_chat(user_id, message, \"deepseek-v3\", session_id, user_name)\r\n return Response(reply, content_type=\"text/plain; charset=utf-8\")\r\n\r\nif __name__ == '__main__':\r\n import os\r\n port = int(os.environ.get(\"PORT\", 10000))\r\n app.run(host=\"0.0.0.0\", port=port)\r\n"}}}}