open-webui/backend/apps/web/models/modelfiles.py

145 lines
3.9 KiB
Python
Raw Normal View History

2024-05-24 07:26:00 +00:00
################################################################################
# DEPRECATION NOTICE #
# #
# This file has been deprecated since version 0.2.0. #
# #
################################################################################
2023-12-27 06:02:17 +00:00
from pydantic import BaseModel
from peewee import *
from playhouse.shortcuts import model_to_dict
from typing import List, Union, Optional
import time
from utils.utils import decode_token
from utils.misc import get_gravatar_url
from apps.web.internal.db import DB
import json
####################
2024-01-03 04:41:37 +00:00
# Modelfile DB Schema
2023-12-27 06:02:17 +00:00
####################
class Modelfile(Model):
tag_name = CharField(unique=True)
user_id = CharField()
modelfile = TextField()
timestamp = BigIntegerField()
2023-12-27 06:02:17 +00:00
class Meta:
database = DB
class ModelfileModel(BaseModel):
tag_name: str
user_id: str
modelfile: str
timestamp: int # timestamp in epoch
####################
# Forms
####################
class ModelfileForm(BaseModel):
modelfile: dict
class ModelfileTagNameForm(BaseModel):
tag_name: str
class ModelfileUpdateForm(ModelfileForm, ModelfileTagNameForm):
pass
2023-12-27 06:02:17 +00:00
class ModelfileResponse(BaseModel):
tag_name: str
user_id: str
modelfile: dict
timestamp: int # timestamp in epoch
class ModelfilesTable:
2023-12-27 06:02:17 +00:00
def __init__(self, db):
self.db = db
self.db.create_tables([Modelfile])
def insert_new_modelfile(
2024-03-31 08:13:39 +00:00
self, user_id: str, form_data: ModelfileForm
) -> Optional[ModelfileModel]:
if "tagName" in form_data.modelfile:
2023-12-27 06:02:17 +00:00
modelfile = ModelfileModel(
**{
"user_id": user_id,
"tag_name": form_data.modelfile["tagName"],
2023-12-27 06:02:17 +00:00
"modelfile": json.dumps(form_data.modelfile),
"timestamp": int(time.time()),
2024-03-31 08:13:39 +00:00
}
)
2023-12-27 07:58:40 +00:00
try:
result = Modelfile.create(**modelfile.model_dump())
if result:
return modelfile
else:
return None
except:
2023-12-27 06:02:17 +00:00
return None
2023-12-27 07:58:40 +00:00
2023-12-27 06:02:17 +00:00
else:
return None
2024-03-31 08:13:39 +00:00
def get_modelfile_by_tag_name(self, tag_name: str) -> Optional[ModelfileModel]:
2023-12-27 06:02:17 +00:00
try:
modelfile = Modelfile.get(Modelfile.tag_name == tag_name)
return ModelfileModel(**model_to_dict(modelfile))
except:
return None
2024-03-31 08:13:39 +00:00
def get_modelfiles(self, skip: int = 0, limit: int = 50) -> List[ModelfileResponse]:
2023-12-27 06:02:17 +00:00
return [
ModelfileResponse(
**{
**model_to_dict(modelfile),
2024-03-31 08:13:39 +00:00
"modelfile": json.loads(modelfile.modelfile),
}
)
for modelfile in Modelfile.select()
2023-12-27 06:02:17 +00:00
# .limit(limit).offset(skip)
]
def update_modelfile_by_tag_name(
2024-03-31 08:13:39 +00:00
self, tag_name: str, modelfile: dict
) -> Optional[ModelfileModel]:
2023-12-27 06:02:17 +00:00
try:
query = Modelfile.update(
modelfile=json.dumps(modelfile),
timestamp=int(time.time()),
).where(Modelfile.tag_name == tag_name)
query.execute()
modelfile = Modelfile.get(Modelfile.tag_name == tag_name)
return ModelfileModel(**model_to_dict(modelfile))
except:
return None
def delete_modelfile_by_tag_name(self, tag_name: str) -> bool:
try:
query = Modelfile.delete().where((Modelfile.tag_name == tag_name))
query.execute() # Remove the rows, return number of rows removed.
return True
except:
return False
Modelfiles = ModelfilesTable(DB)