chats.py 14 KB

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