Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Persisting

00:00 So persisting means nothing else but saving the index to a storage device. Now, why would you want to do that? Well, if you save it to a storage device, you avoid having to re-index your documents.

00:12 So that improves the performance because indexing can take up quite a bit of time, and it can reduce the performance depending on how many documents need to be indexed.

00:21 Also, you don’t have to recreate the embeddings because they are saved or stored as well. And that saves you costs because creating the embeddings is what you use the AI model for, and for some models like OpenAI, at the time of recording, you need to pay for that.

00:39 And it also saves time, of course, because you don’t have to recreate the embeddings, which again can take a bit of time depending on how many documents’ embeddings need to be recalculated for.

00:51 And then finally, because you are reusing the same embeddings across multiple runs, that of course enforces consistency.

01:01 Persisting the index requires quite a bit of coding, so I suggest splitting this up into two lessons. So in the first lesson, you’ll take care of the imports and the setup.

01:11 In the next lesson, you’ll get to focus fully on updating the get_index() function. Okay, so to persist your index, you’ll be needing a storage directory.

01:20 So first of all, it’s best to make working with directories independent of whether you’re on Windows or not. So this is where the Path objects come in handy.

01:31 So on line one, so if you create some space there, from pathlib import Path.

01:37 And Path is with a capital P. And then you go to line four, I suggest again make some space, and then create your Path objects. Now there will be three objects.

01:48 There’s a base directory, then there is the persistence directory on line six, and then you might as well create a Path object of your data file, your company policy there, so that’s line five.

02:01 So that’s your Path objects ready. The next step then is to import two more objects from llama_index.core, so on line two. Now this is going to take up some space and the line will get too long, so I’m going to use parentheses to split this up over several lines.

02:19 So after VectorStoreIndex, type a comma, then on the next line, import StorageContext.

02:26 So this is camel case, and then load_index_from_storage.

02:35 And then on line seven, don’t forget to close the parentheses. So with everything imported now and with your Path objects ready to go, you are ready to start updating the get_index() function in the next lesson.

Become a Member to join the conversation.