chats.py 16 KB

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