open-webui/backend/apps/webui/models/documents.py

168 lines
4.2 KiB
Python
Raw Normal View History

from pydantic import BaseModel, ConfigDict
2024-08-14 12:46:31 +00:00
from typing import Optional
2024-01-08 07:43:32 +00:00
import time
import logging
2024-01-08 07:43:32 +00:00
from sqlalchemy import String, Column, BigInteger, Text
2024-01-08 07:43:32 +00:00
2024-07-04 06:32:39 +00:00
from apps.webui.internal.db import Base, get_db
2024-01-08 07:43:32 +00:00
import json
2024-08-25 14:52:36 +00:00
from env import SRC_LOG_LEVELS
2024-03-31 08:13:39 +00:00
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"])
2024-01-08 07:43:32 +00:00
####################
# Documents DB Schema
####################
class Document(Base):
__tablename__ = "document"
2024-01-08 07:43:32 +00:00
collection_name = Column(String, primary_key=True)
name = Column(String, unique=True)
title = Column(Text)
filename = Column(Text)
content = Column(Text, nullable=True)
user_id = Column(String)
timestamp = Column(BigInteger)
2024-01-08 07:43:32 +00:00
class DocumentModel(BaseModel):
model_config = ConfigDict(from_attributes=True)
2024-01-08 07:43:32 +00:00
collection_name: str
name: str
title: str
filename: str
content: Optional[str] = None
user_id: str
timestamp: int # timestamp in epoch
####################
# Forms
####################
2024-02-03 22:44:49 +00:00
class DocumentResponse(BaseModel):
collection_name: str
name: str
title: str
filename: str
content: Optional[dict] = None
user_id: str
timestamp: int # timestamp in epoch
2024-01-08 07:43:32 +00:00
class DocumentUpdateForm(BaseModel):
name: str
title: str
class DocumentForm(DocumentUpdateForm):
collection_name: str
filename: str
content: Optional[str] = None
class DocumentsTable:
def insert_new_doc(
self, user_id: str, form_data: DocumentForm
2024-01-08 07:43:32 +00:00
) -> Optional[DocumentModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
2024-01-08 07:43:32 +00:00
2024-07-04 06:32:39 +00:00
document = DocumentModel(
**{
**form_data.model_dump(),
"user_id": user_id,
"timestamp": int(time.time()),
}
)
try:
result = Document(**document.model_dump())
db.add(result)
db.commit()
db.refresh(result)
if result:
return DocumentModel.model_validate(result)
else:
return None
2024-08-14 12:38:19 +00:00
except Exception:
return None
2024-01-08 07:43:32 +00:00
def get_doc_by_name(self, name: str) -> Optional[DocumentModel]:
2024-01-08 07:43:32 +00:00
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
document = db.query(Document).filter_by(name=name).first()
return DocumentModel.model_validate(document) if document else None
2024-08-14 12:38:19 +00:00
except Exception:
2024-01-08 07:43:32 +00:00
return None
2024-08-14 12:46:31 +00:00
def get_docs(self) -> list[DocumentModel]:
2024-07-04 06:32:39 +00:00
with get_db() as db:
return [
DocumentModel.model_validate(doc) for doc in db.query(Document).all()
]
2024-01-08 07:43:32 +00:00
def update_doc_by_name(
self, name: str, form_data: DocumentUpdateForm
2024-01-08 07:43:32 +00:00
) -> Optional[DocumentModel]:
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
db.query(Document).filter_by(name=name).update(
{
"title": form_data.title,
"name": form_data.name,
"timestamp": int(time.time()),
}
)
db.commit()
return self.get_doc_by_name(form_data.name)
2024-01-08 09:49:20 +00:00
except Exception as e:
log.exception(e)
2024-01-08 07:43:32 +00:00
return None
2024-02-03 22:44:49 +00:00
def update_doc_content_by_name(
self, name: str, updated: dict
2024-02-03 22:44:49 +00:00
) -> Optional[DocumentModel]:
try:
doc = self.get_doc_by_name(name)
doc_content = json.loads(doc.content if doc.content else "{}")
doc_content = {**doc_content, **updated}
2024-07-04 06:32:39 +00:00
with get_db() as db:
db.query(Document).filter_by(name=name).update(
{
"content": json.dumps(doc_content),
"timestamp": int(time.time()),
}
)
db.commit()
return self.get_doc_by_name(name)
2024-02-03 22:44:49 +00:00
except Exception as e:
log.exception(e)
2024-02-03 22:44:49 +00:00
return None
def delete_doc_by_name(self, name: str) -> bool:
2024-01-08 07:43:32 +00:00
try:
2024-07-04 06:32:39 +00:00
with get_db() as db:
db.query(Document).filter_by(name=name).delete()
db.commit()
2024-07-04 06:32:39 +00:00
return True
2024-08-14 12:38:19 +00:00
except Exception:
2024-01-08 07:43:32 +00:00
return False
Documents = DocumentsTable()