File size: 967 Bytes
34ec765
 
8909b6d
34ec765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8909b6d
34ec765
2c9f86d
 
 
 
 
34ec765
 
 
2c9f86d
34ec765
 
 
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
import uuid
from datetime import datetime
from pydantic import BaseModel, Field, computed_field
from typing import Optional

# -------------------
# SCHEMAS FOR INPUT
# -------------------
class MessageCreate(BaseModel):
    content: str = Field(..., description="The text content of the user's message.")

# -------------------
# SCHEMAS FOR OUTPUT
# -------------------
class MessageRead(BaseModel):
    id: uuid.UUID
    role: str
    created_at: datetime

    links: Optional[list[str]] = None

    raw_content: Optional[str] = Field(None, alias='content', exclude=True)
    answer: Optional[str] = Field(None, alias='answer', exclude=True)

    # Step 2: The computed field now accesses the private, guaranteed-to-exist attributes
    # on the MessageRead instance itself.
    @computed_field
    @property
    def content(self) -> str:
        return self.answer if self.answer is not None else self.raw_content

    class Config:
        from_attributes = True