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

162 lines
4.2 KiB
Python
Raw Normal View History

from pydantic import BaseModel, ConfigDict
from typing import List, 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
from sqlalchemy.orm import Session
2024-01-08 07:43:32 +00:00
from apps.webui.internal.db import Base, get_session
2024-01-08 07:43:32 +00:00
import json
from config 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(String)
filename = Column(String)
content = Column(String, 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]:
document = DocumentModel(
**{
**form_data.model_dump(),
"user_id": user_id,
"timestamp": int(time.time()),
}
)
try:
with get_session() as db:
result = Document(**document.model_dump())
db.add(result)
db.commit()
db.refresh(result)
if result:
return DocumentModel.model_validate(result)
else:
return None
2024-01-08 07:43:32 +00:00
except:
return None
def get_doc_by_name(self, name: str) -> Optional[DocumentModel]:
2024-01-08 07:43:32 +00:00
try:
with get_session() as db:
document = db.query(Document).filter_by(name=name).first()
return DocumentModel.model_validate(document) if document else None
2024-01-08 07:43:32 +00:00
except:
return None
def get_docs(self) -> List[DocumentModel]:
with get_session() as db:
2024-06-24 07:57:08 +00:00
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:
with get_session() 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:
with get_session() as db:
doc = self.get_doc_by_name(name)
doc_content = json.loads(doc.content if doc.content else "{}")
doc_content = {**doc_content, **updated}
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:
with get_session() as db:
db.query(Document).filter_by(name=name).delete()
2024-01-08 07:43:32 +00:00
return True
except:
return False
Documents = DocumentsTable()