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
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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
|
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
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 |
|
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
700 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 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 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 |
|
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)