Open JohnTaco93 opened 5 hours ago
Hey there, @JohnTaco93! I'm here to help you out with any bugs, questions, or contributions you have. Let's tackle this together!
It looks like you're encountering a ValueError
because the KnowledgeGraphIndex
requires at least one of nodes
, objects
, or index_struct
to be provided during initialization. When loading a persisted index, you should use a method that reconstructs the index from the stored data, rather than initializing it directly with KnowledgeGraphIndex
.
Here's how you can load your knowledge graph index correctly:
StorageContext
is correctly set up to load from the persisted directory.load_index_from_storage
function to load the index from the storage context.Here's an example of how you can modify your code to load the index:
from llama_index import StorageContext, load_index_from_storage
index_dir = 'd:/bitbucket/ja-chatbot-resources/John/john_personal_development/KG/index_kg3/'
# Create a storage context from the persisted directory
storage_context = StorageContext.from_defaults(persist_dir=index_dir)
# Load the index from the storage context
index = load_index_from_storage(storage_context)
# Now you can query the loaded index
response = index.query("Your query here")
print(response)
This approach should resolve the ValueError
by properly loading the index from the persisted data [1][2].
To continue talking to Dosu, mention @dosu.
@dosu Thanks, it worked perfectly.
Question Validation
Question
I saved my KG the way below (maybe incorrectly) but can't load it.
Code to save KG
documents = SimpleDirectoryReader( "d:/..../KG/example_docs4" ).load_data()
llm = OpenAI(temperature=0, model="gpt-4o-mini") Settings.llm = llm Settings.chunk_size = 512
graph_store = SimpleGraphStore() storage_context = StorageContext.from_defaults(graph_store=graph_store)
index = KnowledgeGraphIndex.from_documents( documents, max_triplets_per_chunk=4, storage_context=storage_context, include_embeddings=True, )
index_dir = 'd:/bitbucket/ja-chatbot-resources/John/john_personal_development/KG/index_kg3/' if not os.path.exists(index_dir): os.makedirs(index_dir) index.storage_context.persist(persist_dir=index_dir)
OUTPUT
I tried to load it this way but I get an error:
from llama_index import StorageContext, KnowledgeGraphIndex
index_dir = 'd:/bitbucket/ja-chatbot-resources/John/john_personal_development/KG/index_kg3/'
storage_context = StorageContext.from_defaults(persist_dir=index_dir)
index = KnowledgeGraphIndex(storage_context=storage_context)
response = index.query("Your query here") print(response)
Error:
ValueError Traceback (most recent call last) Cell In[7], line 10 7 storage_context = StorageContext.from_defaults(persist_dir=index_dir) 9 # Initialize the Knowledge Graph Index with the loaded storage context ---> 10 index = KnowledgeGraphIndex(storage_context=storage_context) 12 # Now you can query the loaded index 13 response = index.query("Your query here")
File ~.conda\envs\llama_index\lib\site-packages\llama_index\core\indices\knowledge_graph\base.py:99, in KnowledgeGraphIndex.init(self, nodes, objects, index_struct, llm, embed_model, storage_context, kg_triplet_extract_template, max_triplets_per_chunk, include_embeddings, show_progress, max_object_length, kg_triplet_extract_fn, kwargs) 96 self._llm = llm or Settings.llm 97 self._embed_model = embed_model or Settings.embed_model ---> 99 super().init( 100 nodes=nodes, 101 index_struct=index_struct, 102 storage_context=storage_context, 103 show_progress=show_progress, 104 objects=objects, 105 kwargs, 106 ) 108 # TODO: legacy conversion - remove in next release 109 if ( 110 len(self.index_struct.table) > 0 111 and isinstance(self.graph_store, SimpleGraphStore) 112 and len(self.graph_store._data.graph_dict) == 0 113 ):
File ~.conda\envs\llama_index\lib\site-packages\llama_index\core\indices\base.py:48, in BaseIndex.init(self, nodes, objects, index_struct, storage_context, callback_manager, transformations, show_progress, **kwargs) 46 """Initialize with parameters.""" 47 if index_struct is None and nodes is None and objects is None: ---> 48 raise ValueError("One of nodes, objects, or index_struct must be provided.") 49 if index_struct is not None and nodes is not None: 50 raise ValueError("Only one of nodes or index_struct can be provided.")
ValueError: One of nodes, objects, or index_struct must be provided.
Please help