xtekky / gpt4free

The official gpt4free repository | various collection of powerful language models
https://g4f.ai
GNU General Public License v3.0
62.34k stars 13.4k forks source link

g4f api says "this event loop is already running." #2403

Closed DarkReaperBoy closed 1 week ago

DarkReaperBoy commented 1 week ago

here is the log.

ERROR:g4f.api:this event loop is already running.
Traceback (most recent call last):
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/api/__init__.py", line 228, in streaming
    async for chunk in response:
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/client/__init__.py", line 163, in async_iter_append_model_and_provider
    async for chunk in response:
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/client/__init__.py", line 117, in async_iter_response
    async for chunk in response:
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/client/helper.py", line 55, in to_async_iterator
    for item in iterator:
                ^^^^^^^^
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/providers/asyncio.py", line 53, in to_sync_generator
    yield loop.run_until_complete(await_callback(gen.__anext__))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "uvloop/loop.pyx", line 1512, in uvloop.loop.Loop.run_until_complete
  File "uvloop/loop.pyx", line 1505, in uvloop.loop.Loop.run_until_complete
  File "uvloop/loop.pyx", line 1379, in uvloop.loop.Loop.run_forever
  File "uvloop/loop.pyx", line 520, in uvloop.loop.Loop._run
RuntimeError: this event loop is already running.

command used to run (but it ain't provider issue):

g4f api --provider Blackbox

without --provider would be:

ERROR:g4f.api:RetryProvider failed:
Liaobots: RuntimeError: this event loop is already running.
Blackbox: RuntimeError: this event loop is already running.
Traceback (most recent call last):
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/api/__init__.py", line 228, in streaming
    async for chunk in response:
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/client/__init__.py", line 163, in async_iter_append_model_and_provider
    async for chunk in response:
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/client/__init__.py", line 117, in async_iter_response
    async for chunk in response:
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/client/helper.py", line 55, in to_async_iterator
    for item in iterator:
                ^^^^^^^^
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/providers/retry_provider.py", line 68, in create_completion
    raise_exceptions(exceptions)
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/providers/retry_provider.py", line 324, in raise_exceptions
    raise RetryProviderError("RetryProvider failed:\n" + "\n".join([
g4f.errors.RetryProviderError: RetryProvider failed:
Liaobots: RuntimeError: this event loop is already running.
Blackbox: RuntimeError: this event loop is already running.
INFO:     127.0.0.1:35936 - "POST /v1/chat/completions HTTP/1.1" 200 OK
Task exception was never retrieved
future: <Task finished name='Task-10' coro=<await_callback() done, defined at /home/nako/.conda/lib/python3.12/site-packages/g4f/providers/asyncio.py:37> exception=RateLimitError('Response 402: Rate limit reached')>
Traceback (most recent call last):
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/Provider/Liaobots.py", line 272, in create_async_generator
    await raise_for_status(response)
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/requests/raise_for_status.py", line 32, in raise_for_status_async
    raise ResponseStatusError(f"Response {response.status}: {message}")
g4f.errors.ResponseStatusError: Response 500: Error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/providers/asyncio.py", line 38, in await_callback
    return await callback()
           ^^^^^^^^^^^^^^^^
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/Provider/Liaobots.py", line 295, in create_async_generator
    await raise_for_status(response)
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/requests/raise_for_status.py", line 25, in raise_for_status_async
    raise RateLimitError(f"Response {response.status}: Rate limit reached")
g4f.errors.RateLimitError: Response 402: Rate limit reached

thought maybe it's the app that is buggy, but happened same in multiple app. so i guess it's a gpt4free issue.

hlohaus commented 1 week ago

The service provider responded with error codes 500 and 402. Consider using an alternative provider other than Liaobots. Asynchronous and synchronous methods are mixed in g4f. If you use the uvloop package and not asyncio as a web server, you can no longer nest it. Mostly, a switch to the AsyncClient helps.

DarkReaperBoy commented 6 days ago

@hlohaus buuut gui works. image so i'm certain it's an api specific issue. without blackbox param it says:

ERROR:g4f.api:RetryProvider failed:
Blackbox: RuntimeError: this event loop is already running.
Liaobots: RuntimeError: this event loop is already running.
Traceback (most recent call last):
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/api/__init__.py", line 231, in streaming
    async for chunk in response:
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/client/__init__.py", line 163, in async_iter_append_model_and_provider
    async for chunk in response:
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/client/__init__.py", line 117, in async_iter_response
    async for chunk in response:
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/client/helper.py", line 55, in to_async_iterator
    for item in iterator:
                ^^^^^^^^
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/providers/retry_provider.py", line 68, in create_completion
    raise_exceptions(exceptions)
  File "/home/nako/.conda/lib/python3.12/site-packages/g4f/providers/retry_provider.py", line 324, in raise_exceptions
    raise RetryProviderError("RetryProvider failed:\n" + "\n".join([
g4f.errors.RetryProviderError: RetryProvider failed:
Blackbox: RuntimeError: this event loop is already running.
Liaobots: RuntimeError: this event loop is already running.

also...

If you use the uvloop package and not asyncio as a web server, you can no longer nest it. Mostly, a switch to the AsyncClient helps.

no, i'm using regular

g4f api
DarkReaperBoy commented 6 days ago

so did

pip uninstall uvloop

and issue got solved. thanks!