run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
36.61k stars 5.24k forks source link

[Question]: RuntimeError: asyncio.run() cannot be called from a running event loop while creating Propertygraphindex #16126

Closed navaneethstabilix closed 1 month ago

navaneethstabilix commented 1 month ago

Question Validation

Question

from llama_index.core import PropertyGraphIndex from llama_index.embeddings.huggingface import HuggingFaceEmbedding

index = PropertyGraphIndex.from_documents( documents, kg_extractors=[kg_extractor], embed_model=HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"), property_graph_store=graph_store, vector_store=vec_store, show_progress=True, )

Received notification from DBMS server: {severity: WARNING} {code: Neo.ClientNotification.Statement.FeatureDeprecationWarning} {category: DEPRECATION} {title: This feature is deprecated and will be removed in future versions.} {description: The procedure has a deprecated field. ('config' used by 'apoc.meta.graphSample' is deprecated.)} {position: line: 1, column: 1, offset: 0} for query: "CALL apoc.meta.graphSample() YIELD nodes, relationships RETURN nodes, [rel in relationships | {name:apoc.any.property(rel, 'type'), count: apoc.any.property(rel, 'count')}] AS relationships" Parsing nodes: 100%  1/1 [00:00<00:00, 28.43it/s]

RuntimeError Traceback (most recent call last) Cell In[11], line 14 11 from llama_index.core import PropertyGraphIndex 12 from llama_index.embeddings.huggingface import HuggingFaceEmbedding ---> 14 index = PropertyGraphIndex.from_documents( 15 documents, 16 kg_extractors=[kg_extractor], 17 embed_model=HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"), 18 property_graph_store=graph_store, 19 vector_store=vec_store, 20 show_progress=True, 21 )

File ~/anaconda3/envs/project1/lib/python3.9/site-packages/llama_index/core/indices/base.py:119, in BaseIndex.from_documents(cls, documents, storage_context, show_progress, callback_manager, transformations, kwargs) 110 docstore.set_document_hash(doc.get_doc_id(), doc.hash) 112 nodes = run_transformations( 113 documents, # type: ignore 114 transformations, 115 show_progress=show_progress, 116 kwargs, 117 ) --> 119 return cls( 120 nodes=nodes, 121 storage_context=storage_context, 122 callback_manager=callback_manager, 123 show_progress=show_progress, 124 transformations=transformations, 125 **kwargs, 126 )

File ~/anaconda3/envs/project1/lib/python3.9/site-packages/llama_index/core/indices/property_graph/base.py:134, in PropertyGraphIndex.init(self, nodes, llm, kg_extractors, property_graph_store, vector_store, use_async, embed_model, embed_kg_nodes, callback_manager, transformations, storage_context, show_progress, kwargs) 128 self._embed_kg_nodes = embed_kg_nodes 129 self._override_vector_store = ( 130 vector_store is not None 131 or not storage_context.property_graph_store.supports_vector_queries 132 ) --> 134 super().init( 135 nodes=nodes, 136 callback_manager=callback_manager, 137 storage_context=storage_context, 138 transformations=transformations, 139 show_progress=show_progress, 140 kwargs, 141 )

File ~/anaconda3/envs/project1/lib/python3.9/site-packages/llama_index/core/indices/base.py:77, in BaseIndex.init(self, nodes, objects, index_struct, storage_context, callback_manager, transformations, show_progress, kwargs) 75 if index_struct is None: 76 nodes = nodes or [] ---> 77 index_struct = self.build_index_from_nodes( 78 nodes + objects, # type: ignore 79 kwargs, # type: ignore 80 ) 81 self._index_struct = index_struct 82 self._storage_context.index_store.add_index_struct(self._index_struct)

File ~/anaconda3/envs/project1/lib/python3.9/site-packages/llama_index/core/indices/base.py:185, in BaseIndex.build_index_from_nodes(self, nodes, build_kwargs) 183 """Build the index from nodes.""" 184 self._docstore.add_documents(nodes, allow_update=True) --> 185 return self._build_index_from_nodes(nodes, build_kwargs)

File ~/anaconda3/envs/project1/lib/python3.9/site-packages/llama_index/core/indices/property_graph/base.py:334, in PropertyGraphIndex._build_index_from_nodes(self, nodes, build_kwargs) 330 def _build_index_from_nodes( 331 self, nodes: Optional[Sequence[BaseNode]], build_kwargs: Any 332 ) -> IndexLPG: 333 """Build index from nodes.""" --> 334 nodes = self._insert_nodes(nodes or []) 336 # this isn't really used or needed 337 return IndexLPG()

File ~/anaconda3/envs/project1/lib/python3.9/site-packages/llama_index/core/indices/property_graph/base.py:200, in PropertyGraphIndex._insert_nodes(self, nodes) 198 # run transformations on nodes to extract triplets 199 if self._use_async: --> 200 nodes = asyncio.run( 201 arun_transformations( 202 nodes, self._kg_extractors, show_progress=self._show_progress 203 ) 204 ) 205 else: 206 nodes = run_transformations( 207 nodes, self._kg_extractors, show_progress=self._show_progress 208 )

File ~/anaconda3/envs/project1/lib/python3.9/asyncio/runners.py:33, in run(main, debug) 9 """Execute the coroutine and return the result. 10 11 This function runs the passed coroutine, taking care of (...) 30 asyncio.run(main()) 31 """ 32 if events._get_running_loop() is not None: ---> 33 raise RuntimeError( 34 "asyncio.run() cannot be called from a running event loop") 36 if not coroutines.iscoroutine(main): 37 raise ValueError("a coroutine was expected, got {!r}".format(main))

RuntimeError: asyncio.run() cannot be called from a running event loop

why is it casuing this error ?

dosubot[bot] commented 1 month ago

I found a similar closed issue that might help you resolve the RuntimeError: asyncio.run() cannot be called from a running event loop error. The suggested solution is to use nest_asyncio to allow nested use of asyncio.run():

import nest_asyncio
nest_asyncio.apply()

You can apply this patch before running your code to avoid the error [1].

Here's how you can modify your code:

import nest_asyncio
nest_asyncio.apply()

from llama_index.core import PropertyGraphIndex
from llama_index.embeddings.huggingface import HuggingFaceEmbedding

index = PropertyGraphIndex.from_documents(
    documents,
    kg_extractors=[kg_extractor],
    embed_model=HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"),
    property_graph_store=graph_store,
    vector_store=vec_store,
    show_progress=True,
)

Alternatively, you can use the asyncio_run utility provided by the llama_index library, which handles the event loop situation:

from llama_index.core.async_utils import asyncio_run

async def main():
    index = PropertyGraphIndex.from_documents(
        documents,
        kg_extractors=[kg_extractor],
        embed_model=HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"),
        property_graph_store=graph_store,
        vector_store=vec_store,
        show_progress=True,
    )

asyncio_run(main())

This should resolve the RuntimeError when creating a PropertyGraphIndex in an environment with an active event loop, such as a Jupyter Notebook [2].

To continue talking to Dosu, mention @dosu.