File size: 2,833 Bytes
c3aef13 03eefac c3aef13 03eefac c3aef13 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# models/schemas.py
"""Pydantic models for request/response validation"""
from pydantic import BaseModel, Field, validator
from typing import List, Optional, Literal
class EmbeddingRequest(BaseModel):
"""Request model for embedding generation"""
texts: List[str] = Field(
...,
description="List of texts to embed",
example=["Hola mundo", "¿Cómo estás?"]
)
normalize: bool = Field(
default=True,
description="Whether to normalize embeddings to unit length"
)
max_length: Optional[int] = Field(
default=None,
description="Maximum sequence length (uses model default if not specified)"
)
@validator('texts')
def validate_texts(cls, v):
if not v:
raise ValueError("At least one text must be provided")
if len(v) > 50:
raise ValueError("Maximum 50 texts per request")
# Check for empty strings
if any(not text.strip() for text in v):
raise ValueError("Empty texts are not allowed")
return v
@validator('max_length')
def validate_max_length(cls, v):
if v is not None:
if v < 1:
raise ValueError("Max length must be positive")
if v > 8192:
raise ValueError("Max length cannot exceed 8192")
return v
class EmbeddingResponse(BaseModel):
"""Response model for embedding generation"""
embeddings: List[List[float]] = Field(
...,
description="List of embedding vectors"
)
model_used: str = Field(
...,
description="Model that was used"
)
dimensions: int = Field(
...,
description="Dimension of embedding vectors"
)
num_texts: int = Field(
...,
description="Number of texts processed"
)
class ModelInfo(BaseModel):
"""Information about available models"""
model_id: str = Field(
...,
description="Model identifier for API calls"
)
name: str = Field(
...,
description="Full Hugging Face model name"
)
dimensions: int = Field(
...,
description="Output embedding dimensions"
)
max_sequence_length: int = Field(
...,
description="Maximum input sequence length"
)
languages: List[str] = Field(
...,
description="Supported languages"
)
model_type: str = Field(
...,
description="Type/domain of model"
)
description: str = Field(
...,
description="Model description"
)
class ErrorResponse(BaseModel):
"""Error response model"""
detail: str = Field(
...,
description="Error message"
)
error_type: Optional[str] = Field(
default=None,
description="Type of error"
) |