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

180 lines
4.1 KiB
Python
Raw Normal View History

import json
import logging
from typing import Optional
import peewee as pw
2024-05-24 07:26:00 +00:00
from peewee import *
from playhouse.shortcuts import model_to_dict
2024-05-24 06:47:01 +00:00
from pydantic import BaseModel, ConfigDict
from apps.web.internal.db import DB, JSONField
2024-05-24 07:26:00 +00:00
from typing import List, Union, Optional
from config import SRC_LOG_LEVELS
2024-05-24 07:26:00 +00:00
import time
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"])
####################
# Models DB Schema
####################
# ModelParams is a model for the data stored in the params field of the Model table
class ModelParams(BaseModel):
2024-05-24 06:47:01 +00:00
model_config = ConfigDict(extra="allow")
pass
# ModelMeta is a model for the data stored in the meta field of the Model table
class ModelMeta(BaseModel):
2024-05-25 01:26:36 +00:00
profile_image_url: Optional[str] = "/favicon.png"
2024-05-24 06:47:01 +00:00
description: Optional[str] = None
"""
User-facing description of the model.
"""
2024-05-25 06:34:58 +00:00
capabilities: Optional[dict] = None
2024-05-24 06:47:01 +00:00
model_config = ConfigDict(extra="allow")
pass
class Model(pw.Model):
id = pw.TextField(unique=True)
"""
The model's id as used in the API. If set to an existing model, it will override the model.
"""
2024-05-24 05:58:26 +00:00
user_id = pw.TextField()
base_model_id = pw.TextField(null=True)
"""
An optional pointer to the actual model that should be used when proxying requests.
"""
name = pw.TextField()
"""
The human-readable display name of the model.
"""
params = JSONField()
"""
Holds a JSON encoded blob of parameters, see `ModelParams`.
"""
2024-05-24 05:58:26 +00:00
meta = JSONField()
"""
Holds a JSON encoded blob of metadata, see `ModelMeta`.
"""
2024-05-24 07:26:00 +00:00
updated_at = BigIntegerField()
created_at = BigIntegerField()
2024-05-24 05:58:26 +00:00
class Meta:
database = DB
class ModelModel(BaseModel):
id: str
2024-05-25 01:26:36 +00:00
user_id: str
base_model_id: Optional[str] = None
2024-05-24 07:26:00 +00:00
name: str
params: ModelParams
2024-05-24 05:58:26 +00:00
meta: ModelMeta
2024-05-24 07:26:00 +00:00
updated_at: int # timestamp in epoch
created_at: int # timestamp in epoch
####################
# Forms
####################
2024-05-24 07:26:00 +00:00
class ModelResponse(BaseModel):
id: str
name: str
meta: ModelMeta
updated_at: int # timestamp in epoch
created_at: int # timestamp in epoch
class ModelForm(BaseModel):
id: str
base_model_id: Optional[str] = None
name: str
meta: ModelMeta
params: ModelParams
class ModelsTable:
def __init__(
self,
db: pw.SqliteDatabase | pw.PostgresqlDatabase,
):
self.db = db
self.db.create_tables([Model])
2024-05-25 01:26:36 +00:00
def insert_new_model(
self, form_data: ModelForm, user_id: str
) -> Optional[ModelModel]:
model = ModelModel(
**{
**form_data.model_dump(),
"user_id": user_id,
"created_at": int(time.time()),
"updated_at": int(time.time()),
}
)
2024-05-24 07:26:00 +00:00
try:
2024-05-25 01:26:36 +00:00
result = Model.create(**model.model_dump())
if result:
return model
else:
return None
except Exception as e:
print(e)
2024-05-24 07:26:00 +00:00
return None
def get_all_models(self) -> List[ModelModel]:
return [ModelModel(**model_to_dict(model)) for model in Model.select()]
2024-05-24 07:26:00 +00:00
def get_model_by_id(self, id: str) -> Optional[ModelModel]:
try:
2024-05-24 07:26:00 +00:00
model = Model.get(Model.id == id)
return ModelModel(**model_to_dict(model))
except:
return None
2024-05-24 07:26:00 +00:00
def update_model_by_id(self, id: str, model: ModelForm) -> Optional[ModelModel]:
try:
# update only the fields that are present in the model
query = Model.update(**model.model_dump()).where(Model.id == id)
query.execute()
model = Model.get(Model.id == id)
return ModelModel(**model_to_dict(model))
2024-05-25 05:21:57 +00:00
except Exception as e:
print(e)
2024-05-24 07:26:00 +00:00
return None
def delete_model_by_id(self, id: str) -> bool:
try:
query = Model.delete().where(Model.id == id)
query.execute()
return True
2024-05-24 07:26:00 +00:00
except:
return False
Models = ModelsTable(DB)