from llama_index.core import SummaryIndex, Document index = SummaryIndex([]) text_chunks = ["cars are red", "cars are big", "elephants are red"] doc_chunks = [] for i, text in enumerate(text_chunks): doc = Document(text=text, id_=f"doc_id_{i}") doc_chunks.append(doc) kg_index = KnowledgeGraphIndex.from_documents(documents=doc_chunks, storage_context=storage_context, max_triplets_per_chunk=3, space_name=space_name, edge_types=edge_types, rel_prop_names=rel_prop_names, tags=tags, llm=llm, embed_model=embed_model, include_embeddings=True) import networkx as nx G = kg_index.get_networkx_graph() nx.draw(G, with_labels=True, font_weight='bold')
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[70], line 2 1 hybrid_query_engine = kg_index.as_query_engine(llm=llm, embed_model=embed_model) ----> 2 response = hybrid_query_engine.query("Tell me about Europe.") 3 print(response) File ~/anaconda3/envs/llamaindex10/lib/python3.11/site-packages/llama_index/core/base/base_query_engine.py:40, in BaseQueryEngine.query(self, str_or_query_bundle) 38 if isinstance(str_or_query_bundle, str): 39 str_or_query_bundle = QueryBundle(str_or_query_bundle) ---> 40 return self._query(str_or_query_bundle) File ~/anaconda3/envs/llamaindex10/lib/python3.11/site-packages/llama_index/core/query_engine/retriever_query_engine.py:186, in RetrieverQueryEngine._query(self, query_bundle) 182 """Answer a query.""" 183 with self.callback_manager.event( 184 CBEventType.QUERY, payload={EventPayload.QUERY_STR: query_bundle.query_str} 185 ) as query_event: --> 186 nodes = self.retrieve(query_bundle) 187 response = self._response_synthesizer.synthesize( 188 query=query_bundle, 189 nodes=nodes, 190 ) 192 query_event.on_end(payload={EventPayload.RESPONSE: response}) File ~/anaconda3/envs/llamaindex10/lib/python3.11/site-packages/llama_index/core/query_engine/retriever_query_engine.py:142, in RetrieverQueryEngine.retrieve(self, query_bundle) ... --> 177 keyword = rel_text.split(",")[2] 178 if keyword: 179 keywords.append(keyword.strip(" ()\"'")) IndexError: list index out of range
documents = SimpleDirectoryReader("./data").load_data() graph_store = Neo4jGraphStore(username='neo4j', password='1234', url="neo4j://localhost:8088", database="neo4j") storage_context = StorageContext.from_defaults(graph_store=graph_store) index = KnowledgeGraphIndex.from_documents(documents, storage_context=storage_context, max_triplets_per_chunk=2, include_embeddings=True ) query_engine = index.as_query_engine( include_text=True, response_mode="tree_summarize", embedding_mode="hybrid", similarity_top_k=5, ) response = query_engine.query("Tell me more about Europe") print(response)
IndexError Traceback (most recent call last) Cell In[9], line 15 3 index = KnowledgeGraphIndex.from_documents(documents, 4 storage_context=storage_context, max_triplets_per_chunk=2, 5 include_embeddings=True 6 ) 8 query_engine = index.as_query_engine( 9 include_text=True, 10 response_mode="tree_summarize", 11 embedding_mode="hybrid", 12 similarity_top_k=5, 13 ) ---> 15 response = query_engine.query("Tell me more about Europe") 17 print(response) File ~/anaconda3/envs/llamaindex10/lib/python3.11/site-packages/llama_index/core/base/base_query_engine.py:40, in BaseQueryEngine.query(self, str_or_query_bundle) 38 if isinstance(str_or_query_bundle, str): 39 str_or_query_bundle = QueryBundle(str_or_query_bundle) ---> 40 return self._query(str_or_query_bundle) File ~/anaconda3/envs/llamaindex10/lib/python3.11/site-packages/llama_index/core/query_engine/retriever_query_engine.py:186, in RetrieverQueryEngine._query(self, query_bundle) 182 """Answer a query.""" 183 with self.callback_manager.event( 184 CBEventType.QUERY, payload={EventPayload.QUERY_STR: query_bundle.query_str} ... --> 177 keyword = rel_text.split(",")[2] 178 if keyword: 179 keywords.append(keyword.strip(" ()\"'")) IndexError: list index out of range
Class KGTableRetriever(BaseRetriever): [...] def _extract_rel_text_keywords(self, rel_texts: List[str]) -> List[str]: """Find the keywords for given rel text triplets.""" keywords = [] for rel_text in rel_texts: keyword = rel_text.split(",")[0] if keyword: keywords.append(keyword.strip("(\"'")) # Return the Object as well keyword = rel_text.split(",")[1] # was 2 <----------------------------- For some reason this index was set to 2. if keyword: keywords.append(keyword.strip(" ()\"'")) return keywords
StorageContext
requires three arguments: docstore
, index_store
, and vector_stores
, in addition to graph_store
. In the examples provided in the knowledge sources, the StorageContext
is created using the from_defaults
method, which sets these three arguments to their default values. StorageContext
with a Neo4jGraphStore
:from llama_index.core import StorageContext from llama_index.graph_stores.neo4j import Neo4jGraphStore graph_store = Neo4jGraphStore( username="neo4j", password="1234", url="bolt://localhost:1234", database="neo4j", node_label="XXX" ) storage_context = StorageContext.from_defaults(graph_store=graph_store)
graph_store
, you would typically use a KnowledgeGraphQueryEngine
or a KnowledgeGraphRAGQueryEngine
. However, based on the knowledge sources provided, there isn't a direct example of querying the graph_store
using the KnowledgeGraphRAGRetriever
. KnowledgeGraphIndex
using a KnowledgeGraphQueryEngine
:from llama_index.core import KnowledgeGraphIndex # Assuming you have documents and a storage_context index = KnowledgeGraphIndex.from_documents(documents, storage_context=storage_context) # Create a query engine query_engine = index.as_query_engine(include_text=True, response_mode="tree_summarize") # Perform a query response = query_engine.query("Your query here")
graph_store
without first creating a KnowledgeGraphIndex
from documents. The examples provided involve creating a KnowledgeGraphIndex
from documents, and then using a KnowledgeGraphQueryEngine
to query the index.query_graph_db
method of the Neo4jQueryToolSpec
to directly query the Neo4j database. Here's an example:from llama_index.tools.neo4j import Neo4jQueryToolSpec # Assuming you have a Neo4jGraphStore instance named graph_store query_tool = Neo4jQueryToolSpec(graph_store) # Perform a query query = "Your Cypher query here" params = {} # Optional parameters for your query results = query_tool.query_graph_db(query, params)