Chat
Prompt
Bases: BaseModel
Data model for user input in the chat API. Represents a user-provided prompt for querying the chat service.
Attributes:
| Name | Type | Description |
|---|---|---|
prompt |
str | None
|
The user's input query. |
Source code in mycxo/boxtalk/routes/api/chat/chat.py
74 75 76 77 78 79 80 81 82 | |
answer_question(prompt, context, sql_query=None, sql_results=None)
async
Generates an answer based on a given prompt and context using an asynchronous streaming response. It streams the model's response as it becomes available, yielding chunks of data to the client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt for which the question needs to be answered. |
required |
context
|
str
|
The context related to the prompt for providing an accurate answer. |
required |
sql_query
|
str
|
The SQL query generated by the language model. |
None
|
sql_results
|
list
|
The results of the SQL query execution. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
str |
The streamed response from the model. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If there are issues with connecting to the API or parsing data from the response. |
Source code in mycxo/boxtalk/routes/api/chat/chat.py
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 249 250 251 252 253 254 | |
background_chat(messages, return_sql_info=False)
async
Handles the creation and processing of chat messages with the language model. It sends messages to the model, parses the response, and interacts with the database to fetch results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list
|
A list of dictionaries representing the conversation history. |
required |
return_sql_info
|
bool
|
If True, returns the SQL query and results along with the chat response. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
|
list or tuple: - If return_sql_info is False, returns the query results. - If return_sql_info is True, returns a tuple containing: - sql_query (str): The SQL query generated by the language model. - sql_results (list): The results of the SQL query execution. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If there is an issue creating the conversation. |
NoResultsException
|
If no results are found in the database for the provided query. |
SQLQueryException
|
If there is an issue with the SQL query execution. |
Source code in mycxo/boxtalk/routes/api/chat/chat.py
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 | |
chat(prompt, return_sql_info=Query(False, description='Set to True to return SQL query and results'), session=Depends(my_cxo_db_conn.get_db_sess))
async
Processes a user's prompt by querying a database and generating responses using embeddings and similarity calculations. It retries up to three times in case of unsuccessful queries, aiming to refine the results and provide better responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
Prompt
|
The user's input query. |
required |
return_sql_info
|
bool
|
If True, returns the SQL query and results along with the chat response. Default is False. |
Query(False, description='Set to True to return SQL query and results')
|
session
|
AsyncSession
|
AsyncSession: Database session for executing queries. |
Depends(get_db_sess)
|
Returns:
| Name | Type | Description |
|---|---|---|
StreamingResponse |
|
Raises:
| Type | Description |
|---|---|
HTTPException
|
If there is an issue processing the request. |
Source code in mycxo/boxtalk/routes/api/chat/chat.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
get_include_list()
Reads the 'include.json' file in STATIC_DIR and returns the list of tables to include. Assumes the JSON file contains a 'tables' key with a list of table names.
Returns:
| Name | Type | Description |
|---|---|---|
list |
List of tables to include. |
Source code in mycxo/boxtalk/routes/api/chat/chat.py
49 50 51 52 53 54 55 56 57 58 59 | |
get_merchant_notes()
Reads merchant notes from the 'merchant_notes.md' file in STATIC_DIR and returns its content. Assumes the file exists at the specified path.
Returns:
| Name | Type | Description |
|---|---|---|
str |
The content of the merchant notes file. |
Source code in mycxo/boxtalk/routes/api/chat/chat.py
62 63 64 65 66 67 68 69 70 71 | |
get_synonym_map()
Reads the synonym map from the 'synonym_map.md' file in STATIC_DIR and returns its content as a string. Assumes the file exists at the specified path.
Returns:
| Name | Type | Description |
|---|---|---|
str |
The content of the synonym map file. |
Source code in mycxo/boxtalk/routes/api/chat/chat.py
37 38 39 40 41 42 43 44 45 46 | |
get_tables_to_query(prompt)
async
Generates a list of tables relevant to a given prompt by rendering Jinja2 templates for system and user messages. It uses the synonym map, include list, and merchant notes to craft a detailed prompt for the language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The user's query to identify relevant tables. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
JSON string containing table information. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If there is an error in creating campaigns or processing the response from the chat completion client. |
Source code in mycxo/boxtalk/routes/api/chat/chat.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |