first commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
from ..models import Model
|
||||
|
||||
|
||||
class ModelService:
|
||||
@staticmethod
|
||||
def create_model(data):
|
||||
model = Model(
|
||||
provider=data["provider"],
|
||||
name=data["name"],
|
||||
model_id=data["model_id"],
|
||||
api_key=data.get("api_key"),
|
||||
base_url=data.get("base_url"),
|
||||
is_default=data.get("is_default", False),
|
||||
is_active=data.get("is_active", True),
|
||||
)
|
||||
|
||||
if model.is_default:
|
||||
Model.query.filter_by(is_default=True).update({"is_default": False})
|
||||
|
||||
model.save()
|
||||
return model
|
||||
|
||||
@staticmethod
|
||||
def update_model(model, data):
|
||||
if "name" in data:
|
||||
model.name = data["name"]
|
||||
|
||||
if "api_key" in data:
|
||||
model.api_key = data["api_key"]
|
||||
|
||||
if "base_url" in data:
|
||||
model.base_url = data["base_url"]
|
||||
|
||||
if "is_default" in data and data["is_default"]:
|
||||
Model.query.filter(Model.id != model.id, Model.is_default == True).update(
|
||||
{"is_default": False}
|
||||
)
|
||||
model.is_default = True
|
||||
|
||||
if "is_active" in data:
|
||||
model.is_active = data["is_active"]
|
||||
|
||||
model.save()
|
||||
return model
|
||||
|
||||
@staticmethod
|
||||
def delete_model(model_id):
|
||||
model = Model.query.get(model_id)
|
||||
if model.is_default:
|
||||
raise ValueError("默认模型不能删除")
|
||||
model.delete()
|
||||
Reference in New Issue
Block a user