2024-05-19 10:46:24 +00:00
|
|
|
import json
|
2024-05-19 14:11:28 +00:00
|
|
|
import logging
|
2024-05-19 10:46:24 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import peewee as pw
|
|
|
|
from playhouse.shortcuts import model_to_dict
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
2024-05-21 21:05:16 +00:00
|
|
|
from apps.web.internal.db import DB, JSONField
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-05-19 14:11:28 +00:00
|
|
|
from config import SRC_LOG_LEVELS
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["MODELS"])
|
|
|
|
|
2024-05-19 10:46:24 +00:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Models DB Schema
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
|
# ModelParams is a model for the data stored in the params field of the Model table
|
|
|
|
# It isn't currently used in the backend, but it's here as a reference
|
|
|
|
class ModelParams(BaseModel):
|
2024-05-21 21:05:16 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# ModelMeta is a model for the data stored in the meta field of the Model table
|
|
|
|
# It isn't currently used in the backend, but it's here as a reference
|
|
|
|
class ModelMeta(BaseModel):
|
2024-05-19 14:11:28 +00:00
|
|
|
description: str
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
2024-05-19 14:11:28 +00:00
|
|
|
User-facing description of the model.
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
vision_capable: bool
|
2024-05-19 14:11:28 +00:00
|
|
|
"""
|
|
|
|
A flag indicating if the model is capable of vision and thus image inputs
|
|
|
|
"""
|
2024-05-19 10:46:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Model(pw.Model):
|
2024-05-21 21:05:16 +00:00
|
|
|
id = pw.TextField(unique=True)
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
|
|
|
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()
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-05-21 21:05:16 +00:00
|
|
|
base_model_id = pw.TextField(null=True)
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
2024-05-21 21:05:16 +00:00
|
|
|
An optional pointer to the actual model that should be used when proxying requests.
|
|
|
|
Currently unused - but will be used to support Modelfile like behaviour in the future
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
name = pw.TextField()
|
|
|
|
"""
|
2024-05-21 21:05:16 +00:00
|
|
|
The human-readable display name of the model.
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
|
|
|
|
2024-05-21 21:05:16 +00:00
|
|
|
params = JSONField()
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
2024-05-21 21:05:16 +00:00
|
|
|
Holds a JSON encoded blob of parameters, see `ModelParams`.
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
|
|
|
|
2024-05-24 05:58:26 +00:00
|
|
|
meta = JSONField()
|
|
|
|
"""
|
|
|
|
Holds a JSON encoded blob of metadata, see `ModelMeta`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
|
2024-05-19 10:46:24 +00:00
|
|
|
class Meta:
|
|
|
|
database = DB
|
|
|
|
|
|
|
|
|
|
|
|
class ModelModel(BaseModel):
|
|
|
|
id: str
|
2024-05-21 21:05:16 +00:00
|
|
|
base_model_id: Optional[str] = None
|
2024-05-19 10:46:24 +00:00
|
|
|
name: str
|
2024-05-21 21:05:16 +00:00
|
|
|
params: ModelParams
|
2024-05-24 05:58:26 +00:00
|
|
|
meta: ModelMeta
|
2024-05-19 10:46:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
####################
|
|
|
|
# Forms
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
|
class ModelsTable:
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
db: pw.SqliteDatabase | pw.PostgresqlDatabase,
|
|
|
|
):
|
|
|
|
self.db = db
|
|
|
|
self.db.create_tables([Model])
|
|
|
|
|
|
|
|
def get_all_models(self) -> list[ModelModel]:
|
|
|
|
return [ModelModel(**model_to_dict(model)) for model in Model.select()]
|
|
|
|
|
2024-05-21 21:05:16 +00:00
|
|
|
def update_all_models(self, models: list[ModelModel]) -> bool:
|
2024-05-19 10:46:24 +00:00
|
|
|
try:
|
|
|
|
with self.db.atomic():
|
|
|
|
# Fetch current models from the database
|
|
|
|
current_models = self.get_all_models()
|
2024-05-21 21:05:16 +00:00
|
|
|
current_model_dict = {model.id: model for model in current_models}
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-05-21 21:05:16 +00:00
|
|
|
# Create a set of model IDs from the current models and the new models
|
2024-05-19 10:46:24 +00:00
|
|
|
current_model_keys = set(current_model_dict.keys())
|
2024-05-21 21:05:16 +00:00
|
|
|
new_model_keys = set(model.id for model in models)
|
2024-05-19 10:46:24 +00:00
|
|
|
|
|
|
|
# Determine which models need to be created, updated, or deleted
|
|
|
|
models_to_create = [
|
2024-05-21 21:05:16 +00:00
|
|
|
model for model in models if model.id not in current_model_keys
|
2024-05-19 10:46:24 +00:00
|
|
|
]
|
|
|
|
models_to_update = [
|
2024-05-21 21:05:16 +00:00
|
|
|
model for model in models if model.id in current_model_keys
|
2024-05-19 10:46:24 +00:00
|
|
|
]
|
|
|
|
models_to_delete = current_model_keys - new_model_keys
|
|
|
|
|
|
|
|
# Perform the necessary database operations
|
|
|
|
for model in models_to_create:
|
2024-05-21 21:05:16 +00:00
|
|
|
Model.create(**model.model_dump())
|
2024-05-19 10:46:24 +00:00
|
|
|
|
|
|
|
for model in models_to_update:
|
2024-05-21 21:05:16 +00:00
|
|
|
Model.update(**model.model_dump()).where(
|
|
|
|
Model.id == model.id
|
2024-05-19 10:46:24 +00:00
|
|
|
).execute()
|
|
|
|
|
|
|
|
for model_id, model_source in models_to_delete:
|
2024-05-21 21:05:16 +00:00
|
|
|
Model.delete().where(Model.id == model_id).execute()
|
2024-05-19 10:46:24 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
2024-05-19 14:11:28 +00:00
|
|
|
log.exception(e)
|
2024-05-19 10:46:24 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
Models = ModelsTable(DB)
|