File size: 2,514 Bytes
9d1ab9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
KAI_API Pydantic Models
-----------------------
Request and response schemas for the API.
"""

from pydantic import BaseModel, Field
from typing import Optional
from datetime import datetime


class ChatRequest(BaseModel):
    """Request body for POST /chat"""
    message: str = Field(
        ...,
        description="The user's message/prompt to send to the AI",
        min_length=1,
        max_length=32000,
    )
    model: Optional[str] = Field(
        default=None,
        description="AI model to use (e.g., 'gpt-4o-mini'). Defaults to best available.",
    )
    provider: Optional[str] = Field(
        default="auto",
        description="Provider to use: 'auto', 'g4f', or 'pollinations'. If omitted, tries all.",
    )
    system_prompt: Optional[str] = Field(
        default=None,
        description="Optional system prompt to set the AI's behavior",
    )


class ChatResponse(BaseModel):
    """Response body for POST /chat"""
    response: str = Field(description="The AI-generated response")
    model: str = Field(description="The model that generated the response")
    provider: str = Field(description="The provider that handled the request")
    attempts: Optional[int] = Field(
        default=1,
        description="Number of model+provider combinations tried before success",
    )
    response_time_ms: Optional[float] = Field(
        default=None,
        description="Total response time in milliseconds",
    )
    timestamp: str = Field(
        default_factory=lambda: datetime.utcnow().isoformat() + "Z",
        description="UTC timestamp of the response",
    )


class ErrorResponse(BaseModel):
    """Error response body"""
    error: str = Field(description="Error message")
    detail: Optional[str] = Field(default=None, description="Detailed error info")


class ModelInfo(BaseModel):
    """Model information"""
    model: str
    provider: str


class ModelsResponse(BaseModel):
    """Response body for GET /models"""
    models: list[ModelInfo]
    total: int


class ProviderHealth(BaseModel):
    """Health status for a single provider"""
    provider: str
    status: str  # "healthy", "unhealthy", "unknown"
    response_time_ms: Optional[float] = None
    error: Optional[str] = None


class HealthResponse(BaseModel):
    """Response body for GET /health"""
    status: str  # "healthy", "degraded", "unhealthy"
    providers: list[ProviderHealth]
    timestamp: str = Field(
        default_factory=lambda: datetime.utcnow().isoformat() + "Z"
    )