mongo.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. from pymongo import MongoClient
  2. from bson import ObjectId
  3. from datetime import datetime
  4. from dotenv import load_dotenv
  5. import os
  6. load_dotenv()
  7. MONGO_URI = os.getenv("ARK_LOGS_MONGO_URI")
  8. client = MongoClient(MONGO_URI, serverSelectionTimeoutMS=5000)
  9. # 数据库
  10. db = client["arklogs"]
  11. # 豆包大模型的对话日志
  12. chat_logs = db["chat_logs"]
  13. # 聊天历史记录
  14. chat_history_col = db["chat_history"]
  15. # 兴趣圈集合
  16. circle_prompts = db["circle_prompt"]
  17. # 历史人物集合
  18. historical_figures = db["historical_figures"]
  19. def _ensure_index():
  20. try:
  21. chat_logs.create_index([("user_id", 1), ("asked_at", -1)])
  22. chat_history_col.create_index([("user_id", 1), ("session_id", 1), ("timestamp", -1)])
  23. except Exception:
  24. pass
  25. def save_chat_log(
  26. user_id: str,
  27. question: str,
  28. stream_mode: bool,
  29. raw_response: str = None,
  30. status: str = "success",
  31. error: str = None,
  32. ):
  33. """
  34. 保存聊天原始响应日志到 MongoDB
  35. Args:
  36. user_id: 提问人
  37. question: 提问的问题
  38. stream_mode: 回答方式(流式或非流式)
  39. raw_response: API 原始响应的 repr 字符串
  40. status: 响应状态 success | error
  41. error: 异常时的错误信息
  42. """
  43. try:
  44. _ensure_index()
  45. chat_logs.insert_one({
  46. "user_id": user_id,
  47. "question": question,
  48. "stream_mode": stream_mode,
  49. "raw_response": raw_response,
  50. "status": status,
  51. "error": error,
  52. "asked_at": datetime.now(),
  53. })
  54. except Exception as e:
  55. print(f"MongoDB 日志写入失败: {e}")
  56. def save_chat_history(
  57. user_id: str,
  58. session_id: str,
  59. role: str,
  60. content: str,
  61. timestamp: datetime,
  62. response_id: str = None,
  63. thinking: str = None,
  64. searching: str = None,
  65. ):
  66. try:
  67. _ensure_index()
  68. chat_history_col.insert_one({
  69. "user_id": user_id,
  70. "session_id": session_id,
  71. "role": role,
  72. "content": content,
  73. "thinking": thinking,
  74. "searching": searching,
  75. "response_id": response_id,
  76. "timestamp": timestamp,
  77. })
  78. except Exception as e:
  79. print(f"MongoDB 聊天历史写入失败: {e}")
  80. def get_chat_history(user_id: str, session_id: str) -> list:
  81. try:
  82. docs = chat_history_col.find(
  83. {"user_id": user_id, "session_id": session_id},
  84. {"_id": 0}
  85. ).sort("timestamp", 1)
  86. return list(docs)
  87. except Exception as e:
  88. print(f"MongoDB 聊天历史读取失败: {e}")
  89. return []
  90. def get_last_response_id(user_id: str, session_id: str) -> str | None:
  91. try:
  92. doc = chat_history_col.find_one(
  93. {"user_id": user_id, "session_id": session_id, "role": "assistant", "response_id": {"$ne": None}},
  94. {"response_id": 1, "_id": 0},
  95. sort=[("timestamp", -1)]
  96. )
  97. return doc["response_id"] if doc else None
  98. except Exception as e:
  99. print(f"MongoDB 查询 response_id 失败: {e}")
  100. return None
  101. def delete_chat_history(user_id: str, session_id: str) -> int:
  102. try:
  103. result = chat_history_col.delete_many({"user_id": user_id, "session_id": session_id})
  104. return result.deleted_count
  105. except Exception as e:
  106. print(f"MongoDB 聊天历史删除失败: {e}")
  107. return 0
  108. def get_sessions(user_id: str) -> list:
  109. try:
  110. pipeline = [
  111. {"$match": {"user_id": user_id, "role": "user"}},
  112. {"$sort": {"timestamp": 1}},
  113. {"$group": {
  114. "_id": "$session_id",
  115. "createdAt": {"$first": "$timestamp"},
  116. "preview": {"$first": "$content"},
  117. }},
  118. {"$sort": {"createdAt": -1}},
  119. {"$project": {
  120. "_id": 0,
  121. "sessionId": "$_id",
  122. "createdAt": 1,
  123. "preview": {"$substrCP": ["$preview", 0, 20]},
  124. }},
  125. ]
  126. return list(chat_history_col.aggregate(pipeline))
  127. except Exception as e:
  128. print(f"MongoDB 会话列表查询失败: {e}")
  129. return []
  130. _DEFAULT_PROMPT_CONFIG = {
  131. "name": "兴趣圈",
  132. "role": "活跃用户",
  133. "style": "自然亲切,有活人感",
  134. "keywords": [],
  135. "forbidden": [],
  136. }
  137. def get_circle_prompt(app_name: str) -> dict:
  138. try:
  139. doc = circle_prompts.find_one({"appName": app_name})
  140. return doc if doc else _DEFAULT_PROMPT_CONFIG
  141. except Exception:
  142. return _DEFAULT_PROMPT_CONFIG
  143. def upsert_circle_prompt(data: dict) -> None:
  144. circle_prompts.update_one(
  145. {"appName": data["appName"]},
  146. {"$set": data},
  147. upsert=True,
  148. )
  149. # ===================== 历史人物 =====================
  150. def get_all_figures() -> list:
  151. try:
  152. docs = historical_figures.find({})
  153. return [{"_id": str(doc["_id"]), **{k: v for k, v in doc.items() if k != "_id"}} for doc in docs]
  154. except Exception as e:
  155. print(f"MongoDB 历史人物列表查询失败: {e}")
  156. return []
  157. def get_figure_by_id(figure_id: str) -> dict | None:
  158. try:
  159. doc = historical_figures.find_one({"_id": ObjectId(figure_id)})
  160. if doc:
  161. doc["_id"] = str(doc["_id"])
  162. return doc
  163. except Exception as e:
  164. print(f"MongoDB 历史人物查询失败: {e}")
  165. return None
  166. def insert_figure(data: dict) -> str:
  167. try:
  168. result = historical_figures.insert_one(data)
  169. return str(result.inserted_id)
  170. except Exception as e:
  171. print(f"MongoDB 历史人物新增失败: {e}")
  172. return None
  173. def update_figure(figure_id: str, data: dict) -> int:
  174. try:
  175. result = historical_figures.update_one(
  176. {"_id": ObjectId(figure_id)},
  177. {"$set": data},
  178. )
  179. return result.matched_count
  180. except Exception as e:
  181. print(f"MongoDB 历史人物修改失败: {e}")
  182. return 0
  183. def delete_figure(figure_id: str) -> int:
  184. try:
  185. result = historical_figures.delete_one({"_id": ObjectId(figure_id)})
  186. return result.deleted_count
  187. except Exception as e:
  188. print(f"MongoDB 历史人物删除失败: {e}")
  189. return 0