XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Refactor Image Processing and Error Handling in g4f Client Module

8e272393
kqlio67 <kqlio67@users.noreply.github.com>
提交于

代码差异

4 个文件 +82 -463
Modified docs/async_client.md +48 -44
@@ -1,9 +1,10 @@
1 # G4F - Async client API Guide
2 The G4F async client API is a powerful asynchronous interface for interacting with various AI models. This guide provides comprehensive information on how to use the API effectively, including setup, usage examples, best practices, and important considerations for optimal performance.
1
2 # G4F - AsyncClient API Guide
3 The G4F AsyncClient API is a powerful asynchronous interface for interacting with various AI models. This guide provides comprehensive information on how to use the API effectively, including setup, usage examples, best practices, and important considerations for optimal performance.
3 4
4 5
5 6 ## Compatibility Note
6 The G4F async client API is designed to be compatible with the OpenAI API, making it easy for developers familiar with OpenAI's interface to transition to G4F.
7 The G4F AsyncClient API is designed to be compatible with the OpenAI API, making it easy for developers familiar with OpenAI's interface to transition to G4F.
7 8
8 9 ## Table of Contents
9 10 - [Introduction](#introduction)
@@ -26,7 +27,7 @@ The G4F async client API is designed to be compatible with the OpenAI API, makin
26 27
27 28
28 29 ## Introduction
29 The G4F async client API is an asynchronous version of the standard G4F Client API. It offers the same functionality as the synchronous API but with improved performance due to its asynchronous nature. This guide will walk you through the key features and usage of the G4F async client API.
30 The G4F AsyncClient API is an asynchronous version of the standard G4F Client API. It offers the same functionality as the synchronous API but with improved performance due to its asynchronous nature. This guide will walk you through the key features and usage of the G4F AsyncClient API.
30 31
31 32
32 33 ## Key Features
@@ -39,13 +40,13 @@ The G4F async client API is an asynchronous version of the standard G4F Client A
39 40
40 41
41 42 ## Getting Started
42 ### Initializing the Client
43 **To use the G4F `Client`, create a new instance:**
43 ### Initializing the AsyncClient
44 **To use the G4F `AsyncClient`, create a new instance:**
44 45 ```python
45 from g4f.client import Client
46 from g4f.client import AsyncClient
46 47 from g4f.Provider import OpenaiChat, Gemini
47 48
48 client = Client(
49 client = AsyncClient(
49 50 provider=OpenaiChat,
50 51 image_provider=Gemini,
51 52 # Add other parameters as needed
@@ -56,7 +57,7 @@ client = Client(
56 57 ## Creating Chat Completions
57 58 **Here’s an improved example of creating chat completions:**
58 59 ```python
59 response = await async_client.chat.completions.create(
60 response = await client.chat.completions.create(
60 61 model="gpt-4o-mini",
61 62 messages=[
62 63 {
@@ -77,9 +78,9 @@ You can adjust these parameters based on your specific needs.
77 78
78 79
79 80 ### Configuration
80 **Configure the `Client` with additional settings:**
81 **Configure the `AsyncClient` with additional settings:**
81 82 ```python
82 client = Client(
83 client = AsyncClient(
83 84 api_key="your_api_key_here",
84 85 proxies="http://user:pass@host",
85 86 # Add other parameters as needed
@@ -93,12 +94,12 @@ client = Client(
93 94 **Generate text completions using the ChatCompletions endpoint:**
94 95 ```python
95 96 import asyncio
96 from g4f.client import Client
97 from g4f.client import AsyncClient
97 98
98 99 async def main():
99 client = Client()
100 client = AsyncClient()
100 101
101 response = await client.chat.completions.async_create(
102 response = await client.chat.completions.create(
102 103 model="gpt-4o-mini",
103 104 messages=[
104 105 {
@@ -119,12 +120,12 @@ asyncio.run(main())
119 120 **Process responses incrementally as they are generated:**
120 121 ```python
121 122 import asyncio
122 from g4f.client import Client
123 from g4f.client import AsyncClient
123 124
124 125 async def main():
125 client = Client()
126
127 stream = await client.chat.completions.async_create(
126 client = AsyncClient()
127
128 stream = client.chat.completions.create(
128 129 model="gpt-4",
129 130 messages=[
130 131 {
@@ -136,7 +137,7 @@ async def main():
136 137 )
137 138
138 139 async for chunk in stream:
139 if chunk.choices[0].delta.content:
140 if chunk.choices and chunk.choices[0].delta.content:
140 141 print(chunk.choices[0].delta.content, end="")
141 142
142 143 asyncio.run(main())
@@ -150,14 +151,14 @@ asyncio.run(main())
150 151 import g4f
151 152 import requests
152 153 import asyncio
153 from g4f.client import Client
154 from g4f.client import AsyncClient
154 155
155 156 async def main():
156 client = Client()
157 client = AsyncClient()
157 158
158 159 image = requests.get("https://raw.githubusercontent.com/xtekky/gpt4free/refs/heads/main/docs/cat.jpeg", stream=True).raw
159 160
160 response = await client.chat.completions.async_create(
161 response = await client.chat.completions.create(
161 162 model=g4f.models.default,
162 163 provider=g4f.Provider.Bing,
163 164 messages=[
@@ -180,12 +181,12 @@ asyncio.run(main())
180 181 **Generate images using a specified prompt:**
181 182 ```python
182 183 import asyncio
183 from g4f.client import Client
184 from g4f.client import AsyncClient
184 185
185 186 async def main():
186 client = Client()
187 client = AsyncClient()
187 188
188 response = await client.images.async_generate(
189 response = await client.images.generate(
189 190 prompt="a white siamese cat",
190 191 model="flux"
191 192 )
@@ -201,12 +202,12 @@ asyncio.run(main())
201 202 #### Base64 Response Format
202 203 ```python
203 204 import asyncio
204 from g4f.client import Client
205 from g4f.client import AsyncClient
205 206
206 207 async def main():
207 client = Client()
208 client = AsyncClient()
208 209
209 response = await client.images.async_generate(
210 response = await client.images.generate(
210 211 prompt="a white siamese cat",
211 212 model="flux",
212 213 response_format="b64_json"
@@ -224,13 +225,13 @@ asyncio.run(main())
224 225 **Execute multiple tasks concurrently:**
225 226 ```python
226 227 import asyncio
227 from g4f.client import Client
228 from g4f.client import AsyncClient
228 229
229 230 async def main():
230 client = Client()
231 client = AsyncClient()
231 232
232 task1 = client.chat.completions.async_create(
233 model="gpt-4o-mini",
233 task1 = client.chat.completions.create(
234 model=None,
234 235 messages=[
235 236 {
236 237 "role": "user",
@@ -239,18 +240,21 @@ async def main():
239 240 ]
240 241 )
241 242
242 task2 = client.images.async_generate(
243 task2 = client.images.generate(
243 244 model="flux",
244 245 prompt="a white siamese cat"
245 246 )
246 247
247 chat_response, image_response = await asyncio.gather(task1, task2)
248
249 print("Chat Response:")
250 print(chat_response.choices[0].message.content)
251
252 print("Image Response:")
253 print(image_response.data[0].url)
248 try:
249 chat_response, image_response = await asyncio.gather(task1, task2)
250
251 print("Chat Response:")
252 print(chat_response.choices[0].message.content)
253
254 print("\nImage Response:")
255 print(image_response.data[0].url)
256 except Exception as e:
257 print(f"An error occurred: {e}")
254 258
255 259 asyncio.run(main())
256 260 ```
@@ -286,7 +290,7 @@ client = AsyncClient(provider=g4f.Provider.OpenaiChat)
286 290
287 291 # or
288 292
289 response = await client.chat.completions.async_create(
293 response = await client.chat.completions.create(
290 294 model="gpt-4",
291 295 provider=g4f.Provider.Bing,
292 296 messages=[
@@ -306,7 +310,7 @@ Implementing proper error handling and following best practices is crucial when
306 310 1. **Use try-except blocks to catch and handle exceptions:**
307 311 ```python
308 312 try:
309 response = await client.chat.completions.async_create(
313 response = await client.chat.completions.create(
310 314 model="gpt-4o-mini",
311 315 messages=[
312 316 {
@@ -368,7 +372,7 @@ logger = logging.getLogger(__name__)
368 372
369 373 async def make_api_call():
370 374 try:
371 response = await client.chat.completions.async_create(...)
375 response = await client.chat.completions.create(...)
372 376 logger.info(f"API call successful. Tokens used: {response.usage.total_tokens}")
373 377 except Exception as e:
374 378 logger.error(f"API call failed: {e}")
@@ -387,7 +391,7 @@ def get_cached_response(query):
387 391 ```
388 392
389 393 ## Conclusion
390 The G4F async client API provides a powerful and flexible way to interact with various AI models asynchronously. By leveraging its features and following best practices, you can build efficient and responsive applications that harness the power of AI for text generation, image analysis, and image creation.
394 The G4F AsyncClient API provides a powerful and flexible way to interact with various AI models asynchronously. By leveraging its features and following best practices, you can build efficient and responsive applications that harness the power of AI for text generation, image analysis, and image creation.
391 395
392 396 Remember to handle errors gracefully, implement rate limiting, and monitor your API usage to ensure optimal performance and reliability in your applications.
393 397
Renamed docs/legacy.md +0 -0
此文件没有可显示的逐行差异。
Deleted docs/legacy/legacy_async_client.md +0 -380
@@ -1,380 +0,0 @@
1 # G4F - Legacy AsyncClient API Guide
2
3 **IMPORTANT: This guide refers to the old implementation of AsyncClient. The new version of G4F now supports both synchronous and asynchronous operations through a unified interface. Please refer to the [new AsyncClient documentation](https://github.com/xtekky/gpt4free/blob/main/docs/async_client.md) for the latest information.**
4
5 This guide provides comprehensive information on how to use the G4F AsyncClient API, including setup, usage examples, best practices, and important considerations for optimal performance.
6
7 ## Compatibility Note
8 The G4F AsyncClient API is designed to be compatible with the OpenAI API, making it easy for developers familiar with OpenAI's interface to transition to G4F. However, please note that this is the old version, and you should migrate to the new implementation for better support and features.
9
10 ## Table of Contents
11 - [Introduction](#introduction)
12 - [Key Features](#key-features)
13 - [Getting Started](#getting-started)
14 - [Initializing the Client](#initializing-the-client)
15 - [Creating Chat Completions](#creating-chat-completions)
16 - [Configuration](#configuration)
17 - [Usage Examples](#usage-examples)
18 - [Text Completions](#text-completions)
19 - [Streaming Completions](#streaming-completions)
20 - [Using a Vision Model](#using-a-vision-model)
21 - [Image Generation](#image-generation)
22 - [Concurrent Tasks](#concurrent-tasks-with-asynciogather)
23 - [Available Models and Providers](#available-models-and-providers)
24 - [Error Handling and Best Practices](#error-handling-and-best-practices)
25 - [Rate Limiting and API Usage](#rate-limiting-and-api-usage)
26 - [Conclusion](#conclusion)
27
28 ## Introduction
29 This is the old version: The G4F AsyncClient API is an asynchronous version of the standard G4F Client API. It offers the same functionality as the synchronous API but with improved performance due to its asynchronous nature. This guide will walk you through the key features and usage of the G4F AsyncClient API.
30
31 ## Key Features
32 - **Custom Providers**: Use custom providers for enhanced flexibility.
33 - **ChatCompletion Interface**: Interact with chat models through the ChatCompletion class.
34 - **Streaming Responses**: Get responses iteratively as they are received.
35 - **Non-Streaming Responses**: Generate complete responses in a single call.
36 - **Image Generation and Vision Models**: Support for image-related tasks.
37
38 ## Getting Started
39 **To ignore DeprecationWarnings related to the AsyncClient, you can use the following code:***
40 ```python
41 import warnings
42
43 # Ignore DeprecationWarning for AsyncClient
44 warnings.filterwarnings("ignore", category=DeprecationWarning, module="g4f.client")
45 ```
46
47 ### Initializing the Client
48 **To use the G4F `Client`, create a new instance:**
49 ```python
50 from g4f.client import AsyncClient
51 from g4f.Provider import OpenaiChat, Gemini
52
53 client = AsyncClient(
54 provider=OpenaiChat,
55 image_provider=Gemini,
56 # Add other parameters as needed
57 )
58 ```
59
60 ## Creating Chat Completions
61 **Here's an improved example of creating chat completions:**
62 ```python
63 response = await async_client.chat.completions.create(
64 model="gpt-3.5-turbo",
65 messages=[
66 {
67 "role": "user",
68 "content": "Say this is a test"
69 }
70 ]
71 # Add other parameters as needed
72 )
73 ```
74
75 **This example:**
76 - Asks a specific question `Say this is a test`
77 - Configures various parameters like temperature and max_tokens for more control over the output
78 - Disables streaming for a complete response
79
80 You can adjust these parameters based on your specific needs.
81
82 ### Configuration
83 **Configure the `AsyncClient` with additional settings:**
84 ```python
85 client = Client(
86 api_key="your_api_key_here",
87 proxies="http://user:pass@host",
88 # Add other parameters as needed
89 )
90 ```
91
92 ## Usage Examples
93 ### Text Completions
94 **Generate text completions using the ChatCompletions endpoint:**
95 ```python
96 import asyncio
97 import warnings
98 from g4f.client import AsyncClient
99
100 # Ігноруємо DeprecationWarning
101 warnings.filterwarnings("ignore", category=DeprecationWarning)
102
103 async def main():
104 client = AsyncClient()
105
106 response = await client.chat.completions.async_create(
107 model="gpt-3.5-turbo",
108 messages=[
109 {
110 "role": "user",
111 "content": "Say this is a test"
112 }
113 ]
114 )
115
116 print(response.choices[0].message.content)
117
118 asyncio.run(main())
119 ```
120
121 ### Streaming Completions
122 **Process responses incrementally as they are generated:**
123 ```python
124 import asyncio
125 from g4f.client import AsyncClient
126
127 async def main():
128 client = AsyncClient()
129
130 stream = await client.chat.completions.async_create(
131 model="gpt-4",
132 messages=[
133 {
134 "role": "user",
135 "content": "Say this is a test"
136 }
137 ],
138 stream=True,
139 )
140
141 async for chunk in stream:
142 if chunk.choices[0].delta.content:
143 print(chunk.choices[0].delta.content, end="")
144
145 asyncio.run(main())
146 ```
147
148 ### Using a Vision Model
149 **Analyze an image and generate a description:**
150 ```python
151 import g4f
152 import requests
153 import asyncio
154 from g4f.client import AsyncClient
155
156 async def main():
157 client = AsyncClient()
158
159 image = requests.get("https://raw.githubusercontent.com/xtekky/gpt4free/refs/heads/main/docs/cat.jpeg", stream=True).raw
160
161 response = await client.chat.completions.async_create(
162 model=g4f.models.default,
163 provider=g4f.Provider.Bing,
164 messages=[
165 {
166 "role": "user",
167 "content": "What's in this image?"
168 }
169 ],
170 image=image
171 )
172
173 print(response.choices[0].message.content)
174
175 asyncio.run(main())
176 ```
177
178 ### Image Generation
179 **Generate images using a specified prompt:**
180 ```python
181 import asyncio
182 from g4f.client import AsyncClient
183
184 async def main():
185 client = AsyncClient()
186
187 response = await client.images.async_generate(
188 prompt="a white siamese cat",
189 model="flux"
190 )
191
192 image_url = response.data[0].url
193 print(f"Generated image URL: {image_url}")
194
195 asyncio.run(main())
196 ```
197
198 #### Base64 Response Format
199 ```python
200 import asyncio
201 from g4f.client import AsyncClient
202
203 async def main():
204 client = AsyncClient()
205
206 response = await client.images.async_generate(
207 prompt="a white siamese cat",
208 model="flux",
209 response_format="b64_json"
210 )
211
212 base64_text = response.data[0].b64_json
213 print(base64_text)
214
215 asyncio.run(main())
216 ```
217
218 ### Concurrent Tasks with asyncio.gather
219 **Execute multiple tasks concurrently:**
220 ```python
221 import asyncio
222 import warnings
223 from g4f.client import AsyncClient
224
225 # Ignore DeprecationWarning for AsyncClient
226 warnings.filterwarnings("ignore", category=DeprecationWarning, module="g4f.client")
227
228 async def main():
229 client = AsyncClient()
230
231 task1 = client.chat.completions.async_create(
232 model="gpt-3.5-turbo",
233 messages=[
234 {
235 "role": "user",
236 "content": "Say this is a test"
237 }
238 ]
239 )
240
241 task2 = client.images.async_generate(
242 model="flux",
243 prompt="a white siamese cat"
244 )
245
246 chat_response, image_response = await asyncio.gather(task1, task2)
247
248 print("Chat Response:")
249 print(chat_response.choices[0].message.content)
250
251 print("Image Response:")
252 print(image_response.data[0].url)
253
254 asyncio.run(main())
255 ```
256
257 ## Available Models and Providers
258 This is the old version: The G4F AsyncClient supports a wide range of AI models and providers, allowing you to choose the best option for your specific use case.
259 **Here's a brief overview of the available models and providers:**
260
261 ### Models
262 - GPT-3.5-Turbo
263 - GPT-4
264 - DALL-E 3
265 - Gemini
266 - Claude (Anthropic)
267 - And more...
268
269 ### Providers
270 - OpenAI
271 - Google (for Gemini)
272 - Anthropic
273 - Bing
274 - Custom providers
275
276 **To use a specific model or provider, specify it when creating the client or in the API call:**
277 ```python
278 client = AsyncClient(provider=g4f.Provider.OpenaiChat)
279
280 # or
281
282 response = await client.chat.completions.async_create(
283 model="gpt-4",
284 provider=g4f.Provider.Bing,
285 messages=[
286 {
287 "role": "user",
288 "content": "Hello, world!"
289 }
290 ]
291 )
292 ```
293
294 ## Error Handling and Best Practices
295 Implementing proper error handling and following best practices is crucial when working with the G4F AsyncClient API. This ensures your application remains robust and can gracefully handle various scenarios. **Here are some key practices to follow:**
296
297 1. **Use try-except blocks to catch and handle exceptions:**
298 ```python
299 try:
300 response = await client.chat.completions.async_create(
301 model="gpt-3.5-turbo",
302 messages=[
303 {
304 "role": "user",
305 "content": "Hello, world!"
306 }
307 ]
308 )
309 except Exception as e:
310 print(f"An error occurred: {e}")
311 ```
312
313 2. **Check the response status and handle different scenarios:**
314 ```python
315 if response.choices:
316 print(response.choices[0].message.content)
317 else:
318 print("No response generated")
319 ```
320
321 3. **Implement retries for transient errors:**
322 ```python
323 import asyncio
324 from tenacity import retry, stop_after_attempt, wait_exponential
325
326 @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
327 async def make_api_call():
328 # Your API call here
329 pass
330 ```
331
332 ## Rate Limiting and API Usage
333 This is the old version: When working with the G4F AsyncClient API, it's important to implement rate limiting and monitor your API usage. This helps ensure fair usage, prevents overloading the service, and optimizes your application's performance. **Here are some key strategies to consider:**
334
335 1. **Implement rate limiting in your application:**
336 ```python
337 import asyncio
338 from aiolimiter import AsyncLimiter
339
340 rate_limit = AsyncLimiter(max_rate=10, time_period=1) # 10 requests per second
341
342 async def make_api_call():
343 async with rate_limit:
344 # Your API call here
345 pass
346 ```
347
348 2. **Monitor your API usage and implement logging:**
349 ```python
350 import logging
351
352 logging.basicConfig(level=logging.INFO)
353 logger = logging.getLogger(__name__)
354
355 async def make_api_call():
356 try:
357 response = await client.chat.completions.async_create(...)
358 logger.info(f"API call successful. Tokens used: {response.usage.total_tokens}")
359 except Exception as e:
360 logger.error(f"API call failed: {e}")
361 ```
362
363 3. **Use caching to reduce API calls for repeated queries:**
364 ```python
365 from functools import lru_cache
366
367 @lru_cache(maxsize=100)
368 def get_cached_response(query):
369 # Your API call here
370 pass
371 ```
372
373 ## Conclusion
374 This is the old version: The G4F AsyncClient API provides a powerful and flexible way to interact with various AI models asynchronously. By leveraging its features and following best practices, you can build efficient and responsive applications that harness the power of AI for text generation, image analysis, and image creation.
375
376 Remember to handle errors gracefully, implement rate limiting, and monitor your API usage to ensure optimal performance and reliability in your applications.
377
378
379 [Return to Home](/)
Modified g4f/client/__init__.py +34 -39
@@ -247,7 +247,7 @@ class Images:
247 247 """
248 248 Synchronous generate method that runs the async_generate method in an event loop.
249 249 """
250 return asyncio.run(self.async_generate(prompt, model, provider, response_format=response_format, proxy=proxy **kwargs))
250 return asyncio.run(self.async_generate(prompt, model, provider, response_format=response_format, proxy=proxy, **kwargs))
251 251
252 252 async def async_generate(self, prompt: str, model: str = None, provider: ProviderType = None, response_format: str = "url", proxy: str = None, **kwargs) -> ImagesResponse:
253 253 if provider is None:
@@ -261,7 +261,7 @@ class Images:
261 261
262 262 if isinstance(provider_handler, IterListProvider):
263 263 if provider_handler.providers:
264 provider_handler = provider.providers[0]
264 provider_handler = provider_handler.providers[0]
265 265 else:
266 266 raise ValueError(f"IterListProvider for model {model} has no providers")
267 267
@@ -287,44 +287,39 @@ class Images:
287 287 raise NoImageResponseError(f"Unexpected response type: {type(response)}")
288 288
289 289 async def _process_image_response(self, response: ImageResponse, response_format: str, proxy: str = None, model: str = None, provider: str = None) -> ImagesResponse:
290 async def process_image_item(session: aiohttp.ClientSession, image_data: str):
291 if image_data.startswith('http://') or image_data.startswith('https://'):
292 if response_format == "url":
293 return Image(url=image_data, revised_prompt=response.alt)
294 elif response_format == "b64_json":
295 # Fetch the image data and convert it to base64
296 image_content = await self._fetch_image(session, image_data)
297 file_name = self._save_image(image_data_bytes)
298 b64_json = base64.b64encode(image_content).decode('utf-8')
299 return Image(b64_json=b64_json, url=file_name, revised_prompt=response.alt)
300 else:
301 # Assume image_data is base64 data or binary
302 if response_format == "url":
303 if image_data.startswith('data:image'):
304 # Remove the data URL scheme and get the base64 data
305 base64_data = image_data.split(',', 1)[-1]
306 else:
307 base64_data = image_data
308 # Decode the base64 data
309 image_data_bytes = base64.b64decode(base64_data)
310 # Convert bytes to an image
290 async def process_image_item(session: aiohttp.ClientSession, image_data: str):
291 image_data_bytes = None
292 if image_data.startswith("http://") or image_data.startswith("https://"):
293 if response_format == "url":
294 return Image(url=image_data, revised_prompt=response.alt)
295 elif response_format == "b64_json":
296 # Fetch the image data and convert it to base64
297 image_data_bytes = await self._fetch_image(session, image_data)
298 b64_json = base64.b64encode(image_data_bytes).decode("utf-8")
299 return Image(b64_json=b64_json, url=image_data, revised_prompt=response.alt)
300 else:
301 # Assume image_data is base64 data or binary
302 if response_format == "url":
303 if image_data.startswith("data:image"):
304 # Remove the data URL scheme and get the base64 data
305 base64_data = image_data.split(",", 1)[-1]
306 else:
307 base64_data = image_data
308 # Decode the base64 data
309 image_data_bytes = base64.b64decode(base64_data)
310 if image_data_bytes:
311 311 file_name = self._save_image(image_data_bytes)
312 312 return Image(url=file_name, revised_prompt=response.alt)
313 elif response_format == "b64_json":
314 if isinstance(image_data, bytes):
315 file_name = self._save_image(image_data_bytes)
316 b64_json = base64.b64encode(image_data).decode('utf-8')
317 else:
318 b64_json = image_data # If already base64-encoded string
319 return Image(b64_json=b64_json, url=file_name, revised_prompt=response.alt)
320
321 last_provider = get_last_provider(True)
322 async with aiohttp.ClientSession(cookies=response.get("cookies"), connector=get_connector(proxy=proxy)) as session:
323 return ImagesResponse(
324 await asyncio.gather(*[process_image_item(session, image_data) for image_data in response.get_list()]),
325 model=last_provider.get("model") if model is None else model,
326 provider=last_provider.get("name") if provider is None else provider
327 )
313 else:
314 raise ValueError("Unable to process image data")
315
316 last_provider = get_last_provider(True)
317 async with aiohttp.ClientSession(cookies=response.get("cookies"), connector=get_connector(proxy=proxy)) as session:
318 return ImagesResponse(
319 await asyncio.gather(*[process_image_item(session, image_data) for image_data in response.get_list()]),
320 model=last_provider.get("model") if model is None else model,
321 provider=last_provider.get("name") if provider is None else provider
322 )
328 323
329 324 async def _fetch_image(self, session: aiohttp.ClientSession, url: str) -> bytes:
330 325 # Asynchronously fetch image data from the URL
@@ -465,4 +460,4 @@ class AsyncImages(Images):
465 460 async def create_variation(self, image: Union[str, bytes], model: str = None, provider: ProviderType = None, response_format: str = "url", **kwargs) -> ImagesResponse:
466 461 return await self.async_create_variation(
467 462 image, model, provider, response_format, **kwargs
468 )
463 )