返回提交历史
Modified
docs/async_client.md
+64
-36
XFEstudio/gpt4free
docs(docs/async_client.md): update AsyncClient usage examples with asyncio
7645fcb2
代码差异
1 个文件
+64
-36
@@ -1,3 +1,4 @@
1
1
2
# How to Use the G4F AsyncClient API
2
3
3
4
The AsyncClient API is the asynchronous counterpart to the standard G4F Client API. It offers the same functionality as the synchronous API, but with the added benefit of improved performance due to its asynchronous nature.
@@ -57,12 +58,19 @@ client = AsyncClient(
57
58
You can use the `ChatCompletions` endpoint to generate text completions. Here’s how you can do it:
58
59
59
60
```python
60
response = await client.chat.completions.create(
61
model="gpt-3.5-turbo",
62
messages=[{"role": "user", "content": "Say this is a test"}],
63
...
64
)
65
print(response.choices[0].message.content)
61
import asyncio
62
from g4f.client import AsyncClient
63
64
async def main():
65
client = AsyncClient()
66
response = await client.chat.completions.create(
67
[{"role": "user", "content": "say this is a test"}],
68
model="gpt-3.5-turbo"
69
)
70
71
print(response.choices[0].message.content)
72
73
asyncio.run(main())
66
74
```
67
75
68
76
### Streaming Completions
@@ -70,15 +78,20 @@ print(response.choices[0].message.content)
70
78
The `AsyncClient` also supports streaming completions. This allows you to process the response incrementally as it is generated:
71
79
72
80
```python
73
stream = client.chat.completions.create(
74
model="gpt-4",
75
messages=[{"role": "user", "content": "Say this is a test"}],
76
stream=True,
77
...
78
)
79
async for chunk in stream:
80
if chunk.choices[0].delta.content:
81
import asyncio
82
from g4f.client import AsyncClient
83
84
async def main():
85
client = AsyncClient()
86
async for chunk in await client.chat.completions.create(
87
[{"role": "user", "content": "say this is a test"}],
88
model="gpt-4",
89
stream=True,
90
):
81
91
print(chunk.choices[0].delta.content or "", end="")
92
print()
93
94
asyncio.run(main())
82
95
```
83
96
84
97
In this example:
@@ -113,13 +126,22 @@ print(response.choices[0].message.content)
113
126
You can generate images using a specified prompt:
114
127
115
128
```python
116
response = await client.images.generate(
117
model="dall-e-3",
118
prompt="a white siamese cat",
119
...
120
)
129
import asyncio
130
from g4f.client import AsyncClient
121
131
122
image_url = response.data[0].url
132
async def main():
133
client = AsyncClient(image_provider='')
134
response = await client.images.generate(
135
prompt="a white siamese cat"
136
model="flux",
137
#n=1,
138
#size="1024x1024"
139
# ...
140
)
141
image_url = response.data[0].url
142
print(image_url)
143
144
asyncio.run(main())
123
145
```
124
146
125
147
#### Base64 as the response format
@@ -139,28 +161,34 @@ Start two tasks at the same time:
139
161
140
162
```python
141
163
import asyncio
142
164
import g4f
143
165
from g4f.client import AsyncClient
144
from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
145
166
146
167
async def main():
147
168
client = AsyncClient(
148
169
provider=OpenaiChat,
149
image_provider=Gemini,
150
# other parameters...
170
image_provider=BingCreateImages,
151
171
)
152
172
153
task1 = client.chat.completions.create(
154
model="gpt-3.5-turbo",
155
messages=[{"role": "user", "content": "Say this is a test"}],
156
)
157
task2 = client.images.generate(
158
model="dall-e-3",
159
prompt="a white siamese cat",
160
)
161
responses = await asyncio.gather(task1, task2)
162
163
print(responses)
173
# Task for text completion
174
async def text_task():
175
response = await client.chat.completions.create(
176
[{"role": "user", "content": "Say this is a test"}],
177
model="gpt-3.5-turbo",
178
)
179
print(response.choices[0].message.content)
180
print()
181
182
# Task for image generation
183
async def image_task():
184
response = await client.images.generate(
185
"a white siamese cat",
186
model="flux",
187
)
188
print(f"Image generated: {response.data[0].url}")
189
190
# Execute both tasks asynchronously
191
await asyncio.gather(text_task(), image_task())
164
192
165
193
asyncio.run(main())
166
```
194
```