amosjyng / langchain-visualizer

Visualization and debugging tool for LangChain workflows
MIT License
723 stars 52 forks source link

RuntimeError: There is no current event loop in thread 'Thread-7 (process_request_thread)'. #61

Closed LittleColin closed 1 year ago

LittleColin commented 1 year ago

I use the visualiser for specific cases to debug flows but for usage of my API, I don't want the visualisation. However, simply importing the langchain_visualizer causes an error during agent.run (presumably because it's not wrapped in visualize).

  File "/home/me/emu/org/my-api/.venv/lib/python3.10/site-packages/langchain/chains/base.py", line 290, in run
    return self(args[0], callbacks=callbacks, tags=tags)[_output_key]
  File "/home/me/emu/org/my-api/.venv/lib/python3.10/site-packages/langchain_visualizer/hijacking.py", line 74, in overridden_call
    return asyncio.get_event_loop().run_until_complete(
  File "/home/me/emu/org/my-api/.venv/lib/python3.10/site-packages/nest_asyncio.py", line 45, in _get_event_loop
    loop = events.get_event_loop_policy().get_event_loop()
  File "/usr/lib/python3.10/asyncio/events.py", line 656, 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-7 (process_request_thread)'.
amosjyng commented 1 year ago

Interesting, I am unable to reproduce this. Could you please tell me what version of langchain and OS you're running this on, and whether you're doing it from Jupyter or some other similar notebook?

Thanks!

LittleColin commented 1 year ago

Sure, so when I run this from the command line it is all fine but when run as part of a flask API, it gives the error described.

I am using Ubuntu, langchain 0.0.216 and flask 2.2.2.

But I've actually realised I don't need the visualizer imported in this file at all. Instead, I now have a wrapper at a higher level that imports it specifically for command line use.

Previously I was doing this in a file that was being executed as part of the flask app and that was causing the problem:

import langchain_visualizer

# ... functions for my web app

if __name__ == "__main__":
    langchain_visualizer.visualize(chat(sys.argv[1]))
amosjyng commented 1 year ago

Ahh interesting, thanks for letting me know! This doesn't yet support Flask out of the box, although I think others have gotten it to work :P