SciPhi-AI / R2R

Containerized, state of the art Retrieval-Augmented Generation (RAG) system with a RESTful API
https://r2r-docs.sciphi.ai/
MIT License
3.69k stars 277 forks source link

"There is no current event loop in thread" error #1341

Open dwipper opened 1 month ago

dwipper commented 1 month ago

Describe the bug I am working on integrating r2r into my existing Python/Gradio-based app. When I import the R2RClient, I receive the following errors:

Exception in thread Thread-4 (run):
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/davidwipper/Documents/NILIAdvisor/lib/python3.11/site-packages/uvicorn/server.py", line 62, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/davidwipper/Documents/NILIAdvisor/lib/python3.11/site-packages/nest_asyncio.py", line 26, in run
    loop = asyncio.get_event_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/davidwipper/Documents/NILIAdvisor/lib/python3.11/site-packages/nest_asyncio.py", line 40, in _get_event_loop
    loop = events.get_event_loop_policy().get_event_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/events.py", line 677, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-4 (run)'.
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py:1040: RuntimeWarning: coroutine 'Server.serve' was never awaited
  self._invoke_excepthook(self)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

To Reproduce Run the following code. You will need to install Gradio (pip install gradio==4.44.1)

import gradio as gr
from r2r import R2RClient

with gr.Blocks() as demo:
    with gr.Tabs() as Tabs:
        with gr.Tab(label='Images', id=0, visible=True) as images_tab:
            gr.Markdown("This is a very simple Gradio App")

if __name__ == "__main__":
    demo.launch()

Expected behavior The Gradio app should run. If you comment out "from r2r import R2RClient" it will run.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Smartphone (please complete the following information): NA Additional context NA

emrgnt-cmplxty commented 1 month ago

Thanks for sharing - this sounds like a bad interaction with some of our async logic. I will attempt to replicate and fix on my end.

emrgnt-cmplxty commented 1 month ago

What version of python are you running with?

I was able to execute the code above successfully -

poetry run python gradio_tester.py 
Python-dotenv could not parse statement starting at line 14
Python-dotenv could not parse statement starting at line 14
Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.

My python version:

ocolegrove@Owens-MacBook-Pro-2 py % poetry run python --version
Python 3.10.13

I am also running on iOS, 14.5 (23F79) w/ M1 proc.

dwipper commented 1 month ago

I'm on:

dwipper commented 1 month ago

@emrgnt-cmplxty Any more thoughts on this? This is a total blocker for our app to use R2R.....:-(

NolanTrem commented 1 month ago

Hey @dwipper just a heads up that we'll be merging a refactored version of the Python SDK, which includes an asynchronous client and a synchronous, thread safe client. Would love for you to test it out! It should be merged shortly! #1430

dwipper commented 1 month ago

Hey @dwipper just a heads up that we'll be merging a refactored version of the Python SDK, which includes an asynchronous client and a synchronous, thread safe client. Would love for you to test it out! It should be merged shortly! #1430

That would be great. Hopefully it plays well with Gradio, although it appears that the above is an issue with my specific setup, but not sure what or how to track it down....

dwipper commented 1 month ago

@NolanTrem I just reinstalled my Python environment. I'm not having the above issue anymore, but when I try to run the above Gradio app, I get a 'ImportError: No module named 'litellm', likely due to core dependencies not being installed.' The version is 3.2.16. This error message seems to be just a warning, since the code will still run.....