chats.py 14 KB

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