refactor(g4f/client/client.py): Simplify AsyncClient methods

This commit is contained in:
kqlio67 2024-11-13 14:30:44 +02:00
parent 75316a233b
commit 17057742ac

View file

@ -144,39 +144,32 @@ class Client(BaseClient):
class AsyncClient(Client): class AsyncClient(Client):
"""Legacy AsyncClient that redirects to the main Client class. """Legacy AsyncClient that redirects to the main Client class.
This class exists for backwards compatibility.""" This class exists for backwards compatibility."""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
import warnings import warnings
warnings.warn( warnings.warn(
"AsyncClient is deprecated and will be removed in a future version. " "AsyncClient is deprecated and will be removed in future versions."
"Use Client instead, which now supports both sync and async operations.", "Use Client instead, which now supports both sync and async operations.",
DeprecationWarning, DeprecationWarning,
stacklevel=2 stacklevel=2
) )
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.chat = Chat(self)
self._images = Images(self)
self.completions = Completions(self)
@property async def async_create(self, *args, **kwargs):
def images(self) -> 'Images': """Asynchronous create method that calls the synchronous method."""
return self._images return await super().async_create(*args, **kwargs)
async def async_create(self, *args, **kwargs) -> Union['ChatCompletion', AsyncIterator['ChatCompletionChunk']]: async def async_generate(self, *args, **kwargs):
response = await super().async_create(*args, **kwargs) """Asynchronous image generation method."""
async for result in response:
return result
async def async_generate(self, *args, **kwargs) -> 'ImagesResponse':
return await super().async_generate(*args, **kwargs) return await super().async_generate(*args, **kwargs)
async def _fetch_image(self, url: str) -> bytes: async def async_images(self) -> Images:
async with ClientSession() as session: """Asynchronous access to images."""
async with session.get(url) as resp: return await super().async_images()
if resp.status == 200:
return await resp.read() async def async_fetch_image(self, url: str) -> bytes:
else: """Asynchronous fetching of an image by URL."""
raise Exception(f"Failed to fetch image from {url}, status code {resp.status}") return await self._fetch_image(url)
class Completions: class Completions:
def __init__(self, client: Client, provider: ProviderType = None): def __init__(self, client: Client, provider: ProviderType = None):