返回提交历史
Renamed
docs/legacy/legacy.md
+0
-0
Added
docs/legacy/legacy_async_client.md
+380
-0
XFEstudio/gpt4free
Update (docs/)
308d4a7f
代码差异
2 个文件
+380
-0
此文件没有可显示的逐行差异。
@@ -0,0 +1,380 @@
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
380
[Return to Home](/)