documents.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import json
  2. from typing import Optional
  3. from open_webui.apps.webui.models.documents import (
  4. DocumentForm,
  5. DocumentResponse,
  6. Documents,
  7. DocumentUpdateForm,
  8. )
  9. from open_webui.constants import ERROR_MESSAGES
  10. from fastapi import APIRouter, Depends, HTTPException, status
  11. from pydantic import BaseModel
  12. from open_webui.utils.utils import get_admin_user, get_verified_user
  13. router = APIRouter()
  14. ############################
  15. # GetDocuments
  16. ############################
  17. @router.get("/", response_model=list[DocumentResponse])
  18. async def get_documents(user=Depends(get_verified_user)):
  19. docs = [
  20. DocumentResponse(
  21. **{
  22. **doc.model_dump(),
  23. "content": json.loads(doc.content if doc.content else "{}"),
  24. }
  25. )
  26. for doc in Documents.get_docs()
  27. ]
  28. return docs
  29. ############################
  30. # CreateNewDoc
  31. ############################
  32. @router.post("/create", response_model=Optional[DocumentResponse])
  33. async def create_new_doc(form_data: DocumentForm, user=Depends(get_admin_user)):
  34. doc = Documents.get_doc_by_name(form_data.name)
  35. if doc is None:
  36. doc = Documents.insert_new_doc(user.id, form_data)
  37. if doc:
  38. return DocumentResponse(
  39. **{
  40. **doc.model_dump(),
  41. "content": json.loads(doc.content if doc.content else "{}"),
  42. }
  43. )
  44. else:
  45. raise HTTPException(
  46. status_code=status.HTTP_400_BAD_REQUEST,
  47. detail=ERROR_MESSAGES.FILE_EXISTS,
  48. )
  49. else:
  50. raise HTTPException(
  51. status_code=status.HTTP_400_BAD_REQUEST,
  52. detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
  53. )
  54. ############################
  55. # GetDocByName
  56. ############################
  57. @router.get("/doc", response_model=Optional[DocumentResponse])
  58. async def get_doc_by_name(name: str, user=Depends(get_verified_user)):
  59. doc = Documents.get_doc_by_name(name)
  60. if doc:
  61. return DocumentResponse(
  62. **{
  63. **doc.model_dump(),
  64. "content": json.loads(doc.content if doc.content else "{}"),
  65. }
  66. )
  67. else:
  68. raise HTTPException(
  69. status_code=status.HTTP_401_UNAUTHORIZED,
  70. detail=ERROR_MESSAGES.NOT_FOUND,
  71. )
  72. ############################
  73. # TagDocByName
  74. ############################
  75. class TagItem(BaseModel):
  76. name: str
  77. class TagDocumentForm(BaseModel):
  78. name: str
  79. tags: list[dict]
  80. @router.post("/doc/tags", response_model=Optional[DocumentResponse])
  81. async def tag_doc_by_name(form_data: TagDocumentForm, user=Depends(get_verified_user)):
  82. doc = Documents.update_doc_content_by_name(form_data.name, {"tags": form_data.tags})
  83. if doc:
  84. return DocumentResponse(
  85. **{
  86. **doc.model_dump(),
  87. "content": json.loads(doc.content if doc.content else "{}"),
  88. }
  89. )
  90. else:
  91. raise HTTPException(
  92. status_code=status.HTTP_401_UNAUTHORIZED,
  93. detail=ERROR_MESSAGES.NOT_FOUND,
  94. )
  95. ############################
  96. # UpdateDocByName
  97. ############################
  98. @router.post("/doc/update", response_model=Optional[DocumentResponse])
  99. async def update_doc_by_name(
  100. name: str,
  101. form_data: DocumentUpdateForm,
  102. user=Depends(get_admin_user),
  103. ):
  104. doc = Documents.update_doc_by_name(name, form_data)
  105. if doc:
  106. return DocumentResponse(
  107. **{
  108. **doc.model_dump(),
  109. "content": json.loads(doc.content if doc.content else "{}"),
  110. }
  111. )
  112. else:
  113. raise HTTPException(
  114. status_code=status.HTTP_400_BAD_REQUEST,
  115. detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
  116. )
  117. ############################
  118. # DeleteDocByName
  119. ############################
  120. @router.delete("/doc/delete", response_model=bool)
  121. async def delete_doc_by_name(name: str, user=Depends(get_admin_user)):
  122. result = Documents.delete_doc_by_name(name)
  123. return result