chats.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. import json
  2. import logging
  3. from typing import Optional
  4. from open_webui.apps.webui.models.chats import (
  5. ChatForm,
  6. ChatResponse,
  7. Chats,
  8. ChatTitleIdResponse,
  9. )
  10. from open_webui.apps.webui.models.tags import (
  11. ChatIdTagForm,
  12. ChatIdTagModel,
  13. TagModel,
  14. Tags,
  15. )
  16. from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT
  17. from open_webui.constants import ERROR_MESSAGES
  18. from open_webui.env import SRC_LOG_LEVELS
  19. from fastapi import APIRouter, Depends, HTTPException, Request, status
  20. from pydantic import BaseModel
  21. from open_webui.utils.utils import get_admin_user, get_verified_user
  22. log = logging.getLogger(__name__)
  23. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  24. router = APIRouter()
  25. ############################
  26. # GetChatList
  27. ############################
  28. @router.get("/", response_model=list[ChatTitleIdResponse])
  29. @router.get("/list", response_model=list[ChatTitleIdResponse])
  30. async def get_session_user_chat_list(
  31. user=Depends(get_verified_user), page: Optional[int] = None
  32. ):
  33. if page is not None:
  34. limit = 60
  35. skip = (page - 1) * limit
  36. return Chats.get_chat_title_id_list_by_user_id(user.id, skip=skip, limit=limit)
  37. else:
  38. return Chats.get_chat_title_id_list_by_user_id(user.id)
  39. ############################
  40. # DeleteAllChats
  41. ############################
  42. @router.delete("/", response_model=bool)
  43. async def delete_all_user_chats(request: Request, user=Depends(get_verified_user)):
  44. if user.role == "user" and not request.app.state.config.USER_PERMISSIONS.get(
  45. "chat", {}
  46. ).get("deletion", {}):
  47. raise HTTPException(
  48. status_code=status.HTTP_401_UNAUTHORIZED,
  49. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  50. )
  51. result = Chats.delete_chats_by_user_id(user.id)
  52. return result
  53. ############################
  54. # GetUserChatList
  55. ############################
  56. @router.get("/list/user/{user_id}", response_model=list[ChatTitleIdResponse])
  57. async def get_user_chat_list_by_user_id(
  58. user_id: str,
  59. user=Depends(get_admin_user),
  60. skip: int = 0,
  61. limit: int = 50,
  62. ):
  63. if not ENABLE_ADMIN_CHAT_ACCESS:
  64. raise HTTPException(
  65. status_code=status.HTTP_401_UNAUTHORIZED,
  66. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  67. )
  68. return Chats.get_chat_list_by_user_id(
  69. user_id, include_archived=True, skip=skip, limit=limit
  70. )
  71. ############################
  72. # CreateNewChat
  73. ############################
  74. @router.post("/new", response_model=Optional[ChatResponse])
  75. async def create_new_chat(form_data: ChatForm, user=Depends(get_verified_user)):
  76. try:
  77. chat = Chats.insert_new_chat(user.id, form_data)
  78. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  79. except Exception as e:
  80. log.exception(e)
  81. raise HTTPException(
  82. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  83. )
  84. ############################
  85. # GetChats
  86. ############################
  87. @router.get("/all", response_model=list[ChatResponse])
  88. async def get_user_chats(user=Depends(get_verified_user)):
  89. return [
  90. ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  91. for chat in Chats.get_chats_by_user_id(user.id)
  92. ]
  93. ############################
  94. # GetArchivedChats
  95. ############################
  96. @router.get("/all/archived", response_model=list[ChatResponse])
  97. async def get_user_archived_chats(user=Depends(get_verified_user)):
  98. return [
  99. ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  100. for chat in Chats.get_archived_chats_by_user_id(user.id)
  101. ]
  102. ############################
  103. # GetAllChatsInDB
  104. ############################
  105. @router.get("/all/db", response_model=list[ChatResponse])
  106. async def get_all_user_chats_in_db(user=Depends(get_admin_user)):
  107. if not ENABLE_ADMIN_EXPORT:
  108. raise HTTPException(
  109. status_code=status.HTTP_401_UNAUTHORIZED,
  110. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  111. )
  112. return [
  113. ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  114. for chat in Chats.get_chats()
  115. ]
  116. ############################
  117. # GetArchivedChats
  118. ############################
  119. @router.get("/archived", response_model=list[ChatTitleIdResponse])
  120. async def get_archived_session_user_chat_list(
  121. user=Depends(get_verified_user), skip: int = 0, limit: int = 50
  122. ):
  123. return Chats.get_archived_chat_list_by_user_id(user.id, skip, limit)
  124. ############################
  125. # ArchiveAllChats
  126. ############################
  127. @router.post("/archive/all", response_model=bool)
  128. async def archive_all_chats(user=Depends(get_verified_user)):
  129. return Chats.archive_all_chats_by_user_id(user.id)
  130. ############################
  131. # GetSharedChatById
  132. ############################
  133. @router.get("/share/{share_id}", response_model=Optional[ChatResponse])
  134. async def get_shared_chat_by_id(share_id: str, user=Depends(get_verified_user)):
  135. if user.role == "pending":
  136. raise HTTPException(
  137. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  138. )
  139. if user.role == "user" or (user.role == "admin" and not ENABLE_ADMIN_CHAT_ACCESS):
  140. chat = Chats.get_chat_by_share_id(share_id)
  141. elif user.role == "admin" and ENABLE_ADMIN_CHAT_ACCESS:
  142. chat = Chats.get_chat_by_id(share_id)
  143. if chat:
  144. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  145. else:
  146. raise HTTPException(
  147. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  148. )
  149. ############################
  150. # GetChatsByTags
  151. ############################
  152. class TagNameForm(BaseModel):
  153. name: str
  154. skip: Optional[int] = 0
  155. limit: Optional[int] = 50
  156. @router.post("/tags", response_model=list[ChatTitleIdResponse])
  157. async def get_user_chat_list_by_tag_name(
  158. form_data: TagNameForm, user=Depends(get_verified_user)
  159. ):
  160. chat_ids = [
  161. chat_id_tag.chat_id
  162. for chat_id_tag in Tags.get_chat_ids_by_tag_name_and_user_id(
  163. form_data.name, user.id
  164. )
  165. ]
  166. chats = Chats.get_chat_list_by_chat_ids(chat_ids, form_data.skip, form_data.limit)
  167. if len(chats) == 0:
  168. Tags.delete_tag_by_tag_name_and_user_id(form_data.name, user.id)
  169. return chats
  170. ############################
  171. # GetAllTags
  172. ############################
  173. @router.get("/tags/all", response_model=list[TagModel])
  174. async def get_all_tags(user=Depends(get_verified_user)):
  175. try:
  176. tags = Tags.get_tags_by_user_id(user.id)
  177. return tags
  178. except Exception as e:
  179. log.exception(e)
  180. raise HTTPException(
  181. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  182. )
  183. ############################
  184. # GetChatById
  185. ############################
  186. @router.get("/{id}", response_model=Optional[ChatResponse])
  187. async def get_chat_by_id(id: str, user=Depends(get_verified_user)):
  188. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  189. if chat:
  190. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  191. else:
  192. raise HTTPException(
  193. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  194. )
  195. ############################
  196. # UpdateChatById
  197. ############################
  198. @router.post("/{id}", response_model=Optional[ChatResponse])
  199. async def update_chat_by_id(
  200. id: str, form_data: ChatForm, user=Depends(get_verified_user)
  201. ):
  202. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  203. if chat:
  204. updated_chat = {**json.loads(chat.chat), **form_data.chat}
  205. chat = Chats.update_chat_by_id(id, updated_chat)
  206. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  207. else:
  208. raise HTTPException(
  209. status_code=status.HTTP_401_UNAUTHORIZED,
  210. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  211. )
  212. ############################
  213. # DeleteChatById
  214. ############################
  215. @router.delete("/{id}", response_model=bool)
  216. async def delete_chat_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  217. if user.role == "admin":
  218. result = Chats.delete_chat_by_id(id)
  219. return result
  220. else:
  221. if not request.app.state.config.USER_PERMISSIONS.get("chat", {}).get(
  222. "deletion", {}
  223. ):
  224. raise HTTPException(
  225. status_code=status.HTTP_401_UNAUTHORIZED,
  226. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  227. )
  228. result = Chats.delete_chat_by_id_and_user_id(id, user.id)
  229. return result
  230. ############################
  231. # CloneChat
  232. ############################
  233. @router.get("/{id}/clone", response_model=Optional[ChatResponse])
  234. async def clone_chat_by_id(id: str, user=Depends(get_verified_user)):
  235. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  236. if chat:
  237. chat_body = json.loads(chat.chat)
  238. updated_chat = {
  239. **chat_body,
  240. "originalChatId": chat.id,
  241. "branchPointMessageId": chat_body["history"]["currentId"],
  242. "title": f"Clone of {chat.title}",
  243. }
  244. chat = Chats.insert_new_chat(user.id, ChatForm(**{"chat": updated_chat}))
  245. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  246. else:
  247. raise HTTPException(
  248. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  249. )
  250. ############################
  251. # ArchiveChat
  252. ############################
  253. @router.get("/{id}/archive", response_model=Optional[ChatResponse])
  254. async def archive_chat_by_id(id: str, user=Depends(get_verified_user)):
  255. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  256. if chat:
  257. chat = Chats.toggle_chat_archive_by_id(id)
  258. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  259. else:
  260. raise HTTPException(
  261. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  262. )
  263. ############################
  264. # ShareChatById
  265. ############################
  266. @router.post("/{id}/share", response_model=Optional[ChatResponse])
  267. async def share_chat_by_id(id: str, user=Depends(get_verified_user)):
  268. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  269. if chat:
  270. if chat.share_id:
  271. shared_chat = Chats.update_shared_chat_by_chat_id(chat.id)
  272. return ChatResponse(
  273. **{**shared_chat.model_dump(), "chat": json.loads(shared_chat.chat)}
  274. )
  275. shared_chat = Chats.insert_shared_chat_by_chat_id(chat.id)
  276. if not shared_chat:
  277. raise HTTPException(
  278. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  279. detail=ERROR_MESSAGES.DEFAULT(),
  280. )
  281. return ChatResponse(
  282. **{**shared_chat.model_dump(), "chat": json.loads(shared_chat.chat)}
  283. )
  284. else:
  285. raise HTTPException(
  286. status_code=status.HTTP_401_UNAUTHORIZED,
  287. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  288. )
  289. ############################
  290. # DeletedSharedChatById
  291. ############################
  292. @router.delete("/{id}/share", response_model=Optional[bool])
  293. async def delete_shared_chat_by_id(id: str, user=Depends(get_verified_user)):
  294. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  295. if chat:
  296. if not chat.share_id:
  297. return False
  298. result = Chats.delete_shared_chat_by_chat_id(id)
  299. update_result = Chats.update_chat_share_id_by_id(id, None)
  300. return result and update_result != None
  301. else:
  302. raise HTTPException(
  303. status_code=status.HTTP_401_UNAUTHORIZED,
  304. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  305. )
  306. ############################
  307. # GetChatTagsById
  308. ############################
  309. @router.get("/{id}/tags", response_model=list[TagModel])
  310. async def get_chat_tags_by_id(id: str, user=Depends(get_verified_user)):
  311. tags = Tags.get_tags_by_chat_id_and_user_id(id, user.id)
  312. if tags != None:
  313. return tags
  314. else:
  315. raise HTTPException(
  316. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  317. )
  318. ############################
  319. # AddChatTagById
  320. ############################
  321. @router.post("/{id}/tags", response_model=Optional[ChatIdTagModel])
  322. async def add_chat_tag_by_id(
  323. id: str, form_data: ChatIdTagForm, user=Depends(get_verified_user)
  324. ):
  325. tags = Tags.get_tags_by_chat_id_and_user_id(id, user.id)
  326. if form_data.tag_name not in tags:
  327. tag = Tags.add_tag_to_chat(user.id, form_data)
  328. if tag:
  329. return tag
  330. else:
  331. raise HTTPException(
  332. status_code=status.HTTP_401_UNAUTHORIZED,
  333. detail=ERROR_MESSAGES.NOT_FOUND,
  334. )
  335. else:
  336. raise HTTPException(
  337. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  338. )
  339. ############################
  340. # DeleteChatTagById
  341. ############################
  342. @router.delete("/{id}/tags", response_model=Optional[bool])
  343. async def delete_chat_tag_by_id(
  344. id: str, form_data: ChatIdTagForm, user=Depends(get_verified_user)
  345. ):
  346. result = Tags.delete_tag_by_tag_name_and_chat_id_and_user_id(
  347. form_data.tag_name, id, user.id
  348. )
  349. if result:
  350. return result
  351. else:
  352. raise HTTPException(
  353. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  354. )
  355. ############################
  356. # DeleteAllChatTagsById
  357. ############################
  358. @router.delete("/{id}/tags/all", response_model=Optional[bool])
  359. async def delete_all_chat_tags_by_id(id: str, user=Depends(get_verified_user)):
  360. result = Tags.delete_tags_by_chat_id_and_user_id(id, user.id)
  361. if result:
  362. return result
  363. else:
  364. raise HTTPException(
  365. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  366. )