-
Notifications
You must be signed in to change notification settings - Fork 571
/
Copy pathchat.py
192 lines (164 loc) · 5.6 KB
/
chat.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from datetime import datetime, timezone
from enum import Enum
from typing import List, NotRequired, Optional, TypedDict
from pydantic import BaseModel, Field
from sqlalchemy import Column, DateTime, Index, String, func
from sqlalchemy.dialects.postgresql import JSONB
from sqlmodel import Field as SQLModelField
from sqlmodel import SQLModel
from models.db import get_session
class ChatMessageAttachmentType(str, Enum):
"""Type of chat message attachment."""
LINK = "link"
IMAGE = "image"
FILE = "file"
class AuthorType(str, Enum):
"""Type of message author."""
AGENT = "agent"
TRIGGER = "trigger"
SKILL = "skill"
TELEGRAM = "telegram"
TWITTER = "twitter"
WEB = "web"
SYSTEM = "system"
class ChatMessageAttachment(TypedDict):
"""Chat message attachment model.
An attachment can be a link, image, or file that is associated with a chat message.
"""
type: ChatMessageAttachmentType = Field(
...,
description="Type of the attachment (link, image, or file)",
examples=["link"],
)
url: str = Field(
...,
description="URL of the attachment",
examples=["https://example.com/image.jpg"],
)
class ChatMessageSkillCall(TypedDict):
"""TypedDict for skill call details."""
name: str
parameters: dict
success: bool
response: NotRequired[
str
] # Optional response from the skill call, trimmed to 100 characters
error_message: NotRequired[str] # Optional error message from the skill call
class ChatMessageRequest(BaseModel):
"""Request model for chat messages.
This model represents the request body for creating a new chat message.
It contains the necessary fields to identify the chat context, user,
and message content, along with optional attachments.
"""
chat_id: str = Field(
...,
description="Unique identifier for the chat thread",
examples=["chat-123"],
min_length=1,
)
user_id: str = Field(
...,
description="Unique identifier of the user sending the message",
examples=["user-456"],
min_length=1,
)
message: str = Field(
...,
description="Content of the message",
examples=["Hello, how can you help me today?"],
min_length=1,
)
attachments: Optional[List[ChatMessageAttachment]] = Field(
None,
description="Optional list of attachments (links, images, or files)",
examples=[[{"type": "link", "url": "https://example.com"}]],
)
class Config:
"""Pydantic model configuration."""
use_enum_values = True
json_schema_extra = {
"example": {
"chat_id": "chat-123",
"user_id": "user-456",
"message": "Hello, how can you help me today?",
"attachments": [
{
"type": "link",
"url": "https://example.com",
}
],
}
}
class ChatMessage(SQLModel, table=True):
"""Chat message model."""
__tablename__ = "chat_messages"
__table_args__ = (Index("ix_chat_messages_chat_id", "chat_id"),)
id: str = SQLModelField(
primary_key=True,
description="Unique identifier for the chat message",
)
agent_id: str = SQLModelField(
description="ID of the agent this message belongs to",
)
chat_id: str = SQLModelField(
description="ID of the chat this message belongs to",
)
author_id: str = SQLModelField(
description="ID of the message author",
)
author_type: AuthorType = SQLModelField(
sa_column=Column(String, nullable=False),
description="Type of the message author",
)
message: str = SQLModelField(
description="Content of the message",
)
attachments: Optional[List[ChatMessageAttachment]] = SQLModelField(
default=None,
sa_column=Column(JSONB, nullable=True),
description="List of attachments in the message",
)
skill_calls: Optional[List[ChatMessageSkillCall]] = SQLModelField(
default=None,
sa_column=Column(JSONB, nullable=True),
description="Skill call details",
)
input_tokens: int = SQLModelField(
default=0,
description="Number of tokens in the input message",
)
output_tokens: int = SQLModelField(
default=0,
description="Number of tokens in the output message",
)
time_cost: float = SQLModelField(
default=0.0,
description="Time cost for the message in seconds",
)
cold_start_cost: float = SQLModelField(
default=0.0,
description="Cost for the cold start of the message in seconds",
)
created_at: datetime = SQLModelField(
default_factory=lambda: datetime.now(timezone.utc),
sa_type=DateTime(timezone=True),
sa_column_kwargs={"server_default": func.now()},
nullable=False,
description="Timestamp when this message was created",
)
class Config:
use_enum_values = True
json_encoders = {datetime: lambda v: v.isoformat(timespec="milliseconds")}
def __str__(self):
resp = ""
if self.skill_calls:
for call in self.skill_calls:
resp += f"{call['name']} {call['parameters']}: {call['response'] if call['success'] else call['error_message']}\n"
resp += "\n"
resp += self.message
return resp
async def save(self):
async with get_session() as db:
db.add(self)
await db.commit()
await db.refresh(self)