feat(db): add message and chat schemas for input/output
Browse filesAdd Pydantic schemas for message and chat models including create, read and update operations. Includes relationships between chat and message models.
- db/schemas/chat.py +37 -0
- db/schemas/message.py +29 -0
db/schemas/chat.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from pydantic import BaseModel, Field
|
| 5 |
+
|
| 6 |
+
from .message import MessageRead
|
| 7 |
+
|
| 8 |
+
# -------------------
|
| 9 |
+
# SCHEMAS FOR INPUT
|
| 10 |
+
# -------------------
|
| 11 |
+
class ChatBase(BaseModel):
|
| 12 |
+
title: Optional[str] = None
|
| 13 |
+
|
| 14 |
+
# Properties to receive on creation
|
| 15 |
+
class ChatCreate(ChatBase):
|
| 16 |
+
title: str
|
| 17 |
+
|
| 18 |
+
# Properties to receive on update
|
| 19 |
+
class ChatUpdate(ChatBase):
|
| 20 |
+
title: str
|
| 21 |
+
|
| 22 |
+
# -------------------
|
| 23 |
+
# SCHEMAS FOR OUTPUT
|
| 24 |
+
# -------------------
|
| 25 |
+
# Schema for listing chats in a sidebar
|
| 26 |
+
class ChatReadSimple(BaseModel):
|
| 27 |
+
id: uuid.UUID
|
| 28 |
+
title: str
|
| 29 |
+
created_at: datetime
|
| 30 |
+
updated_at: datetime
|
| 31 |
+
|
| 32 |
+
class Config:
|
| 33 |
+
from_attributes = True
|
| 34 |
+
|
| 35 |
+
# Full schema for viewing a single chat thread with all its messages.
|
| 36 |
+
class ChatReadWithMessages(ChatReadSimple):
|
| 37 |
+
messages: list[MessageRead] = []
|
db/schemas/message.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from pydantic import BaseModel, Field, HttpUrl, computed_field
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
# -------------------
|
| 7 |
+
# SCHEMAS FOR INPUT
|
| 8 |
+
# -------------------
|
| 9 |
+
class MessageCreate(BaseModel):
|
| 10 |
+
content: str = Field(..., description="The text content of the user's message.")
|
| 11 |
+
|
| 12 |
+
# -------------------
|
| 13 |
+
# SCHEMAS FOR OUTPUT
|
| 14 |
+
# -------------------
|
| 15 |
+
class MessageRead(BaseModel):
|
| 16 |
+
id: uuid.UUID
|
| 17 |
+
role: str
|
| 18 |
+
created_at: datetime
|
| 19 |
+
|
| 20 |
+
links: Optional[list[HttpUrl]] = None
|
| 21 |
+
|
| 22 |
+
@computed_field
|
| 23 |
+
@property
|
| 24 |
+
def content(self) -> str:
|
| 25 |
+
# self here is the SQLModel Message object
|
| 26 |
+
return self.answer
|
| 27 |
+
|
| 28 |
+
class Config:
|
| 29 |
+
from_attributes = True
|