ChatGPT icon indicating copy to clipboard operation
ChatGPT copied to clipboard

[BUG] RuntimeWarning: coroutine 'AsyncChatbot.get_cf_cookies' was never awaited

Open FawenYo opened this issue 3 years ago • 17 comments

Describe the bug Got error message showing " RuntimeWarning: coroutine 'AsyncChatbot.get_cf_cookies' was never awaited"

Steps to reproduce

I call the function from my code response = await get_ai_response(question=user_message)

Here is my function...

async def get_ai_response(question: str):
    try:
        chatbot = Chatbot(gpt_config, captcha_solver=CaptchaSolver())
        message = chatbot.get_chat_response(prompt=question)["message"]
        return await message
    except Exception as exc:
        return "Something went wrong!"

And it will show the error RuntimeWarning: coroutine 'AsyncChatbot.get_cf_cookies' was never awaited Not so sure where the problem could be

FawenYo avatar Dec 13 '22 13:12 FawenYo

Might be outdated. Please use https://github.com/acheong08/ChatGPT/releases/tag/0.0.43

acheong08 avatar Dec 13 '22 14:12 acheong08

Checked with the package version, it is 0.0.43 but it still pop out the error message

FawenYo avatar Dec 13 '22 15:12 FawenYo

Hmm. It might be because you're using Chatbot inside an async function. Try using AsyncChatbot or defining Chatbot outside the function

acheong08 avatar Dec 13 '22 15:12 acheong08

OK, so it seems to be that I need to initialize Chatbot outside of async function. It works normally now, but I sometimes get the error message "RuntimeError: asyncio.run() cannot be called from a running event loop" Could this be fixed?

FawenYo avatar Dec 13 '22 15:12 FawenYo

OK, so it seems to be that I need to initialize Chatbot outside of async function. It works normally now, but I sometimes get the error message "RuntimeError: asyncio.run() cannot be called from a running event loop" Could this be fixed?

I'm not good with async. Need input from @Harry-Jing

acheong08 avatar Dec 13 '22 15:12 acheong08

It probably can be. Need to find some ways to make everything async

acheong08 avatar Dec 13 '22 15:12 acheong08

getting this issue as well

mgpai22 avatar Dec 13 '22 16:12 mgpai22

the issue happen because of cant nested use of asyncio.run

Harry-Jing avatar Dec 13 '22 16:12 Harry-Jing

I will try to fix that

Harry-Jing avatar Dec 13 '22 16:12 Harry-Jing

I just pushed a fix

acheong08 avatar Dec 13 '22 16:12 acheong08

@Harry-Jing The solution I used wasn't optimal. You can improve it

acheong08 avatar Dec 13 '22 16:12 acheong08

@mgpai22 @FawenYo Try https://github.com/acheong08/ChatGPT/releases/tag/0.0.43.1

acheong08 avatar Dec 13 '22 17:12 acheong08

RuntimeWarning: coroutine 'AsyncChatbot.get_cf_cookies' was never awaited
  asyncio.run(self.get_cf_cookies())
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Kinda worked, seems to be buggy

mgpai22 avatar Dec 13 '22 17:12 mgpai22

RuntimeWarning: coroutine 'AsyncChatbot.get_cf_cookies' was never awaited
  asyncio.run(self.get_cf_cookies())
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Kinda worked, seems to be buggy

It is buggy now. Harry-jing might push a fix later

acheong08 avatar Dec 13 '22 17:12 acheong08

There are many async bugs with the new releases

acheong08 avatar Dec 13 '22 17:12 acheong08

Yes, I'm also getting an async-related error when trying to run the Chatbot with FastAPI

INFO:     Will watch for changes in these directories: ['/home/alber/Desktop/ChatGPTApp']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [64988] using WatchFiles
INFO:     Started server process [64990]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:46022 - "GET / HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/revChatGPT/revChatGPT.py", line 286, in refresh_session
    asyncio.run(self.get_cf_cookies())
  File "/usr/lib/python3.10/asyncio/runners.py", line 33, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 419, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/fastapi/applications.py", line 270, in __call__
    await super().__call__(scope, receive, send)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/starlette/routing.py", line 706, in __call__
    await route.handle(scope, receive, send)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/fastapi/routing.py", line 235, in app
    raw_response = await run_endpoint_function(
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/fastapi/routing.py", line 161, in run_endpoint_function
    return await dependant.call(**values)
  File "/home/alber/Desktop/ChatGPTApp/./main.py", line 25, in send_message
    chatbot = create_chatbot()
  File "/home/alber/Desktop/ChatGPTApp/./main.py", line 12, in create_chatbot
    chatbot = Chatbot(config)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/revChatGPT/revChatGPT.py", line 112, in __init__
    self.refresh_session()
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/revChatGPT/revChatGPT.py", line 288, in refresh_session
    nest_asyncio.apply()
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/nest_asyncio.py", line 19, in apply
    _patch_loop(loop)
  File "/home/alber/Desktop/ChatGPTApp/chatgpt_venv/lib/python3.10/site-packages/nest_asyncio.py", line 175, in _patch_loop
    raise ValueError('Can\'t patch loop of type %s' % type(loop))
ValueError: Can't patch loop of type <class 'uvloop.Loop'>

alberduris avatar Dec 13 '22 17:12 alberduris

don't use asyncio.run() in async function , u can use "get_chat_response" in AsyncChatbot , eg:

async def get_chat(args):
    return await chatbot.get_chat_response(args,output="text")

async def  main():
	ur_text = do something()
    message = await get_chat(ur_text)
	strip_text = message["message"]

the "strip_text" is type"text" of ChatGTP's response

nek0us avatar Dec 13 '22 17:12 nek0us