import uuid from django.conf import settings from django.db import models from django.db.models import Q class ModelProvider(models.Model): name = models.CharField(max_length=120) base_url = models.URLField(max_length=500) api_key_encrypted = models.TextField(blank=True) enabled = models.BooleanField(default=True) default_timeout_seconds = models.PositiveIntegerField(default=60) image_egress_approved = models.BooleanField(default=False) image_egress_approved_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="approved_image_model_providers", ) image_egress_approved_at = models.DateTimeField(null=True, blank=True) credential_updated_at = models.DateTimeField(null=True, blank=True) credential_updated_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="updated_model_provider_credentials", ) archived_at = models.DateTimeField(null=True, blank=True, db_index=True) archived_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="archived_model_providers", ) created_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="created_model_providers", ) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ["name"] constraints = [ models.UniqueConstraint( fields=["name"], condition=Q(archived_at__isnull=True), name="unique_active_model_provider_name", ) ] class ModelDeployment(models.Model): class StructuredOutputMode(models.TextChoices): UNCONFIGURED = "unconfigured", "未配置" NATIVE_JSON_SCHEMA = "native_json_schema", "原生 JSON Schema" JSON_OBJECT = "json_object", "JSON Object" PROMPT_SCHEMA = "prompt_schema", "Prompt Schema" NONE = "none", "不支持" class StructuredOutputTestStatus(models.TextChoices): UNTESTED = "untested", "未验证" PASSED = "passed", "已通过" FAILED = "failed", "验证失败" provider = models.ForeignKey( ModelProvider, on_delete=models.PROTECT, related_name="deployments", ) display_name = models.CharField(max_length=120) model_name = models.CharField(max_length=160) capability = models.CharField(max_length=40, default="text_chat") default_temperature = models.FloatField(default=0.2) max_output_tokens = models.PositiveIntegerField(default=2048) structured_output_mode = models.CharField( max_length=32, choices=StructuredOutputMode.choices, default=StructuredOutputMode.UNCONFIGURED, ) structured_output_test_status = models.CharField( max_length=16, choices=StructuredOutputTestStatus.choices, default=StructuredOutputTestStatus.UNTESTED, ) structured_output_verified_at = models.DateTimeField(null=True, blank=True) structured_output_test_message = models.TextField(blank=True) enabled = models.BooleanField(default=True) archived_at = models.DateTimeField(null=True, blank=True, db_index=True) archived_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="archived_model_deployments", ) archived_via_provider = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: ordering = ["provider__name", "display_name"] constraints = [ models.UniqueConstraint( fields=["provider", "model_name"], condition=Q(archived_at__isnull=True), name="unique_active_provider_model_name", ) ] class ModelInvocation(models.Model): public_id = models.UUIDField(default=uuid.uuid4, unique=True, editable=False) deployment = models.ForeignKey( ModelDeployment, null=True, blank=True, on_delete=models.SET_NULL, related_name="invocations", ) invocation_type = models.CharField(max_length=40, default="model_test") status = models.CharField(max_length=24, default="queued") model_snapshot = models.JSONField(default=dict, blank=True) input_snapshot = models.JSONField(default=dict, blank=True) output_text = models.TextField(blank=True) input_tokens = models.PositiveIntegerField(null=True, blank=True) output_tokens = models.PositiveIntegerField(null=True, blank=True) duration_ms = models.PositiveIntegerField(default=0) retry_count = models.PositiveIntegerField(default=0) error_code = models.CharField(max_length=80, blank=True) error_message = models.TextField(blank=True) diagnostics_scrubbed_at = models.DateTimeField(null=True, blank=True) created_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL, related_name="model_invocations", ) created_at = models.DateTimeField(auto_now_add=True) started_at = models.DateTimeField(null=True, blank=True) finished_at = models.DateTimeField(null=True, blank=True) class Meta: ordering = ["-created_at"] indexes = [ models.Index(fields=["status", "created_at"]), models.Index(fields=["invocation_type", "created_at"]), models.Index(fields=["deployment", "created_at"]), ]