/ src / api / models.py
models.py
 1  """Pydantic models for OpenAI-compatible API."""
 2  
 3  
 4  from pydantic import BaseModel, Field
 5  
 6  
 7  class Message(BaseModel):
 8      """Chat message."""
 9  
10      role: str = Field(..., description="Role of the message sender (user, assistant, system)")
11      content: str = Field(..., description="Content of the message")
12  
13  
14  class ChatCompletionRequest(BaseModel):
15      """Request body for chat completions."""
16  
17      model: str = Field(default="smpte-copilot", description="Model identifier")
18      messages: list[Message] = Field(..., description="List of messages in the conversation")
19      temperature: float | None = Field(default=0.7, ge=0, le=2, description="Sampling temperature")
20      max_tokens: int | None = Field(default=None, description="Maximum tokens to generate")
21      stream: bool | None = Field(default=False, description="Whether to stream responses")
22      top_p: float | None = Field(default=1.0, ge=0, le=1, description="Nucleus sampling parameter")
23  
24  
25  class ChatCompletionChoice(BaseModel):
26      """A single completion choice."""
27  
28      index: int
29      message: Message
30      finish_reason: str = "stop"
31  
32  
33  class Usage(BaseModel):
34      """Token usage statistics."""
35  
36      prompt_tokens: int
37      completion_tokens: int
38      total_tokens: int
39  
40  
41  class ChatCompletionResponse(BaseModel):
42      """Response body for chat completions."""
43  
44      id: str
45      object: str = "chat.completion"
46      created: int
47      model: str
48      choices: list[ChatCompletionChoice]
49      usage: Usage
50  
51  
52  class RAGQueryRequest(BaseModel):
53      """Request body for RAG query endpoint."""
54  
55      query: str = Field(..., description="User's question or query text")
56  
57  
58  class Citation(BaseModel):
59      """A single citation from retrieved documents."""
60  
61      id: int = Field(..., description="Citation index (1-based)")
62      source: str | None = Field(default=None, description="Source document name or path")
63      page: int | None = Field(default=None, description="Page number in the source document")
64      score: float = Field(..., description="Relevance score")
65      content: str = Field(..., description="Content of the retrieved chunk")
66  
67  
68  class RAGQueryResponse(BaseModel):
69      """Response body for RAG query endpoint with citations."""
70  
71      response: str = Field(..., description="LLM-generated response with citation markers")
72      citations: list[Citation] = Field(default_factory=list, description="List of citations")