Storage
Storage
llama_utils.retrieval.storage
A module for managing vector Storage and retrieval.
Storage
A class to manage vector Storage and retrieval.
The Storage class is used to manage the storage and retrieval of documents. It provides methods to add documents to the store, read documents from a directory, and extract information from the documents.
Source code in src/llama_utils/retrieval/storage.py
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 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 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 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 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 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 |
|
docstore: BaseDocumentStore
property
Get the document store.
index_store: BaseIndexStore
property
Get the index store.
metadata_index: pd.DataFrame
property
Get the metadata index.
store: StorageContext
property
Get the Storage context.
vector_store
property
Get the vector store.
__init__(storage_context: StorageContext = None)
Initialize the Storage.
The constructor method takes a llama_index.core.StorageContext object that is a native llamaIndex object and and a metadata table (pandas.DataFrame-optional) as input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
storage_context
|
StorageContext
|
the StorageContext object that is created by LlamaIndex (a native llamaIndex object). |
None
|
Source code in src/llama_utils/retrieval/storage.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
__repr__()
Return a string representation of the storage.
Source code in src/llama_utils/retrieval/storage.py
218 219 220 221 222 223 224 |
|
__str__()
Return a string representation of the storage.
Source code in src/llama_utils/retrieval/storage.py
210 211 212 213 214 215 216 |
|
add_documents(docs: Sequence[Union[Document, TextNode]], generate_id: bool = True, update: bool = False)
Add node/documents to the store.
The add_documents
method adds a node to the store. The node's id is a sha256 hash generated based on the
node's text content. if the update
parameter is True and the nodes already exist the existing node will
be updated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs
|
Sequence[Union[Document, TextNode]]
|
The node/documents to add to the store. |
required |
generate_id
|
bool
|
True if you want to generate a sha256 hash number as a doc_id based on the content of the nodes. |
True
|
update
|
bool
|
True to update the document in the docstore if it already exist. |
False
|
Returns:
Type | Description |
---|---|
None
|
|
Examples:
- First create the storage object:
>>> store = Storage.create() - Then you can add documents to the store using the `add_documents` method: >>> data_path = "examples/data/essay" >>> documents = Storage.read_documents(data_path) >>> store.add_documents(documents) >>> print(store) # doctest: +SKIP <BLANKLINE> Documents: 1 Indexes: 0 <BLANKLINE> - once the documents are added successfully, they are added also to the metadata index. >>> metadata = store.metadata(as_dataframe=True) >>> print(metadata) # doctest: +SKIP file_name doc_id 0 paul_graham_essay.txt cadde590b82362fc7a5f8ce0751c5b30b11c0f81369df7... >>> docstore = store.docstore >>> print(docstore.docs) # doctest: +SKIP { 'a25111e2e59f81bb7a0e3efb48255f4a5d4f722aaf13ffd112463fb98c227092': Document( id_='a25111e2e59f81bb7a0e3efb48255f4a5d4f722aaf13ffd112463fb98c227092', embedding=None, metadata={ 'file_path': 'examples\\data\\essay\\paul-graham-essay.txt', 'file_name': 'paul-graham-essay.txt', 'file_type': 'text/plain', 'file_size': 75395, 'creation_date': '2024-10-25', 'last_modified_date': '2024-09-16' }, excluded_embed_metadata_keys=['file_name'], excluded_llm_metadata_keys=['file_name'], relationships={}, text='What I Worked On February 2021 Before college the two ...', mimetype='text/plain', start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n' ) }
Source code in src/llama_utils/retrieval/storage.py
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 |
|
apply_extractors(documents: List[Union[Document, BaseNode]], extractors: Dict[str, Dict[str, int]] = None) -> Sequence[BaseNode]
staticmethod
Extract information from a list of documents using predefined extractors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
documents
|
List[Union[Document, BaseNode]]
|
List of documents or nodes to process. Each document should be an instance of |
required |
extractors
|
Dict[str, Dict[str, Any]]
|
A dictionary defining the information extraction configuration. If not provided, default extractors will be used.
.. code-block:: rst
|
None
|
Returns:
Type | Description |
---|---|
Sequence[BaseNode]
|
A sequence of processed nodes with extracted metadata. Extracted data is stored in the node's
|
Examples:
First create a config loader object:
>>> from llama_utils.utils.config_loader import ConfigLoader
>>> config_loader = ConfigLoader()
You can extract information from a single document as follows:
>>> docs = [Document(text="Sample text", metadata={})]
>>> extractors_info = {
... "text_splitter": {"separator": " ", "chunk_size": 512, "chunk_overlap": 128},
... "title": {"nodes": 5},
... "summary": {"summaries": ["prev", "self"]}
... }
>>> extracted_nodes = Storage.apply_extractors(docs, extractors_info) # doctest: +SKIP
Parsing nodes: 100%|██████████| 1/1 [00:00<00:00, 1000.31it/s]
100%|██████████| 1/1 [00:05<00:00, 5.82s/it]
100%|██████████| 1/1 [00:00<00:00, 1.54it/s]
>>> len(extracted_nodes) # doctest: +SKIP
1
>>> print(extracted_nodes[0].metadata) # doctest: +SKIP
{
'document_title': "I'm excited to help! Unfortunately, there doesn't seem to be any text provided.
Please go ahead and share the sample text, and I'll do my best to give you a comprehensive title
that summarizes all the unique entities, titles, or themes found in it.",
'section_summary': "I apologize, but since there is no provided text, I have nothing to summarize.
Please provide the sample text, and I'll be happy to help you summarize the key topics and
entities!"
}
>>> data_path = "examples/data/essay"
>>> docs = Storage.read_documents(data_path)
>>> extractors_info = {
... "text_splitter": {"separator": " ", "chunk_size": 512, "chunk_overlap": 128},
... "title": {"nodes": 5},
... "question_answer": {"questions": 1},
... }
>>> extracted_docs = Storage.apply_extractors(docs, extractors_info) # doctest: +SKIP
Parsing nodes: 100%|██████████| 1/1 [00:00<00:00, 4.52it/s]
100%|██████████| 5/5 [00:15<00:00, 3.19s/it]
100%|██████████| 53/53 [03:46<00:00, 4.27s/it]
26%|██▋ | 14/53 [00:48<02:08, 3.29s/it]
100%|██████████| 53/53 [00:47<00:00, 1.13it/s]
>>> len(extracted_docs) # doctest: +SKIP
53
>>> print(extracted_docs[0]) # doctest: +SKIP
Node ID: 9b4fca22-7f1f-4876-bb71-d4b29500daa3
Text: What I Worked On February 2021 Before college the two main
things I worked on, outside of school, were writing and programming. I
didn't write essays. I wrote what beginning writers were supposed to
write then, and probably still are: short stories. My stories were
awful. They had hardly any plot, just characters with strong feelings,
whic...
>>> print(extracted_docs[0].extra_info) # doctest: +SKIP
{
'file_path': 'examples\\data\\essay\\paul-graham-essay.txt',
'file_name': 'paul-graham-essay.txt',
'file_type': 'text/plain',
'file_size': 75395,
'creation_date': '2024-10-25',
'last_modified_date': '2024-09-16',
'document_title': 'After reviewing the potential titles and themes mentioned in the context,
I would suggest the following comprehensive title \n\n"A Personal Odyssey ***,'.
'questions_this_excerpt_can_answer': "Based on the provided context, here's a question that this
context can specifically answer:\n\nWhat was Paul Graham's experience with the IBM ***",
'section_summary': 'Here is a summary of the key topics and entities in the section:\n\n**Key
Topics:**\n\n1. Paul Graham\'s early experiences with writing and programming.\n2. His work on ***',
'excerpt_keywords': 'Here are three unique keywords for this document:\n\nPaul Graham, IBM 1401,
Microcomputers'
}
Source code in src/llama_utils/retrieval/storage.py
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 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 |
|
create() -> Storage
classmethod
Create a new in-memory Storage.
Returns:
Name | Type | Description |
---|---|---|
Storage |
Storage
|
The storage Context. |
Examples:
You can create a new storage (in-memory) using the create
method as follows:
>>> store = Storage.create()
>>> print(store)
<BLANKLINE>
Documents: 0
Indexes: 0
<BLANKLINE>
Source code in src/llama_utils/retrieval/storage.py
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 |
|
delete_document(doc_id: str)
Delete a document from the docstore.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
doc_id
|
str
|
The ID of the document to delete. |
required |
Returns:
Type | Description |
---|---|
None
|
|
Examples:
You can delete a document from the document store and all the nodes that are related to it using the
delete_document
method by providing the document_id
:
>>> store = Storage.load("examples/paul-graham-essay-storage")
>>> document_metadata = store.metadata
>>> document_id = list(document_metadata().keys())[0]
>>> print(document_id) # doctest: +SKIP
a25111e2e59f81bb7a0e3efb48255f4a5d4f722aaf13ffd112463fb98c227092
>>> store.delete_document(document_id)
Now if you check the document_metadata, you will find that the document is deleted:
>>> print(store.metadata())
{}
Source code in src/llama_utils/retrieval/storage.py
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 |
|
delete_node(node_id: str)
Delete a node from the docstore.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node_id
|
str
|
The ID of the node to delete. |
required |
Returns:
Type | Description |
---|---|
None
|
|
Examples:
You can delete a node from the document store using the delete_node
method by providing the node_id
:
>>> store = Storage.load("examples/paul-graham-essay-storage")
>>> node_id = store.node_id_list()[0]
>>> print(node_id)
cadde590b82362fc7a5f8ce0751c5b30b11c0f81369df7d83a76956bf22765b7
>>> store.delete_node(node_id)
Source code in src/llama_utils/retrieval/storage.py
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 |
|
get_nodes_by_file_name(file_name: str, exact_match: bool = False) -> List[BaseNode]
Get nodes by file name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
The file name to search for. |
required |
exact_match
|
bool
|
True to search for an exact match, False to search for a partial match. |
False
|
Returns:
Type | Description |
---|---|
List[TextNode]
|
The nodes with the specified file name. |
Examples:
- First read the storage context from a directory:
>>> storage_dir = "examples/paul-graham-essay-storage" >>> store = Storage.load(storage_dir) >>> print(store) # doctest: +SKIP <BLANKLINE> Documents: 53 Indexes: 2 <BLANKLINE> - The storage context contains the following data: >>> print(store.metadata_index.head(3)) file_name doc_id 0 paul_graham_essay.txt cadde590b82362fc7a5f8ce0751c5b30b11c0f81369df7... 1 paul_graham_essay.txt 0567f3a9756983e1d040ec332255db94521ed5dc1b03fc... 2 paul_graham_essay.txt d5542515414f1bf30f6c21f0796af8bde4c513f2e72a2d... You can get all the nodes for file `paul_graham_essay.txt` as follows: >>> nodes = store.get_nodes_by_file_name("paul_graham_essay.txt") >>> nodes[0] # doctest: +SKIP TextNode( id_='cadde590b82362fc7a5f8ce0751c5b30b11c0f81369df7d83a76956bf22765b7', embedding=None, metadata={ 'file_path': 'examples\\data\\paul_graham_essay.txt', 'file_name': 'paul_graham_essay.txt', 'file_type': 'text/plain', 'file_size': 75395, 'creation_date': '2024-10-24', 'last_modified_date': '2024-09-16', 'document_title': 'Based on the candidate titles and content, I would suggest a comprehensive title that captures the essence of the text. Here\'s a potential title:\n\n"From Early Days ***' }, excluded_embed_metadata_keys=['file_name'], excluded_llm_metadata_keys=['file_name'], relationships={ <NodeRelationship.SOURCE: '1'>: RelatedNodeInfo( node_id='a25111e2e59f81bb7a0e3efb48255f4a5d4f722aaf13ffd112463fb98c227092', node_type=<ObjectType.DOCUMENT: '4'>, metadata={ 'file_path': 'examples\\data\\paul_graham_essay.txt', 'file_name': 'paul_graham_essay.txt', 'file_type': 'text/plain', 'file_size': 75395, 'creation_date': '2024-10-24', 'last_modified_date': '2024-09-16' }, hash='2a494d84cd0ab1e73396773258b809a47739482c90b80d5f61d374e754c3ef06' ), <NodeRelationship.NEXT: '3'>: RelatedNodeInfo(node_id='15478c7a-fdab-40c8-92e7-42973b9d3b28', node_type=<ObjectType.TEXT: '1'>, metadata={}, hash='424546c0aa78015988ced235522cdd238633d5edc1b92667cbdcda44d72613ec')}, text='What I Worked On\r\n\r\nFebruary 2021\r\n\r\nBefore college the two main things I worked on, outside of school, were writing and programming. I didn\'t write essays. I wrote what beginning writers were supposed to write then, and probably still are: short stories. My stories were awful. They had hardly any plot, just characters with strong feelings, which I imagined made them deep.\r\n\r\nThe first programs I tried writing were on the IBM 1401 that our school district used for what was then called "data processing." This was in 9th grade, so I was 13 or 14. The school district\'s 1401 happened to be in the basement of our junior high school, and my friend Rich Draves and I got permission to use it. It was like a mini Bond villain\'s lair down there, with all these alien-looking machines — CPU, disk drives, printer, card reader — sitting up on a raised floor under bright fluorescent lights.\r\n\r\nThe language we used was an early version of Fortran. You had to type programs on punch cards, then stack them in the card reader and press a button to load the program into memory and run it. The result would ordinarily be to print something on the spectacularly loud printer.\r\n\r\nI was puzzled by the 1401. I couldn\'t figure out what to do with it. And in retrospect there\'s not much I could have', mimetype='text/plain', start_char_idx=4, end_char_idx=2027, text_template='[Excerpt from document]\n{metadata_str}\nExcerpt:\n-----\n{content}\n-----\n', metadata_template='{key}: {value}', metadata_seperator='\n' )
Source code in src/llama_utils/retrieval/storage.py
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 678 679 680 |
|
load(store_dir: str) -> Storage
classmethod
Load the store from a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
store_dir
|
str
|
The directory containing the store. |
required |
Returns:
Name | Type | Description |
---|---|---|
Storage |
Storage
|
The loaded storage. |
Raises:
Type | Description |
---|---|
StorageNotFoundError
|
If the storage is not found at the specified directory. |
Examples:
You can load a storage from a directory as follows:
>>> store = Storage.load("examples/paul-graham-essay-storage")
>>> print(store) # doctest: +SKIP
<BLANKLINE>
Documents: 53
Indexes: 2
<BLANKLINE>
>>> metadata = store.metadata(as_dataframe=True)
>>> print(metadata.head()) # doctest: +SKIP
doc_id node_id file_name
0 a25111e2e59f81bb7a0e3efb4825... cadde590b82362fc7a5f8ce0751c5b30b... paul_graham_essay.txt
1 a25111e2e59f81bb7a0e3efb4825... 0567f3a9756983e1d040ec332255db945... paul_graham_essay.txt
2 a25111e2e59f81bb7a0e3efb4825... d5542515414f1bf30f6c21f0796af8bde... paul_graham_essay.txt
3 a25111e2e59f81bb7a0e3efb4825... 120b69658a6c69ab8de3167b5ed0db779... paul_graham_essay.txt
>>> docstore = store.docstore # doctest: +SKIP
<llama_index.core.storage.docstore.simple_docstore.SimpleDocumentStore at 0x20444d31be0>
>>> vector_store = store.vector_store
>>> print(type(vector_store))
<class 'llama_index.core.vector_stores.simple.SimpleVectorStore'>
Source code in src/llama_utils/retrieval/storage.py
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 |
|
metadata(as_dataframe: Optional[bool] = False) -> Union[Dict[str, RefDocInfo], DataFrame]
Document metadata.
Get the metadata of all the documents in the docstore.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
as_dataframe
|
Optional[bool]
|
True to return the metadata as a DataFrame. |
False
|
Returns:
Type | Description |
---|---|
Dict[str, RefDocInfo] or DataFrame
|
The metadata of all the documents in the docstore. |
Examples:
You can get the document metadata as a dictionary using the metadata
method with the default parameter values:
>>> store = Storage.load("examples/paul-graham-essay-storage")
>>> metadata = store.metadata()
metadata
is a dictionary with the document ID as the key and the document metadata as the value:
>>> documents_id = list(metadata.keys())
>>> print(documents_id) # doctest: +SKIP
['a25111e2e59f81bb7a0e3efb48255f4a5d4f722aaf13ffd112463fb98c227092']
>>> print(metadata) # doctest: +SKIP
{
'a25111e2e59f81bb7a0e3efb48255f4a5d4f722aaf13ffd112463fb98c227092':
RefDocInfo(
node_ids=[
'cadde590b82362fc7a5f8ce0751c5b30b11c0f81369df7d83a76956bf22765b7',
'0567f3a9756983e1d040ec332255db94521ed5dc1b03fc7312f653c0e670a0bf',
'd5542515414f1bf30f6c21f0796af8bde4c513f2e72a2df21f0810f10826252f',
'120b69658a6c69ab8de3167b5ed0db77941a2b487e94d5d0e64a0d2d2805a4b7'
],
metadata={
'file_path': 'examples\\data\\paul_graham_essay.txt',
'file_name': 'paul_graham_essay.txt',
'file_type': 'text/plain',
'file_size': 75395,
'creation_date': '2024-10-24',
'last_modified_date': '2024-09-16',
'document_title': 'Based on the candidate titles and content, I would suggest a***.'
}
)
}
as_dataframe
parameter to True:
>>> metadata = store.metadata(as_dataframe=True)
>>> print(metadata) # doctest: +SKIP
doc_id node_id
0 a25111e2e59f81bb7a0e3efb48255f4a5d4f722aaf13ff... cadde590b82362fc7a5f8ce0751c5b30b11c0f81369df7...
1 a25111e2e59f81bb7a0e3efb48255f4a5d4f722aaf13ff... 0567f3a9756983e1d040ec332255db94521ed5dc1b03fc...
2 a25111e2e59f81bb7a0e3efb48255f4a5d4f722aaf13ff... d5542515414f1bf30f6c21f0796af8bde4c513f2e72a2d...
Source code in src/llama_utils/retrieval/storage.py
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 |
|
node_id_list() -> List[str]
Get the metadata of the nodes in the docstore.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, Any]]
|
The metadata of the nodes in the docstore. |
Examples:
You can get the metadata of the nodes in the docstore using the nodes_metadata
method:
>>> store = Storage.load("examples/paul-graham-essay-storage")
>>> nodes_metadata = store.node_id_list()
>>> print(nodes_metadata) # doctest: +SKIP
[
'cadde590b82362fc7a5f8ce0751c5b30b11c0f81369df7d83a76956bf22765b7',
'0567f3a9756983e1d040ec332255db94521ed5dc1b03fc7312f653c0e670a0bf',
'd5542515414f1bf30f6c21f0796af8bde4c513f2e72a2df21f0810f10826252f',
...
]
Source code in src/llama_utils/retrieval/storage.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 |
|
read_documents(path: str, show_progres: bool = False, num_workers: int = None, recursive: bool = False, **kwargs) -> List[Union[Document, TextNode]]
staticmethod
Read documents from a directory.
the read_documents
method reads documents from a directory and returns a list of documents.
the doc_id
is sha256 hash number generated based on the document's text content.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
path to the directory containing the documents. |
required |
show_progres
|
bool
|
True to show progress bar. |
False
|
num_workers
|
int
|
The number of workers to use for loading the data. |
None
|
recursive
|
bool
|
True to read from subdirectories. |
False
|
Returns:
Type | Description |
---|---|
Sequence[Union[Document, TextNode]]
|
The documents/nodes read from the store. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the directory is not found. |
Examples:
You can read documents from a directory as follows:
>>> data_path = "examples/data/essay"
>>> docs = Storage.read_documents(data_path)
>>> print(docs) # doctest: +SKIP
[
Document(
id_='a25111e2e59f81bb7a0e3efb48255**',
embedding=None,
metadata={
'file_path': 'examples/data/essay/paul-graham-essay.txt',
'file_name': 'paul-graham-essay.txt',
'file_type': 'text/plain',
'file_size': 75395,
'creation_date': '2024-10-25',
'last_modified_date': '2024-09-16'
},
excluded_embed_metadata_keys=['file_name'],
excluded_llm_metadata_keys=['file_name'],
relationships={},
text='What I Worked On\n\nFebruary 2021\n\nBefore college the two main things ****',
mimetype='text/plain',
start_char_idx=None,
end_char_idx=None,
text_template='{metadata_str}\n\n{content}',
metadata_template='{key}: {value}',
metadata_seperator='\n'
)
]
Source code in src/llama_utils/retrieval/storage.py
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 |
|
save(store_dir: str)
Save the storage to a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
store_dir
|
str
|
The directory to save the store. |
required |
Examples:
You can save a storage to a directory as follows:
>>> store = Storage.create()
>>> store.save("examples/paul-graham-essay-storage-example")
Source code in src/llama_utils/retrieval/storage.py
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 |
|
create_metadata_index_existing_docs(docs: Dict[str, BaseNode])
Create a metadata index for existing documents.
Source code in src/llama_utils/retrieval/storage.py
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
|
read_metadata_index(path: str) -> pd.DataFrame
Read the ID mapping from a JSON file.
Source code in src/llama_utils/retrieval/storage.py
819 820 821 822 823 |
|
save_metadata_index(data: pd.DataFrame, path: str)
Save the ID mapping to a JSON file.
Source code in src/llama_utils/retrieval/storage.py
826 827 828 |
|