Find answers from the community

Updated 3 months ago

how can I load multiple indices as a RecursiveRetriever? I have already loaded the indices

how can I load multiple indices as a RecursiveRetriever? I have already loaded the indices where each index is of type VectorStoreIndex:

index_objects = self.initialize_multiple_indexes(indices=indices)
k
r
3 comments
Please stand by @Jam Mirzakhalov as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
To load multiple indices as a RecursiveRetriever, you would need to create a dictionary where the keys are the names of your indices and the values are the corresponding index objects. However, the information extracts provided do not contain the specific code for loading multiple indices as a RecursiveRetriever.

Here is an example of how you might load multiple indices, based on the information available:

Plain Text
from llama_index import load_index_from_storage, StorageContext, ServiceContext

index_set = {}
service_context = ServiceContext.from_defaults()

for index_name in indices:
    storage_context = StorageContext.from_defaults(persist_dir=f"./storage/{index_name}")
    cur_index = load_index_from_storage(storage_context, service_context=service_context)
    index_set[index_name] = cur_index


In this example, indices is a list of the names of your indices. The code loads each index from storage and adds it to the index_set dictionary.

Please refer to the LlamaIndex documentation () for more details on how to load indices from disk.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@Jam Mirzakhalov To load multiple indices as a RecursiveRetriever, you would first need to convert each index into a retriever. You can do this using the as_retriever method of the BaseIndex class. Once you have a dictionary of retrievers, you can pass it to the RecursiveRetriever constructor.

Here's an example of how you might do this:

Plain Text
# Assuming index_objects is a dictionary where the keys are index names and the values are VectorStoreIndex instances
retriever_dict = {index_name: index.as_retriever() for index_name, index in index_objects.items()}

# Create a RecursiveRetriever with the retrievers
recursive_retriever = RecursiveRetriever(root_id='root', retriever_dict=retriever_dict)


In this example, root_id is set to 'root', but you should replace this with the appropriate root id for your use case.

Please note that this is a general example and the exact code may vary depending on the specifics of your implementation and the classes you're using.
Add a reply
Sign up and join the conversation on Discord