chats.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. import json
  2. import logging
  3. from typing import Optional
  4. from open_webui.models.chats import (
  5. ChatForm,
  6. ChatImportForm,
  7. ChatResponse,
  8. Chats,
  9. ChatTitleIdResponse,
  10. )
  11. from open_webui.models.tags import TagModel, Tags
  12. from open_webui.models.folders import Folders
  13. from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT
  14. from open_webui.constants import ERROR_MESSAGES
  15. from open_webui.env import SRC_LOG_LEVELS
  16. from fastapi import APIRouter, Depends, HTTPException, Request, status
  17. from pydantic import BaseModel
  18. from open_webui.utils.auth import get_admin_user, get_verified_user
  19. from open_webui.utils.access_control import has_permission
  20. log = logging.getLogger(__name__)
  21. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  22. router = APIRouter()
  23. ############################
  24. # GetChatList
  25. ############################
  26. @router.get("/", response_model=list[ChatTitleIdResponse])
  27. @router.get("/list", response_model=list[ChatTitleIdResponse])
  28. async def get_session_user_chat_list(
  29. user=Depends(get_verified_user), page: Optional[int] = None
  30. ):
  31. if page is not None:
  32. limit = 60
  33. skip = (page - 1) * limit
  34. return Chats.get_chat_title_id_list_by_user_id(user.id, skip=skip, limit=limit)
  35. else:
  36. return Chats.get_chat_title_id_list_by_user_id(user.id)
  37. ############################
  38. # DeleteAllChats
  39. ############################
  40. @router.delete("/", response_model=bool)
  41. async def delete_all_user_chats(request: Request, user=Depends(get_verified_user)):
  42. if user.role == "user" and not has_permission(
  43. user.id, "chat.delete", request.app.state.config.USER_PERMISSIONS
  44. ):
  45. raise HTTPException(
  46. status_code=status.HTTP_401_UNAUTHORIZED,
  47. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  48. )
  49. result = Chats.delete_chats_by_user_id(user.id)
  50. return result
  51. ############################
  52. # GetUserChatList
  53. ############################
  54. @router.get("/list/user/{user_id}", response_model=list[ChatTitleIdResponse])
  55. async def get_user_chat_list_by_user_id(
  56. user_id: str,
  57. user=Depends(get_admin_user),
  58. skip: int = 0,
  59. limit: int = 50,
  60. ):
  61. if not ENABLE_ADMIN_CHAT_ACCESS:
  62. raise HTTPException(
  63. status_code=status.HTTP_401_UNAUTHORIZED,
  64. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  65. )
  66. return Chats.get_chat_list_by_user_id(
  67. user_id, include_archived=True, skip=skip, limit=limit
  68. )
  69. ############################
  70. # CreateNewChat
  71. ############################
  72. @router.post("/new", response_model=Optional[ChatResponse])
  73. async def create_new_chat(form_data: ChatForm, user=Depends(get_verified_user)):
  74. try:
  75. chat = Chats.insert_new_chat(user.id, form_data)
  76. return ChatResponse(**chat.model_dump())
  77. except Exception as e:
  78. log.exception(e)
  79. raise HTTPException(
  80. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  81. )
  82. ############################
  83. # ImportChat
  84. ############################
  85. @router.post("/import", response_model=Optional[ChatResponse])
  86. async def import_chat(form_data: ChatImportForm, user=Depends(get_verified_user)):
  87. try:
  88. chat = Chats.import_chat(user.id, form_data)
  89. if chat:
  90. tags = chat.meta.get("tags", [])
  91. for tag_id in tags:
  92. tag_id = tag_id.replace(" ", "_").lower()
  93. tag_name = " ".join([word.capitalize() for word in tag_id.split("_")])
  94. if (
  95. tag_id != "none"
  96. and Tags.get_tag_by_name_and_user_id(tag_name, user.id) is None
  97. ):
  98. Tags.insert_new_tag(tag_name, user.id)
  99. return ChatResponse(**chat.model_dump())
  100. except Exception as e:
  101. log.exception(e)
  102. raise HTTPException(
  103. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  104. )
  105. ############################
  106. # GetChats
  107. ############################
  108. @router.get("/search", response_model=list[ChatTitleIdResponse])
  109. async def search_user_chats(
  110. text: str, page: Optional[int] = None, user=Depends(get_verified_user)
  111. ):
  112. if page is None:
  113. page = 1
  114. limit = 60
  115. skip = (page - 1) * limit
  116. chat_list = [
  117. ChatTitleIdResponse(**chat.model_dump())
  118. for chat in Chats.get_chats_by_user_id_and_search_text(
  119. user.id, text, skip=skip, limit=limit
  120. )
  121. ]
  122. # Delete tag if no chat is found
  123. words = text.strip().split(" ")
  124. if page == 1 and len(words) == 1 and words[0].startswith("tag:"):
  125. tag_id = words[0].replace("tag:", "")
  126. if len(chat_list) == 0:
  127. if Tags.get_tag_by_name_and_user_id(tag_id, user.id):
  128. log.debug(f"deleting tag: {tag_id}")
  129. Tags.delete_tag_by_name_and_user_id(tag_id, user.id)
  130. return chat_list
  131. ############################
  132. # GetChatsByFolderId
  133. ############################
  134. @router.get("/folder/{folder_id}", response_model=list[ChatResponse])
  135. async def get_chats_by_folder_id(folder_id: str, user=Depends(get_verified_user)):
  136. folder_ids = [folder_id]
  137. children_folders = Folders.get_children_folders_by_id_and_user_id(
  138. folder_id, user.id
  139. )
  140. if children_folders:
  141. folder_ids.extend([folder.id for folder in children_folders])
  142. return [
  143. ChatResponse(**chat.model_dump())
  144. for chat in Chats.get_chats_by_folder_ids_and_user_id(folder_ids, user.id)
  145. ]
  146. ############################
  147. # GetPinnedChats
  148. ############################
  149. @router.get("/pinned", response_model=list[ChatResponse])
  150. async def get_user_pinned_chats(user=Depends(get_verified_user)):
  151. return [
  152. ChatResponse(**chat.model_dump())
  153. for chat in Chats.get_pinned_chats_by_user_id(user.id)
  154. ]
  155. ############################
  156. # GetChats
  157. ############################
  158. @router.get("/all", response_model=list[ChatResponse])
  159. async def get_user_chats(user=Depends(get_verified_user)):
  160. return [
  161. ChatResponse(**chat.model_dump())
  162. for chat in Chats.get_chats_by_user_id(user.id)
  163. ]
  164. ############################
  165. # GetArchivedChats
  166. ############################
  167. @router.get("/all/archived", response_model=list[ChatResponse])
  168. async def get_user_archived_chats(user=Depends(get_verified_user)):
  169. return [
  170. ChatResponse(**chat.model_dump())
  171. for chat in Chats.get_archived_chats_by_user_id(user.id)
  172. ]
  173. ############################
  174. # GetAllTags
  175. ############################
  176. @router.get("/all/tags", response_model=list[TagModel])
  177. async def get_all_user_tags(user=Depends(get_verified_user)):
  178. try:
  179. tags = Tags.get_tags_by_user_id(user.id)
  180. return tags
  181. except Exception as e:
  182. log.exception(e)
  183. raise HTTPException(
  184. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  185. )
  186. ############################
  187. # GetAllChatsInDB
  188. ############################
  189. @router.get("/all/db", response_model=list[ChatResponse])
  190. async def get_all_user_chats_in_db(user=Depends(get_admin_user)):
  191. if not ENABLE_ADMIN_EXPORT:
  192. raise HTTPException(
  193. status_code=status.HTTP_401_UNAUTHORIZED,
  194. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  195. )
  196. return [ChatResponse(**chat.model_dump()) for chat in Chats.get_chats()]
  197. ############################
  198. # GetArchivedChats
  199. ############################
  200. @router.get("/archived", response_model=list[ChatTitleIdResponse])
  201. async def get_archived_session_user_chat_list(
  202. user=Depends(get_verified_user), skip: int = 0, limit: int = 50
  203. ):
  204. return Chats.get_archived_chat_list_by_user_id(user.id, skip, limit)
  205. ############################
  206. # ArchiveAllChats
  207. ############################
  208. @router.post("/archive/all", response_model=bool)
  209. async def archive_all_chats(user=Depends(get_verified_user)):
  210. return Chats.archive_all_chats_by_user_id(user.id)
  211. ############################
  212. # GetSharedChatById
  213. ############################
  214. @router.get("/share/{share_id}", response_model=Optional[ChatResponse])
  215. async def get_shared_chat_by_id(share_id: str, user=Depends(get_verified_user)):
  216. if user.role == "pending":
  217. raise HTTPException(
  218. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  219. )
  220. if user.role == "user" or (user.role == "admin" and not ENABLE_ADMIN_CHAT_ACCESS):
  221. chat = Chats.get_chat_by_share_id(share_id)
  222. elif user.role == "admin" and ENABLE_ADMIN_CHAT_ACCESS:
  223. chat = Chats.get_chat_by_id(share_id)
  224. if chat:
  225. return ChatResponse(**chat.model_dump())
  226. else:
  227. raise HTTPException(
  228. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  229. )
  230. ############################
  231. # GetChatsByTags
  232. ############################
  233. class TagForm(BaseModel):
  234. name: str
  235. class TagFilterForm(TagForm):
  236. skip: Optional[int] = 0
  237. limit: Optional[int] = 50
  238. @router.post("/tags", response_model=list[ChatTitleIdResponse])
  239. async def get_user_chat_list_by_tag_name(
  240. form_data: TagFilterForm, user=Depends(get_verified_user)
  241. ):
  242. chats = Chats.get_chat_list_by_user_id_and_tag_name(
  243. user.id, form_data.name, form_data.skip, form_data.limit
  244. )
  245. if len(chats) == 0:
  246. Tags.delete_tag_by_name_and_user_id(form_data.name, user.id)
  247. return chats
  248. ############################
  249. # GetChatById
  250. ############################
  251. @router.get("/{id}", response_model=Optional[ChatResponse])
  252. async def get_chat_by_id(id: str, user=Depends(get_verified_user)):
  253. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  254. if chat:
  255. return ChatResponse(**chat.model_dump())
  256. else:
  257. raise HTTPException(
  258. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  259. )
  260. ############################
  261. # UpdateChatById
  262. ############################
  263. @router.post("/{id}", response_model=Optional[ChatResponse])
  264. async def update_chat_by_id(
  265. id: str, form_data: ChatForm, user=Depends(get_verified_user)
  266. ):
  267. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  268. if chat:
  269. updated_chat = {**chat.chat, **form_data.chat}
  270. chat = Chats.update_chat_by_id(id, updated_chat)
  271. return ChatResponse(**chat.model_dump())
  272. else:
  273. raise HTTPException(
  274. status_code=status.HTTP_401_UNAUTHORIZED,
  275. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  276. )
  277. ############################
  278. # DeleteChatById
  279. ############################
  280. @router.delete("/{id}", response_model=bool)
  281. async def delete_chat_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  282. if user.role == "admin":
  283. chat = Chats.get_chat_by_id(id)
  284. for tag in chat.meta.get("tags", []):
  285. if Chats.count_chats_by_tag_name_and_user_id(tag, user.id) == 1:
  286. Tags.delete_tag_by_name_and_user_id(tag, user.id)
  287. result = Chats.delete_chat_by_id(id)
  288. return result
  289. else:
  290. if not has_permission(
  291. user.id, "chat.delete", request.app.state.config.USER_PERMISSIONS
  292. ):
  293. raise HTTPException(
  294. status_code=status.HTTP_401_UNAUTHORIZED,
  295. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  296. )
  297. chat = Chats.get_chat_by_id(id)
  298. for tag in chat.meta.get("tags", []):
  299. if Chats.count_chats_by_tag_name_and_user_id(tag, user.id) == 1:
  300. Tags.delete_tag_by_name_and_user_id(tag, user.id)
  301. result = Chats.delete_chat_by_id_and_user_id(id, user.id)
  302. return result
  303. ############################
  304. # GetPinnedStatusById
  305. ############################
  306. @router.get("/{id}/pinned", response_model=Optional[bool])
  307. async def get_pinned_status_by_id(id: str, user=Depends(get_verified_user)):
  308. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  309. if chat:
  310. return chat.pinned
  311. else:
  312. raise HTTPException(
  313. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  314. )
  315. ############################
  316. # PinChatById
  317. ############################
  318. @router.post("/{id}/pin", response_model=Optional[ChatResponse])
  319. async def pin_chat_by_id(id: str, user=Depends(get_verified_user)):
  320. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  321. if chat:
  322. chat = Chats.toggle_chat_pinned_by_id(id)
  323. return chat
  324. else:
  325. raise HTTPException(
  326. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  327. )
  328. ############################
  329. # CloneChat
  330. ############################
  331. class CloneForm(BaseModel):
  332. title: Optional[str] = None
  333. @router.post("/{id}/clone", response_model=Optional[ChatResponse])
  334. async def clone_chat_by_id(
  335. form_data: CloneForm, id: str, user=Depends(get_verified_user)
  336. ):
  337. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  338. if chat:
  339. updated_chat = {
  340. **chat.chat,
  341. "originalChatId": chat.id,
  342. "branchPointMessageId": chat.chat["history"]["currentId"],
  343. "title": form_data.title if form_data.title else f"Clone of {chat.title}",
  344. }
  345. chat = Chats.insert_new_chat(user.id, ChatForm(**{"chat": updated_chat}))
  346. return ChatResponse(**chat.model_dump())
  347. else:
  348. raise HTTPException(
  349. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  350. )
  351. ############################
  352. # CloneSharedChatById
  353. ############################
  354. @router.post("/{id}/clone/shared", response_model=Optional[ChatResponse])
  355. async def clone_shared_chat_by_id(id: str, user=Depends(get_verified_user)):
  356. chat = Chats.get_chat_by_share_id(id)
  357. if chat:
  358. updated_chat = {
  359. **chat.chat,
  360. "originalChatId": chat.id,
  361. "branchPointMessageId": chat.chat["history"]["currentId"],
  362. "title": f"Clone of {chat.title}",
  363. }
  364. chat = Chats.insert_new_chat(user.id, ChatForm(**{"chat": updated_chat}))
  365. return ChatResponse(**chat.model_dump())
  366. else:
  367. raise HTTPException(
  368. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  369. )
  370. ############################
  371. # ArchiveChat
  372. ############################
  373. @router.post("/{id}/archive", response_model=Optional[ChatResponse])
  374. async def archive_chat_by_id(id: str, user=Depends(get_verified_user)):
  375. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  376. if chat:
  377. chat = Chats.toggle_chat_archive_by_id(id)
  378. # Delete tags if chat is archived
  379. if chat.archived:
  380. for tag_id in chat.meta.get("tags", []):
  381. if Chats.count_chats_by_tag_name_and_user_id(tag_id, user.id) == 0:
  382. log.debug(f"deleting tag: {tag_id}")
  383. Tags.delete_tag_by_name_and_user_id(tag_id, user.id)
  384. else:
  385. for tag_id in chat.meta.get("tags", []):
  386. tag = Tags.get_tag_by_name_and_user_id(tag_id, user.id)
  387. if tag is None:
  388. log.debug(f"inserting tag: {tag_id}")
  389. tag = Tags.insert_new_tag(tag_id, user.id)
  390. return ChatResponse(**chat.model_dump())
  391. else:
  392. raise HTTPException(
  393. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  394. )
  395. ############################
  396. # ShareChatById
  397. ############################
  398. @router.post("/{id}/share", response_model=Optional[ChatResponse])
  399. async def share_chat_by_id(id: str, user=Depends(get_verified_user)):
  400. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  401. if chat:
  402. if chat.share_id:
  403. shared_chat = Chats.update_shared_chat_by_chat_id(chat.id)
  404. return ChatResponse(**shared_chat.model_dump())
  405. shared_chat = Chats.insert_shared_chat_by_chat_id(chat.id)
  406. if not shared_chat:
  407. raise HTTPException(
  408. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  409. detail=ERROR_MESSAGES.DEFAULT(),
  410. )
  411. return ChatResponse(**shared_chat.model_dump())
  412. else:
  413. raise HTTPException(
  414. status_code=status.HTTP_401_UNAUTHORIZED,
  415. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  416. )
  417. ############################
  418. # DeletedSharedChatById
  419. ############################
  420. @router.delete("/{id}/share", response_model=Optional[bool])
  421. async def delete_shared_chat_by_id(id: str, user=Depends(get_verified_user)):
  422. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  423. if chat:
  424. if not chat.share_id:
  425. return False
  426. result = Chats.delete_shared_chat_by_chat_id(id)
  427. update_result = Chats.update_chat_share_id_by_id(id, None)
  428. return result and update_result != None
  429. else:
  430. raise HTTPException(
  431. status_code=status.HTTP_401_UNAUTHORIZED,
  432. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  433. )
  434. ############################
  435. # UpdateChatFolderIdById
  436. ############################
  437. class ChatFolderIdForm(BaseModel):
  438. folder_id: Optional[str] = None
  439. @router.post("/{id}/folder", response_model=Optional[ChatResponse])
  440. async def update_chat_folder_id_by_id(
  441. id: str, form_data: ChatFolderIdForm, user=Depends(get_verified_user)
  442. ):
  443. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  444. if chat:
  445. chat = Chats.update_chat_folder_id_by_id_and_user_id(
  446. id, user.id, form_data.folder_id
  447. )
  448. return ChatResponse(**chat.model_dump())
  449. else:
  450. raise HTTPException(
  451. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  452. )
  453. ############################
  454. # GetChatTagsById
  455. ############################
  456. @router.get("/{id}/tags", response_model=list[TagModel])
  457. async def get_chat_tags_by_id(id: str, user=Depends(get_verified_user)):
  458. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  459. if chat:
  460. tags = chat.meta.get("tags", [])
  461. return Tags.get_tags_by_ids_and_user_id(tags, user.id)
  462. else:
  463. raise HTTPException(
  464. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  465. )
  466. ############################
  467. # AddChatTagById
  468. ############################
  469. @router.post("/{id}/tags", response_model=list[TagModel])
  470. async def add_tag_by_id_and_tag_name(
  471. id: str, form_data: TagForm, user=Depends(get_verified_user)
  472. ):
  473. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  474. if chat:
  475. tags = chat.meta.get("tags", [])
  476. tag_id = form_data.name.replace(" ", "_").lower()
  477. if tag_id == "none":
  478. raise HTTPException(
  479. status_code=status.HTTP_400_BAD_REQUEST,
  480. detail=ERROR_MESSAGES.DEFAULT("Tag name cannot be 'None'"),
  481. )
  482. if tag_id not in tags:
  483. Chats.add_chat_tag_by_id_and_user_id_and_tag_name(
  484. id, user.id, form_data.name
  485. )
  486. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  487. tags = chat.meta.get("tags", [])
  488. return Tags.get_tags_by_ids_and_user_id(tags, user.id)
  489. else:
  490. raise HTTPException(
  491. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  492. )
  493. ############################
  494. # DeleteChatTagById
  495. ############################
  496. @router.delete("/{id}/tags", response_model=list[TagModel])
  497. async def delete_tag_by_id_and_tag_name(
  498. id: str, form_data: TagForm, user=Depends(get_verified_user)
  499. ):
  500. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  501. if chat:
  502. Chats.delete_tag_by_id_and_user_id_and_tag_name(id, user.id, form_data.name)
  503. if Chats.count_chats_by_tag_name_and_user_id(form_data.name, user.id) == 0:
  504. Tags.delete_tag_by_name_and_user_id(form_data.name, user.id)
  505. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  506. tags = chat.meta.get("tags", [])
  507. return Tags.get_tags_by_ids_and_user_id(tags, user.id)
  508. else:
  509. raise HTTPException(
  510. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  511. )
  512. ############################
  513. # DeleteAllTagsById
  514. ############################
  515. @router.delete("/{id}/tags/all", response_model=Optional[bool])
  516. async def delete_all_tags_by_id(id: str, user=Depends(get_verified_user)):
  517. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  518. if chat:
  519. Chats.delete_all_tags_by_id_and_user_id(id, user.id)
  520. for tag in chat.meta.get("tags", []):
  521. if Chats.count_chats_by_tag_name_and_user_id(tag, user.id) == 0:
  522. Tags.delete_tag_by_name_and_user_id(tag, user.id)
  523. return True
  524. else:
  525. raise HTTPException(
  526. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  527. )