acheong08 / Bard

Python SDK/API for reverse engineered Google Bard
MIT License
1.42k stars 177 forks source link

coroutine 'AsyncChatbot.create' was never awaited #47

Closed PhoenixPhyre closed 1 year ago

PhoenixPhyre commented 1 year ago

This happens immediately after chatbot.ask returns its answer. The answer is proper, the "ask" worked, but then the entire thing crashes.

I have updated Python and all associated libs this morning. I have made sure there are no other active logins on the machine.

Traceback (most recent call last): File "C:\Users\phoen\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 116, in del File "C:\Users\phoen\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close File "C:\Users\phoen\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 719, in call_soon File "C:\Users\phoen\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 508, in _check_closed RuntimeError: Event loop is closed sys:1: RuntimeWarning: coroutine 'AsyncChatbot.create' was never awaited

Thanks!

PhoenixPhyre commented 1 year ago

The source seems to be line 64 of Chatbot class:

self.async_chatbot = self.loop.run_until_complete( AsyncChatbot.create(session_id, proxy, timeout) )

PhoenixPhyre commented 1 year ago

AH -- I am putting this into a uvicorn fastapi server; something about uvicorn seems to be trying to launch a new instance before the old is cleaned up? If I comment out the call to uvicorn, and just run the script top to bottom, with a single ask for a joke, I get the joke, and then the process closes out fine, no errors. But it was running fine as a uvicorn server before I installed Bard, so I am sure the server part is solid. Something about the two working together is not great... still digging

PhoenixPhyre commented 1 year ago

Okay, this is all prototype for me, so I know my solution is a hack, but just info for you... I wrapped the creation of my chatbot in a boolean, and set it false on the first creation... seems to do the trick. As I say, feels hacky to me, but if you have any other ideas, happy to hear them. If that sounds like the proper fix, then feel free to close this out.

Thanks.

PhoenixPhyre commented 1 year ago

Ick... I was wrong. That keeps the program from crashing, but on my second call to my server, I get "Chatbot is not defined" error. So, it still feels like some strange timing error... the chatbot gets created, I use it... then, either I get an error when uvicorn runs through the main loop again because the chatbot is already running; or, if I skip instantiation on all further calls, I get an error that the bot does not exist. So, somewhere in the middle is the place where it gets created, used, and properly destroyed before my code reruns. Looking for that now.

PhoenixPhyre commented 1 year ago

I think the real problem is, I am trying to put this into a uvicorn server; but I have discovered, the synchronous chatbot is just a wrapper around the async chatbot; so at some level, I have two event loops competing. I will continue to poke around, but if there is a clean way to use the synchronous chatbot that is truly asynchronous, please let me know. At the end of the day, when uvicorn hits the bottom of my code and cycles back, I need that chatbot to get properly destroyed without any potential timing errors from wanting to wait on it's on potential outstanding work, if that makes sense.

acheong08 commented 1 year ago

https://stackoverflow.com/questions/46827007/runtimeerror-this-event-loop-is-already-running-in-python

Can nest_asyncio fix the issue of nested event loops?

acheong08 commented 1 year ago

But if uvicorn is async, you should be able to use the async class directly rather than relying on the sync wrapper

acheong08 commented 1 year ago

You can revert back to https://github.com/acheong08/Bard/releases/tag/1.2.2 which doesn't have async

PhoenixPhyre commented 1 year ago

https://stackoverflow.com/questions/46827007/runtimeerror-this-event-loop-is-already-running-in-python

Can nest_asyncio fix the issue of nested event loops?

This worked perfectly, thanks for the suggestion