Find answers from the community

Updated 3 months ago

I want to load the sentence + metadata

I want to load the sentence + metadata index from a persisted directory (where I have saved the relevant docstore, indexstore and vectorstore json files). Does anyone have a code snippet how we might do so? Because when I call :

storage_context_s = StorageContext.from_defaults(
docstore=SimpleDocumentStore.from_persist_dir(persist_dir=r"persist_dir"),
vector_store=SimpleVectorStore.from_persist_dir(persist_dir=r"persist_dir"),
index_store=SimpleIndexStore.from_persist_dir(persist_dir=r"persist_dir"),
)

load_indices_from_storage

load_index_from_storage

sentence_index = load_indices_from_storage(storage_context_s)

query_engine = sentence_index.as_query_engine(
similarity_top_k=2,
# the target key defaults to window to match the node_parser's default
node_postprocessors=[
MetadataReplacementPostProcessor(target_metadata_key="window")
],
)
window_response = query_engine.query(
"Who is xxx?"
)
print(window_response)

I later get:

AttributeError: 'list' object has no attribute 'as_query_engine'

Help would be much obliged
L
3 comments
you can simplify this code quite a bit

Plain Text
from llama_index import StorageContext, load_index_from_storage
storage_context = StorageContext.from_defaults(persist_dir="persist_dir")
index = load_index_from_storage(storage_context)
note that you are using load_indices_from_storage, which returns a list
if you use load_index_from_storage it returns a single index (assuming that the storage context only contains one index)
Add a reply
Sign up and join the conversation on Discord