mongo.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. attachments: list = None,
  66. ):
  67. try:
  68. _ensure_index()
  69. doc = {
  70. "user_id": user_id,
  71. "session_id": session_id,
  72. "role": role,
  73. "content": content,
  74. "thinking": thinking,
  75. "searching": searching,
  76. "response_id": response_id,
  77. "timestamp": timestamp,
  78. }
  79. if attachments:
  80. doc["attachments"] = attachments
  81. chat_history_col.insert_one(doc)
  82. except Exception as e:
  83. print(f"MongoDB 聊天历史写入失败: {e}")
  84. def get_chat_history(user_id: str, session_id: str) -> list:
  85. try:
  86. docs = chat_history_col.find(
  87. {"user_id": user_id, "session_id": session_id},
  88. {"_id": 0}
  89. ).sort("timestamp", 1)
  90. return list(docs)
  91. except Exception as e:
  92. print(f"MongoDB 聊天历史读取失败: {e}")
  93. return []
  94. def get_last_response_id(user_id: str, session_id: str) -> str | None:
  95. try:
  96. doc = chat_history_col.find_one(
  97. {"user_id": user_id, "session_id": session_id, "role": "assistant", "response_id": {"$ne": None}},
  98. {"response_id": 1, "_id": 0},
  99. sort=[("timestamp", -1)]
  100. )
  101. return doc["response_id"] if doc else None
  102. except Exception as e:
  103. print(f"MongoDB 查询 response_id 失败: {e}")
  104. return None
  105. def delete_chat_history(user_id: str, session_id: str) -> int:
  106. try:
  107. result = chat_history_col.delete_many({"user_id": user_id, "session_id": session_id})
  108. return result.deleted_count
  109. except Exception as e:
  110. print(f"MongoDB 聊天历史删除失败: {e}")
  111. return 0
  112. def get_sessions(user_id: str) -> list:
  113. try:
  114. pipeline = [
  115. {"$match": {"user_id": user_id, "role": "user"}},
  116. {"$sort": {"timestamp": 1}},
  117. {"$group": {
  118. "_id": "$session_id",
  119. "createdAt": {"$first": "$timestamp"},
  120. "preview": {"$first": "$content"},
  121. }},
  122. {"$sort": {"createdAt": -1}},
  123. {"$project": {
  124. "_id": 0,
  125. "sessionId": "$_id",
  126. "createdAt": 1,
  127. "preview": {"$substrCP": ["$preview", 0, 20]},
  128. }},
  129. ]
  130. return list(chat_history_col.aggregate(pipeline))
  131. except Exception as e:
  132. print(f"MongoDB 会话列表查询失败: {e}")
  133. return []
  134. _DEFAULT_PROMPT_CONFIG = {
  135. "name": "兴趣圈",
  136. "role": "活跃用户",
  137. "style": "自然亲切,有活人感",
  138. "keywords": [],
  139. "forbidden": [],
  140. }
  141. def get_circle_prompt(app_name: str) -> dict:
  142. try:
  143. doc = circle_prompts.find_one({"appName": app_name})
  144. return doc if doc else _DEFAULT_PROMPT_CONFIG
  145. except Exception:
  146. return _DEFAULT_PROMPT_CONFIG
  147. def upsert_circle_prompt(data: dict) -> None:
  148. circle_prompts.update_one(
  149. {"appName": data["appName"]},
  150. {"$set": data},
  151. upsert=True,
  152. )
  153. # ===================== 历史人物 =====================
  154. def get_all_figures() -> list:
  155. try:
  156. docs = historical_figures.find({})
  157. return [{"_id": str(doc["_id"]), **{k: v for k, v in doc.items() if k != "_id"}} for doc in docs]
  158. except Exception as e:
  159. print(f"MongoDB 历史人物列表查询失败: {e}")
  160. return []
  161. def get_figure_by_id(figure_id: str) -> dict | None:
  162. try:
  163. doc = historical_figures.find_one({"_id": ObjectId(figure_id)})
  164. if doc:
  165. doc["_id"] = str(doc["_id"])
  166. return doc
  167. except Exception as e:
  168. print(f"MongoDB 历史人物查询失败: {e}")
  169. return None
  170. def insert_figure(data: dict) -> str:
  171. try:
  172. result = historical_figures.insert_one(data)
  173. return str(result.inserted_id)
  174. except Exception as e:
  175. print(f"MongoDB 历史人物新增失败: {e}")
  176. return None
  177. def update_figure(figure_id: str, data: dict) -> int:
  178. try:
  179. result = historical_figures.update_one(
  180. {"_id": ObjectId(figure_id)},
  181. {"$set": data},
  182. )
  183. return result.matched_count
  184. except Exception as e:
  185. print(f"MongoDB 历史人物修改失败: {e}")
  186. return 0
  187. def delete_figure(figure_id: str) -> int:
  188. try:
  189. result = historical_figures.delete_one({"_id": ObjectId(figure_id)})
  190. return result.deleted_count
  191. except Exception as e:
  192. print(f"MongoDB 历史人物删除失败: {e}")
  193. return 0