Persisting Functionality
00:00
With the storage context imported and your Path object set up, you can now tackle the get_index() function, and this function will now take two input parameters.
00:11
First one is persist_dir, and I’m going to set that equal to the PERSIST_DIR path object defined in line 11, and the second input parameter is data_file, and that is set equal to, well, you guessed it, DATA_FILE, the path object.
00:32
Then I’m going to use that data_file, which is a path object now, in line 27 to replace the string reference here. I’m going to apply str() to my path object data_file.
00:47 The logic now is that if a storage directory exists, then read from the storage directory. Now, if it doesn’t exist, then calculate the index and create the storage directory.
01:00
Now, given that you are using path objects, path objects have a method called exists(), which makes the following very straightforward, if persist_dir.exists(), with parentheses, then you’re going to have to do something, I’ll type pass for now, because I’d like to do the else leg first, so else, and then select rows 30 to 32 and hit tab. As before, you create the reader, the documents, then you have the index, so the index is calculated, you now need to store it.
01:34
So how do you store the index? Well, the index has an attribute called storage_context,
01:43
and that has a method called persist(), and then you need to feed it the persist_dir and set that equal to str() of your path object.
01:57
And then just for clarity, I add this print() functionality here that says the index has been created and persisted to storage, so you can see that when you run your model. Okay, that leaves the if leg to finish.
02:11
So the first thing you have to do is to create a storage context from which you can then read the index. So line 28, where it says pass, get rid of pass and type storage_context,
02:26
and that is equal to StorageContext in camel case, so that’s the object you imported, .from_defaults, open parentheses and then persist_dir, and that again is a str() of your path object, persist_dir.
02:47
On the next line, then you create your index by loading it from this storage context, so load_index_from_storage, and then open parentheses and then you feed it the storage_context that you have created on line 28.
03:08
And then finally, just for clarity again, I add some printing functionality here that says "Index loaded from storage".
03:17 You have now incorporated a storage context to either persist or save the index to or read an index from. So the next step in the next lesson is to check the output.
Become a Member to join the conversation.
