2024-01-07 23:43:32 -08:00
|
|
|
import json
|
2024-08-28 00:10:27 +02:00
|
|
|
from typing import Optional
|
2024-01-07 23:43:32 -08:00
|
|
|
|
2024-09-04 16:54:48 +02:00
|
|
|
from open_webui.apps.webui.models.documents import (
|
2024-01-07 23:43:32 -08:00
|
|
|
DocumentForm,
|
2024-02-03 14:44:49 -08:00
|
|
|
DocumentResponse,
|
2024-08-28 00:10:27 +02:00
|
|
|
Documents,
|
|
|
|
|
DocumentUpdateForm,
|
2024-01-07 23:43:32 -08:00
|
|
|
)
|
2024-09-04 16:54:48 +02:00
|
|
|
from open_webui.constants import ERROR_MESSAGES
|
2024-08-28 00:10:27 +02:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
|
from pydantic import BaseModel
|
2024-09-04 16:54:48 +02:00
|
|
|
from open_webui.utils.utils import get_admin_user, get_verified_user
|
2024-01-07 23:43:32 -08:00
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
|
# GetDocuments
|
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
2024-08-14 13:46:31 +01:00
|
|
|
@router.get("/", response_model=list[DocumentResponse])
|
2024-06-27 11:29:59 -07:00
|
|
|
async def get_documents(user=Depends(get_verified_user)):
|
2024-02-03 14:44:49 -08:00
|
|
|
docs = [
|
|
|
|
|
DocumentResponse(
|
|
|
|
|
**{
|
|
|
|
|
**doc.model_dump(),
|
|
|
|
|
"content": json.loads(doc.content if doc.content else "{}"),
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-06-21 14:58:57 +02:00
|
|
|
for doc in Documents.get_docs()
|
2024-02-03 14:44:49 -08:00
|
|
|
]
|
|
|
|
|
return docs
|
2024-01-07 23:43:32 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
|
# CreateNewDoc
|
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
2024-02-03 14:44:49 -08:00
|
|
|
@router.post("/create", response_model=Optional[DocumentResponse])
|
2024-06-24 09:57:08 +02:00
|
|
|
async def create_new_doc(form_data: DocumentForm, user=Depends(get_admin_user)):
|
2024-06-21 14:58:57 +02:00
|
|
|
doc = Documents.get_doc_by_name(form_data.name)
|
2024-08-14 13:39:53 +01:00
|
|
|
if doc is None:
|
2024-06-21 14:58:57 +02:00
|
|
|
doc = Documents.insert_new_doc(user.id, form_data)
|
2024-01-07 23:43:32 -08:00
|
|
|
|
|
|
|
|
if doc:
|
2024-02-03 14:44:49 -08:00
|
|
|
return DocumentResponse(
|
|
|
|
|
**{
|
|
|
|
|
**doc.model_dump(),
|
|
|
|
|
"content": json.loads(doc.content if doc.content else "{}"),
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-01-07 23:43:32 -08:00
|
|
|
else:
|
|
|
|
|
raise HTTPException(
|
2024-01-08 01:54:03 -08:00
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
|
detail=ERROR_MESSAGES.FILE_EXISTS,
|
2024-01-07 23:43:32 -08:00
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
2024-01-08 01:54:03 -08:00
|
|
|
detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
|
2024-01-07 23:43:32 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
|
# GetDocByName
|
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
2024-06-12 13:45:13 -07:00
|
|
|
@router.get("/doc", response_model=Optional[DocumentResponse])
|
2024-06-27 11:29:59 -07:00
|
|
|
async def get_doc_by_name(name: str, user=Depends(get_verified_user)):
|
2024-06-21 14:58:57 +02:00
|
|
|
doc = Documents.get_doc_by_name(name)
|
2024-01-07 23:43:32 -08:00
|
|
|
|
|
|
|
|
if doc:
|
2024-02-03 14:44:49 -08:00
|
|
|
return DocumentResponse(
|
|
|
|
|
**{
|
|
|
|
|
**doc.model_dump(),
|
|
|
|
|
"content": json.loads(doc.content if doc.content else "{}"),
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
|
detail=ERROR_MESSAGES.NOT_FOUND,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
|
# TagDocByName
|
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
2024-02-17 21:06:08 -08:00
|
|
|
class TagItem(BaseModel):
|
|
|
|
|
name: str
|
|
|
|
|
|
|
|
|
|
|
2024-02-03 14:44:49 -08:00
|
|
|
class TagDocumentForm(BaseModel):
|
|
|
|
|
name: str
|
2024-08-14 13:46:31 +01:00
|
|
|
tags: list[dict]
|
2024-02-03 14:44:49 -08:00
|
|
|
|
|
|
|
|
|
2024-06-12 13:45:13 -07:00
|
|
|
@router.post("/doc/tags", response_model=Optional[DocumentResponse])
|
2024-06-27 11:29:59 -07:00
|
|
|
async def tag_doc_by_name(form_data: TagDocumentForm, user=Depends(get_verified_user)):
|
2024-06-24 09:57:08 +02:00
|
|
|
doc = Documents.update_doc_content_by_name(form_data.name, {"tags": form_data.tags})
|
2024-02-03 14:44:49 -08:00
|
|
|
|
|
|
|
|
if doc:
|
|
|
|
|
return DocumentResponse(
|
|
|
|
|
**{
|
|
|
|
|
**doc.model_dump(),
|
|
|
|
|
"content": json.loads(doc.content if doc.content else "{}"),
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-01-07 23:43:32 -08:00
|
|
|
else:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
|
detail=ERROR_MESSAGES.NOT_FOUND,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
|
# UpdateDocByName
|
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
2024-06-12 13:45:13 -07:00
|
|
|
@router.post("/doc/update", response_model=Optional[DocumentResponse])
|
2024-01-07 23:43:32 -08:00
|
|
|
async def update_doc_by_name(
|
2024-06-18 15:03:31 +02:00
|
|
|
name: str,
|
|
|
|
|
form_data: DocumentUpdateForm,
|
|
|
|
|
user=Depends(get_admin_user),
|
2024-01-07 23:43:32 -08:00
|
|
|
):
|
2024-06-21 14:58:57 +02:00
|
|
|
doc = Documents.update_doc_by_name(name, form_data)
|
2024-01-07 23:43:32 -08:00
|
|
|
if doc:
|
2024-02-03 14:44:49 -08:00
|
|
|
return DocumentResponse(
|
|
|
|
|
**{
|
|
|
|
|
**doc.model_dump(),
|
|
|
|
|
"content": json.loads(doc.content if doc.content else "{}"),
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-01-07 23:43:32 -08:00
|
|
|
else:
|
|
|
|
|
raise HTTPException(
|
2024-01-08 01:49:20 -08:00
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
|
detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
|
2024-01-07 23:43:32 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
|
# DeleteDocByName
|
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
2024-06-12 13:45:13 -07:00
|
|
|
@router.delete("/doc/delete", response_model=bool)
|
2024-06-24 09:57:08 +02:00
|
|
|
async def delete_doc_by_name(name: str, user=Depends(get_admin_user)):
|
2024-06-21 14:58:57 +02:00
|
|
|
result = Documents.delete_doc_by_name(name)
|
2024-01-07 23:43:32 -08:00
|
|
|
return result
|