Skip to content

Commit

Permalink
Fix AsyncClient / loop is already running
Browse files Browse the repository at this point in the history
  • Loading branch information
hlohaus committed Nov 25, 2024
1 parent 6e99f70 commit 0043e04
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
19 changes: 10 additions & 9 deletions g4f/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ async def async_iter_response(
content = filter_json(content)
yield ChatCompletion.model_construct(content, finish_reason, completion_id, int(time.time()))
finally:
if hasattr(response, 'aclose'):
await safe_aclose(response)
await safe_aclose(response)

async def async_iter_append_model_and_provider(
response: AsyncChatCompletionResponseType
Expand All @@ -167,8 +166,7 @@ async def async_iter_append_model_and_provider(
chunk.provider = last_provider.get("name")
yield chunk
finally:
if hasattr(response, 'aclose'):
await safe_aclose(response)
await safe_aclose(response)

class Client(BaseClient):
def __init__(
Expand Down Expand Up @@ -292,7 +290,7 @@ async def async_generate(
proxy = self.client.proxy

response = None
if isinstance(provider, type) and issubclass(provider, AsyncGeneratorProvider):
if hasattr(provider_handler, "create_async_generator"):
messages = [{"role": "user", "content": f"Generate a image: {prompt}"}]
async for item in provider_handler.create_async_generator(model, messages, prompt=prompt, **kwargs):
if isinstance(item, ImageResponse):
Expand Down Expand Up @@ -354,7 +352,7 @@ async def async_create_variation(
if proxy is None:
proxy = self.client.proxy

if isinstance(provider, type) and issubclass(provider, AsyncGeneratorProvider):
if hasattr(provider, "create_async_generator"):
messages = [{"role": "user", "content": "create a variation of this image"}]
generator = None
try:
Expand All @@ -364,8 +362,7 @@ async def async_create_variation(
response = chunk
break
finally:
if generator and hasattr(generator, 'aclose'):
await safe_aclose(generator)
await safe_aclose(generator)
elif hasattr(provider, 'create_variation'):
if asyncio.iscoroutinefunction(provider.create_variation):
response = await provider.create_variation(image, model=model, response_format=response_format, proxy=proxy, **kwargs)
Expand Down Expand Up @@ -454,7 +451,11 @@ def create(
)
stop = [stop] if isinstance(stop, str) else stop

response = provider.create_completion(
if hasattr(provider, "create_async_generator"):
create_handler = provider.create_async_generator
else:
create_handler = provider.create_completion
response = create_handler(
model,
messages,
stream=stream,
Expand Down
3 changes: 2 additions & 1 deletion g4f/client/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def filter_none(**kwargs) -> dict:

async def safe_aclose(generator: AsyncGenerator) -> None:
try:
await generator.aclose()
if generator and hasattr(generator, 'aclose'):
await generator.aclose()
except Exception as e:
logging.warning(f"Error while closing generator: {e}")

Expand Down

0 comments on commit 0043e04

Please sign in to comment.