models.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import json
  2. import logging
  3. from typing import Optional
  4. import peewee as pw
  5. from peewee import *
  6. from playhouse.shortcuts import model_to_dict
  7. from pydantic import BaseModel, ConfigDict
  8. from apps.web.internal.db import DB, JSONField
  9. from typing import List, Union, Optional
  10. from config import SRC_LOG_LEVELS
  11. import time
  12. log = logging.getLogger(__name__)
  13. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  14. ####################
  15. # Models DB Schema
  16. ####################
  17. # ModelParams is a model for the data stored in the params field of the Model table
  18. class ModelParams(BaseModel):
  19. model_config = ConfigDict(extra="allow")
  20. pass
  21. # ModelMeta is a model for the data stored in the meta field of the Model table
  22. # It isn't currently used in the backend, but it's here as a reference
  23. class ModelMeta(BaseModel):
  24. profile_image_url: Optional[str] = "/favicon.png"
  25. description: Optional[str] = None
  26. """
  27. User-facing description of the model.
  28. """
  29. vision_capable: Optional[bool] = None
  30. """
  31. A flag indicating if the model is capable of vision and thus image inputs
  32. """
  33. model_config = ConfigDict(extra="allow")
  34. pass
  35. class Model(pw.Model):
  36. id = pw.TextField(unique=True)
  37. """
  38. The model's id as used in the API. If set to an existing model, it will override the model.
  39. """
  40. user_id = pw.TextField()
  41. base_model_id = pw.TextField(null=True)
  42. """
  43. An optional pointer to the actual model that should be used when proxying requests.
  44. """
  45. name = pw.TextField()
  46. """
  47. The human-readable display name of the model.
  48. """
  49. params = JSONField()
  50. """
  51. Holds a JSON encoded blob of parameters, see `ModelParams`.
  52. """
  53. meta = JSONField()
  54. """
  55. Holds a JSON encoded blob of metadata, see `ModelMeta`.
  56. """
  57. updated_at = BigIntegerField()
  58. created_at = BigIntegerField()
  59. class Meta:
  60. database = DB
  61. class ModelModel(BaseModel):
  62. id: str
  63. user_id: str
  64. base_model_id: Optional[str] = None
  65. name: str
  66. params: ModelParams
  67. meta: ModelMeta
  68. updated_at: int # timestamp in epoch
  69. created_at: int # timestamp in epoch
  70. ####################
  71. # Forms
  72. ####################
  73. class ModelResponse(BaseModel):
  74. id: str
  75. name: str
  76. meta: ModelMeta
  77. updated_at: int # timestamp in epoch
  78. created_at: int # timestamp in epoch
  79. class ModelForm(BaseModel):
  80. id: str
  81. base_model_id: Optional[str] = None
  82. name: str
  83. meta: ModelMeta
  84. params: ModelParams
  85. class ModelsTable:
  86. def __init__(
  87. self,
  88. db: pw.SqliteDatabase | pw.PostgresqlDatabase,
  89. ):
  90. self.db = db
  91. self.db.create_tables([Model])
  92. def insert_new_model(
  93. self, form_data: ModelForm, user_id: str
  94. ) -> Optional[ModelModel]:
  95. model = ModelModel(
  96. **{
  97. **form_data.model_dump(),
  98. "user_id": user_id,
  99. "created_at": int(time.time()),
  100. "updated_at": int(time.time()),
  101. }
  102. )
  103. try:
  104. result = Model.create(**model.model_dump())
  105. if result:
  106. return model
  107. else:
  108. return None
  109. except Exception as e:
  110. print(e)
  111. return None
  112. def get_all_models(self) -> List[ModelModel]:
  113. return [ModelModel(**model_to_dict(model)) for model in Model.select()]
  114. def get_model_by_id(self, id: str) -> Optional[ModelModel]:
  115. try:
  116. model = Model.get(Model.id == id)
  117. return ModelModel(**model_to_dict(model))
  118. except:
  119. return None
  120. def update_model_by_id(self, id: str, model: ModelForm) -> Optional[ModelModel]:
  121. try:
  122. # update only the fields that are present in the model
  123. query = Model.update(**model.model_dump()).where(Model.id == id)
  124. query.execute()
  125. model = Model.get(Model.id == id)
  126. return ModelModel(**model_to_dict(model))
  127. except Exception as e:
  128. print(e)
  129. return None
  130. def delete_model_by_id(self, id: str) -> bool:
  131. try:
  132. query = Model.delete().where(Model.id == id)
  133. query.execute()
  134. return True
  135. except:
  136. return False
  137. Models = ModelsTable(DB)