Reference
document_chat()
Serves the document chat page using cached content based on the provided content ID.
Retrieves cached data (title and content) for a given contentId passed as a query parameter and renders a chat interface for continued conversation with the document.
Examples:
None
Returns:
Type | Description |
---|---|
Any
|
Rendered HTML page (documentChat.html) with: |
Any
|
|
Any
|
|
Any
|
|
Source code in src/main.py
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
|
handle_chat_message(data)
Handles incoming chat messages and generates a streamed response using cached document context.
This function is triggered via a Socket.IO event when the user sends a new chat message related to a previously processed PDF. It loads the relevant cached document data and chat history, configures the Gemini model, and streams the generated response back to the frontend in real time.
The function also updates the chat history in the cache after responding, enabling continued conversation with memory of previous exchanges.
Examples:
>>> handle_chat_message({
"input": "What are the risks mentioned in the document?",
"contentId": "content-pdf0_3",
"output_size": "medium",
"slider_value": 0.5,
...
})
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict
|
A dictionary containing chat message data and UI parameters. Expected keys include: - "input": User's chat message (prompt) (str). - "contentId": The ID of the document container (str). - "output_size": Desired response length (str). - "choosen_model": Selected Gemini model (str). - "slider_value": Level of detail or verbosity (float or str). - "show_pages_checkbox": Whether to include page numbers (bool or str). - "change_length_checkbox": Whether the output size can be adjusted (bool or str). |
required |
Returns:
Type | Description |
---|---|
None
|
None. Results are emitted via Socket.IO: - "receive_chat_message": Streams chat responses to the client. - "error": Emits errors if input is invalid or processing fails. - "stream_stopped": Indicates the end of streaming or failure. |
Source code in src/main.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
|
handle_clear_cache()
Clears all cached chat instances (UUIDs) created by the current client's session. Triggered by a button press on the main page.
Source code in src/main.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
handle_disconnect()
Handles cache cleanup for all UUIDs created by a client's session.
Source code in src/main.py
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
|
handle_reset_chat_history(data)
Finds a specific chat session by its UUID and resets its history, keeping only the first two messages (initial prompt and response).
Source code in src/main.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
handle_stop()
Stops the current processing stream when triggered by the client.
This function sets the global streaming flag to False, effectively stopping any ongoing data generation or response processing.
Examples:
None
Returns:
Type | Description |
---|---|
None
|
None |
Source code in src/main.py
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
|
index()
Serves the main page of the application with a list of available PDF files.
This route handler is mapped to the root URL ("/"). When accessed, it logs that the application is running, scans the designated directory for PDF files, and renders the homepage template with those files listed.
This enables users to see which documents are available for analysis.
Examples:
None
Returns:
Type | Description |
---|---|
str
|
A rendered HTML page (index.html) with the following context: |
str
|
|
Source code in src/main.py
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 |
|
process_text(data)
Handles the initial processing of user input and selected PDFs using a generative model.
This function is triggered via a Socket.IO event when a user initiates processing. It validates the input prompt and selected PDF files, sets up the selected Gemini model, and processes each PDF using retrieval-augmented generation (RAG). The response is streamed back to the client in real time, rendered in markdown, and cached for future access.
Each processed PDF results in the creation of a content container, which is dynamically sent to the frontend. If errors occur during processing, they are logged and sent to the client as error events.
Examples:
Triggered internally by Socket.IO when the user starts processing:
>>> process_text({
"input": "Summarize the risks mentioned",
"pdfFiles": ["KNF_2022_01.pdf"],
"output_size": "short",
"show_pages_checkbox": True,
"choosen_model": "gemini-2.0-flash",
...
})
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict
|
A dictionary containing user input and options. Expected keys include: - "input": User’s prompt (str). - "pdfFiles": List of PDF filenames to process (List[str]). - "output_size": Approximate length of the response (str). - "show_pages_checkbox": Whether to include page numbers (bool or str). - "choosen_model": Selected Gemini model (str). - "change_length_checkbox": Whether output length can vary (bool or str). - "slider_value": Float controlling verbosity or detail (str or float). - "ragDocSlider": Toggle between RAG and document mode (str). - Other UI flags or settings. |
required |
Returns:
Type | Description |
---|---|
None
|
None. Results are streamed to the client via Socket.IO events: - "new_container": Sends a new HTML container for each PDF. - "update_content": Streams chunks of the model's response. - "processing_complete_for_container": Signals PDF completion. - "error": Sends error messages if validation or processing fails. - "stream_stopped": Indicates the end of the streaming session. |
Source code in src/main.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 255 256 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 319 320 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 |
|
extract_text_from_pdf(pdf_path)
Extracts text from a PDF file.
This function reads a PDF file, extracts text from each page, and concatenates the extracted text into a single string. If an error occurs during extraction, it logs the error and returns an empty string.
Examples:
>>> extract_text_from_pdf(Path("document.pdf"))
'This is the extracted text from the PDF document.'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pdf_path
|
Path
|
A Path object representing the PDF file to extract text from. |
required |
Returns:
Type | Description |
---|---|
A string containing the extracted text from the PDF. If an error occurs, |
|
an empty string is returned. |
Raises:
Type | Description |
---|---|
Exception
|
Any exceptions encountered while reading or extracting text |
Source code in src/backend/extract_text.py
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 |
|
scrape_knf(scraped_dir, num_retries, user_agent_list)
Scrapes pdf files from KNF url.
This function scrapes pdf files from a KNF url. For a certain number of tries it masks under an agent from given agent list and downloads the file into a directory. It adds a document ID If an error occurs during scraping, it logs the error message.
Examples:
>>> scrape_knf(10, ["Mozilla/5.0", "Mozilla/4.0"])
None
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_retries
|
int
|
An int describing number of retries the program will |
required |
user_agent_list
|
list
|
A list of strings with user agents for masking. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
Exception
|
If an error occurs during processing, it is logged and returned |
Source code in src/backend/knf_scraping.py
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 |
|
windows_safe_filename(filename)
Removes invalid characters from the file name.
This function removes any invalid character in Windows file name from the filename.
It also replaces end of line characters "
" with spaces " ". Returns the new file name.
Examples:
>>> windows_safe_filename('invalid:filename?.txt')
'invalidfilename.txt'
Args:
filename: A string containing the file name.
Returns:
A string containing file name cleansed from any invalid characters.
Raises:
None
Source code in src/backend/knf_scraping.py
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 |
|
process_chat_query_with_rag(prompt, chat_history, pdf_name, model, change_length_checkbox, enhancer_checkbox, output_size, temperature_slider_value, chroma_client, collection_name, rag_doc_slider)
Processes a chat query using RAG, incorporating conversation history.
Retrieves relevant context from a document collection based on the current user prompt. It then combines the (potentially enhanced) prompt with this context and the existing chat history, queries the generative model, and streams the response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The user's current query/message in the conversation. |
required |
chat_history
|
str
|
A string representation of the conversation history. (Note: Assumes model.start_chat() accepts this string format). |
required |
pdf_name
|
str
|
The identifier/name of the document (for RAG context and logging). |
required |
model
|
GenerativeModel
|
The generative AI model instance (e.g., genai.GenerativeModel). |
required |
change_length_checkbox
|
str
|
String flag ("True"/"False") to modify response length. |
required |
enhancer_checkbox
|
str
|
String flag ("True"/"False") for prompt enhancement. |
required |
output_size
|
str
|
The desired output size (e.g., number of words). |
required |
temperature_slider_value
|
float
|
Temperature for model generation. |
required |
chroma_client
|
Client
|
The ChromaDB client instance. |
required |
collection_name
|
str
|
Name of the ChromaDB collection for this document. |
required |
rag_doc_slider
|
str
|
String flag ("True" to use all chunks from the document's collection, "False" for a default number). |
required |
Yields:
Name | Type | Description |
---|---|---|
dict |
Any
|
A dictionary for each chunk of the response or for an error.
For content: |
Source code in src/backend/process_query.py
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 |
|
process_pdf(prompt, pdf, model, change_length_checkbox, enhancer_checkbox, output_size, temperature_slider_value)
Uploads a PDF, processes it with a generative model, and streams content.
This function takes a PDF file and a prompt, uploads the file, and then calls the generative model to process the content based on the (potentially enhanced) prompt. It streams the model's response, yielding cleaned text chunks or an error dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The base prompt for processing the document. |
required |
pdf
|
Path
|
A Path object representing the PDF file to be processed. |
required |
model
|
GenerativeModel
|
The generative AI model instance (e.g., genai.GenerativeModel). |
required |
change_length_checkbox
|
str
|
String flag ("True"/"False") to indicate if output size instruction should be added. |
required |
enhancer_checkbox
|
str
|
String flag ("True"/"False") to indicate if the prompt should be enhanced. |
required |
output_size
|
str
|
The desired output size (e.g., number of words). |
required |
temperature_slider_value
|
float
|
The temperature setting for model generation. |
required |
Yields:
Name | Type | Description |
---|---|---|
dict |
Any
|
A dictionary for each chunk of the response or for an error.
For content: |
Source code in src/backend/process_query.py
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 249 250 251 252 253 254 255 256 257 258 |
|
process_query_with_rag(prompt, pdf_name, model, change_length_checkbox, enhancer_checkbox, output_size, temperature_slider_value, chroma_client, collection_name, rag_doc_slider)
Processes a query using RAG, combining it with context from a document.
This function retrieves relevant context from a specified document collection (via ChromaDB) based on the user's prompt. It then combines the (potentially enhanced) prompt with this context and queries the generative model, streaming the response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The user's base query/prompt. |
required |
pdf_name
|
str
|
The identifier/name of the document (for RAG context and logging). |
required |
model
|
GenerativeModel
|
The generative AI model instance (e.g., genai.GenerativeModel). |
required |
change_length_checkbox
|
str
|
String flag ("True"/"False") to modify response length. |
required |
enhancer_checkbox
|
str
|
String flag ("True"/"False") for prompt enhancement. |
required |
output_size
|
str
|
The desired output size (e.g., number of words). |
required |
temperature_slider_value
|
float
|
Temperature for model generation. |
required |
chroma_client
|
Client
|
The ChromaDB client instance. |
required |
collection_name
|
str
|
Name of the ChromaDB collection for this document. |
required |
rag_doc_slider
|
str
|
String flag ("True" to use all chunks from the document's collection, "False" for a default number). |
required |
Yields:
Name | Type | Description |
---|---|---|
dict |
Any
|
A dictionary for each chunk of the response or for an error.
For content: |
Source code in src/backend/process_query.py
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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
show_pages(system_prompt, show_pages_checkbox)
Add an additional string to the system prompt to instruct model to include page name in outputs.
Source code in src/backend/show_pages.py
13 14 15 16 17 18 19 20 21 22 |
|