返回提交历史
Modified
g4f/client/client.py
+14
-21
XFEstudio/gpt4free
refactor(g4f/client/client.py): Simplify AsyncClient methods
17057742
代码差异
1 个文件
+14
-21
@@ -144,39 +144,32 @@ class Client(BaseClient):
144
144
class AsyncClient(Client):
145
145
"""Legacy AsyncClient that redirects to the main Client class.
146
146
This class exists for backwards compatibility."""
147
147
148
148
def __init__(self, *args, **kwargs):
149
149
import warnings
150
150
warnings.warn(
151
"AsyncClient is deprecated and will be removed in a future version. "
151
"AsyncClient is deprecated and will be removed in future versions."
152
152
"Use Client instead, which now supports both sync and async operations.",
153
153
DeprecationWarning,
154
154
stacklevel=2
155
155
)
156
156
super().__init__(*args, **kwargs)
157
self.chat = Chat(self)
158
self._images = Images(self)
159
self.completions = Completions(self)
160
161
@property
162
def images(self) -> 'Images':
163
return self._images
164
157
165
async def async_create(self, *args, **kwargs) -> Union['ChatCompletion', AsyncIterator['ChatCompletionChunk']]:
166
response = await super().async_create(*args, **kwargs)
167
async for result in response:
168
return result
158
async def async_create(self, *args, **kwargs):
159
"""Asynchronous create method that calls the synchronous method."""
160
return await super().async_create(*args, **kwargs)
169
161
170
async def async_generate(self, *args, **kwargs) -> 'ImagesResponse':
162
async def async_generate(self, *args, **kwargs):
163
"""Asynchronous image generation method."""
171
164
return await super().async_generate(*args, **kwargs)
172
165
173
async def _fetch_image(self, url: str) -> bytes:
174
async with ClientSession() as session:
175
async with session.get(url) as resp:
176
if resp.status == 200:
177
return await resp.read()
178
else:
179
raise Exception(f"Failed to fetch image from {url}, status code {resp.status}")
166
async def async_images(self) -> Images:
167
"""Asynchronous access to images."""
168
return await super().async_images()
169
170
async def async_fetch_image(self, url: str) -> bytes:
171
"""Asynchronous fetching of an image by URL."""
172
return await self._fetch_image(url)
180
173
181
174
class Completions:
182
175
def __init__(self, client: Client, provider: ProviderType = None):