forked from jenkinsci/resources-ai-chatbot-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemas.py
More file actions
248 lines (197 loc) · 6.68 KB
/
schemas.py
File metadata and controls
248 lines (197 loc) · 6.68 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""
Schemas for the chatbot API.
This module defines the request and response data models exchanged between
clients and the chatbot API endpoints.
"""
from enum import Enum
from typing import List, Optional
from pydantic import BaseModel, field_validator, model_validator
class FileType(str, Enum):
"""Enum representing supported file types."""
TEXT = "text"
IMAGE = "image"
class FileAttachment(BaseModel):
"""
Represents a processed file attachment.
Fields:
filename (str): Original name of the uploaded file.
type (FileType): Type of file - TEXT or IMAGE.
content (str): Text content or base64 encoded image data.
mime_type (str): MIME type of the file.
"""
filename: str
type: FileType
content: str
mime_type: str
class ChatRequest(BaseModel):
"""
Represents a user message submitted to the chatbot.
Fields:
message (str): The user's input message.
Validation:
- Rejects messages that are empty.
"""
message: str
@field_validator("message")
def message_must_not_be_empty(cls, v): # pylint: disable=no-self-argument
"""Validator that checks that a message is not empty."""
if not v.strip():
raise ValueError("Message cannot be empty.")
return v
class ChatRequestWithFiles(BaseModel):
"""
Represents a user message with optional file attachments.
Fields:
message (str): The user's input message.
files (List[FileAttachment]): Optional list of file attachments.
Validation:
- Rejects when both message is empty and no files are attached.
"""
message: str = ""
files: Optional[List[FileAttachment]] = None
@model_validator(mode="after")
def validate_message_or_files(self):
"""Validates that at least message or files are present."""
has_message = bool(self.message and self.message.strip())
has_files = bool(self.files and len(self.files) > 0)
if not has_message and not has_files:
raise ValueError("Either message or files must be provided.")
return self
class ChatResponse(BaseModel):
"""
Represents the chatbot's reply.
"""
reply: str
class ChatResponseWithFiles(BaseModel):
"""
Represents the chatbot's reply with information about processed files.
Fields:
reply (str): The chatbot's text response.
processed_files (List[str]): List of filenames that were processed.
"""
reply: str
processed_files: Optional[List[str]] = None
class FileUploadResponse(BaseModel):
"""
Response model for file upload operations.
Fields:
success (bool): Whether the upload was successful.
filename (str): Name of the uploaded file.
type (str): Type of file processed ("text" or "image").
message (str): Status message.
"""
success: bool
filename: str
type: str
message: str
class SupportedExtensionsResponse(BaseModel):
"""
Response model for supported file extensions.
Fields:
text (List[str]): List of supported text file extensions.
image (List[str]): List of supported image file extensions.
max_text_size_mb (float): Maximum text file size in MB.
max_image_size_mb (float): Maximum image file size in MB.
"""
text: List[str]
image: List[str]
max_text_size_mb: float
max_image_size_mb: float
class SessionResponse(BaseModel):
"""
Response model when a new chat session is created.
"""
session_id: str
class DeleteResponse(BaseModel):
"""
Response model when a session is successfully deleted.
"""
message: str
class MessageItem(BaseModel):
"""
Represents a single message in the conversation history.
Fields:
role (str): The role of the message sender ('human' or 'ai').
content (str): The text content of the message.
"""
role: str
content: str
class MessageHistoryResponse(BaseModel):
"""
Response model for retrieving the conversation history of a session.
Fields:
session_id (str): The session identifier.
messages (List[MessageItem]): Ordered list of messages in the session.
"""
session_id: str
messages: List[MessageItem]
class SessionInfo(BaseModel):
"""
Basic metadata for a single active session.
Fields:
session_id (str): The session identifier.
message_count (int): Number of messages exchanged in the session.
last_accessed (str): ISO-8601 timestamp of last activity.
"""
session_id: str
message_count: int
last_accessed: str
class SessionListResponse(BaseModel):
"""
Response model for listing all active sessions.
Fields:
sessions (List[SessionInfo]): Ordered list of active sessions.
total (int): Total number of active sessions returned.
page (int): Current page number (1-indexed).
page_size (int): Number of sessions per page.
"""
sessions: List[SessionInfo]
total: int
page: int
page_size: int
class QueryType(Enum):
"""
Enum that represents the possible query types:
- MULTI -> Represents a multi-question query.
- SIMPLE -> Represents a single scope query.
"""
MULTI = 'MULTI'
SIMPLE = 'SIMPLE'
def is_valid_query_type(input_str: str) -> bool:
"""
Check if the given string is a valid member of the QueryType enum.
Args:
input_str (str): The string to validate.
Returns:
bool: True if the string is a valid QueryType member, False otherwise.
"""
return input_str in QueryType.__members__
def str_to_query_type(input_str: str) -> QueryType:
"""
Convert a string to its corresponding QueryType enum member.
Args:
input_str (str): The string representation of a QueryType.
Returns:
QueryType: The corresponding enum member.
Raises:
ValueError: If the input string is not a valid QueryType.
"""
try:
return QueryType[input_str]
except KeyError as e:
raise ValueError(f"Invalid query type: {input_str}") from e
def try_str_to_query_type(query_type: str, logger) -> QueryType:
"""
Extract the generated query type. In case the query type is not
a not valid output it sets by default to MULTI, since in case it of a false
positive it won't split up the query.
Args:
query (str): The user query.
logger: The logger param.
Returns:
QueryType: the query type, either 'SIMPLE' or 'MULTI'
"""
if not is_valid_query_type(query_type):
logger.info("Not valid query type: %s. Setting to default to MULTI.", query_type)
query_type = 'MULTI'
return str_to_query_type(query_type)