KnowledgeBase
The KnowledgeBase class is the main interface for working with dsRAG. It handles document processing, storage, and retrieval.
Public Methods
The following methods are part of the public API:
__init__
: Initialize a new KnowledgeBase instanceadd_document
: Add a single document to the knowledge baseadd_documents
: Add multiple documents in paralleldelete
: Delete the entire knowledge base and all associated datadelete_document
: Delete a specific document from the knowledge basequery
: Search the knowledge base with one or more queries
Initialize a KnowledgeBase instance.
PARAMETER | DESCRIPTION |
---|---|
kb_id
|
Unique identifier for the knowledge base.
TYPE:
|
title
|
Title of the knowledge base. Defaults to "".
TYPE:
|
supp_id
|
Supplementary identifier. Defaults to "".
TYPE:
|
description
|
Description of the knowledge base. Defaults to "".
TYPE:
|
language
|
Language code for the knowledge base. Defaults to "en".
TYPE:
|
storage_directory
|
Base directory for storing files. Defaults to "~/dsRAG".
TYPE:
|
embedding_model
|
Model for generating embeddings. Defaults to OpenAIEmbedding.
TYPE:
|
reranker
|
Model for reranking results. Defaults to CohereReranker.
TYPE:
|
auto_context_model
|
LLM for generating context. Defaults to OpenAIChatAPI.
TYPE:
|
vector_db
|
Vector database for storing embeddings. Defaults to BasicVectorDB.
TYPE:
|
chunk_db
|
Database for storing text chunks. Defaults to BasicChunkDB.
TYPE:
|
file_system
|
File system for storing images. Defaults to LocalFileSystem.
TYPE:
|
exists_ok
|
Whether to load existing KB if it exists. Defaults to True.
TYPE:
|
save_metadata_to_disk
|
Whether to persist metadata. Defaults to True.
TYPE:
|
metadata_storage
|
Storage for KB metadata. Defaults to LocalMetadataStorage.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If KB exists and exists_ok is False. |
Source code in dsrag/knowledge_base.py
Functions
add_document
add_document(doc_id: str, text: str = '', file_path: str = '', document_title: str = '', auto_context_config: dict = {}, file_parsing_config: dict = {}, semantic_sectioning_config: dict = {}, chunking_config: dict = {}, chunk_size: int = None, min_length_for_chunking: int = None, supp_id: str = '', metadata: dict = {})
Add a document to the knowledge base.
This method processes and adds a document to the knowledge base. The document can be provided either as text or as a file path. The document will be processed according to the provided configuration parameters.
Note
Either text or file_path must be provided. If both are provided, text takes precedence. The document processing flow is: 1. File parsing (if file_path provided) 2. Semantic sectioning (if enabled) 3. Chunking 4. AutoContext 5. Embedding 6. Storage in vector and chunk databases
Source code in dsrag/knowledge_base.py
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 433 434 435 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 |
|
add_documents
add_documents(documents: List[Dict[str, Union[str, dict]]], max_workers: int = 1, show_progress: bool = True, rate_limit_pause: float = 1.0) -> List[str]
Add multiple documents to the knowledge base in parallel.
PARAMETER | DESCRIPTION |
---|---|
documents
|
List of document dictionaries. Each must contain: - 'doc_id' (str): Unique identifier for the document And either: - 'text' (str): The document content, or - 'file_path' (str): Path to the document file Optional keys: - 'document_title' (str): Document title - 'auto_context_config' (dict): AutoContext configuration - 'file_parsing_config' (dict): File parsing configuration - 'semantic_sectioning_config' (dict): Semantic sectioning configuration - 'chunking_config' (dict): Chunking configuration - 'supp_id' (str): Supplementary identifier - 'metadata' (dict): Additional metadata
TYPE:
|
max_workers
|
Maximum number of worker threads. Defaults to 1.
TYPE:
|
show_progress
|
Whether to show a progress bar. Defaults to True.
TYPE:
|
rate_limit_pause
|
Pause between uploads in seconds. Defaults to 1.0.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[str]
|
List[str]: List of successfully uploaded document IDs. |
Note
Be sure to use thread-safe VectorDB and ChunkDB implementations when max_workers > 1. The default implementations (BasicVectorDB and BasicChunkDB) are not thread-safe.
Source code in dsrag/knowledge_base.py
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 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
|
delete
Delete the knowledge base and all associated data.
Removes all documents, vectors, chunks, and metadata associated with this KB.
Source code in dsrag/knowledge_base.py
delete_document
Delete a document from the knowledge base.
PARAMETER | DESCRIPTION |
---|---|
doc_id
|
ID of the document to delete.
TYPE:
|
Source code in dsrag/knowledge_base.py
query
query(search_queries: list[str], rse_params: Union[Dict, str] = 'balanced', latency_profiling: bool = False, metadata_filter: Optional[MetadataFilter] = None, return_mode: str = 'text') -> list[dict]
Query the knowledge base to retrieve relevant segments.
Source code in dsrag/knowledge_base.py
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 |
|
KB Components
Vector Databases
VectorDB
Bases: ABC
Functions
add_vectors
abstractmethod
delete
abstractmethod
remove_document
abstractmethod
search
abstractmethod
search(query_vector, top_k: int = 10, metadata_filter: Optional[dict] = None) -> list[VectorSearchResult]
Retrieve the top-k closest vectors to a given query vector. - needs to return results as list of dictionaries in this format: { 'metadata': { 'doc_id': doc_id, 'chunk_index': chunk_index, 'chunk_header': chunk_header, 'chunk_text': chunk_text }, 'similarity': similarity, }
Source code in dsrag/database/vector/db.py
Chunk Databases
ChunkDB
Bases: ABC
Functions
add_document
abstractmethod
delete
abstractmethod
get_all_doc_ids
abstractmethod
get_chunk_page_numbers
abstractmethod
Retrieve the page numbers of a specific chunk from a given document ID.
get_chunk_text
abstractmethod
get_document
abstractmethod
get_document_summary
abstractmethod
Retrieve the document summary of a specific chunk from a given document ID.
get_document_title
abstractmethod
Retrieve the document title of a specific chunk from a given document ID.
get_is_visual
abstractmethod
Retrieve the is_visual flag of a specific chunk from a given document ID.
get_section_summary
abstractmethod
Retrieve the section summary of a specific chunk from a given document ID.
get_section_title
abstractmethod
Retrieve the section title of a specific chunk from a given document ID.
remove_document
abstractmethod
Embedding Models
Embedding
Rerankers
Reranker
Bases: ABC
LLM Providers
LLM
Bases: ABC
Functions
make_llm_call
abstractmethod
Takes in chat_messages (OpenAI format) and returns the response from the LLM as a string.
File Systems
FileSystem
Bases: ABC
Source code in dsrag/dsparse/file_parsing/file_system.py
Functions
load_data
abstractmethod
Load JSON data from a file Args: kb_id: Knowledge base ID doc_id: Document ID data_name: Name of the data to load (e.g. "elements" for elements.json)