返回提交历史
Modified
docs/async_client.md
+39
-4
Added
etc/examples/image_api.py
+9
-0
Modified
g4f/gui/client/static/css/style.css
+3
-0
XFEstudio/gpt4free
Improve async client readme, Fix print styling, Add image api example
77a1f500
代码差异
3 个文件
+51
-4
@@ -9,15 +9,39 @@ Designed to maintain compatibility with the existing OpenAI API, the G4F AsyncCl
9
9
The G4F AsyncClient API offers several key features:
10
10
11
11
- **Custom Providers:** The G4F Client API allows you to use custom providers. This feature enhances the flexibility of the API, enabling it to cater to a wide range of use cases.
12
13
12
- **ChatCompletion Interface:** The G4F package provides an interface for interacting with chat models through the ChatCompletion class. This class provides methods for creating both streaming and non-streaming responses.
14
15
13
- **Streaming Responses:** The ChatCompletion.create method can return a response iteratively as and when they are received if the stream parameter is set to True.
16
17
14
- **Non-Streaming Responses:** The ChatCompletion.create method can also generate non-streaming responses.
15
- **Image Generation and Vision Models:** The G4F Client API also supports image generation and vision models, expanding its utility beyond text-based interactions.
16
17
## Initializing the Client
18
19
To utilize the G4F AsyncClient, create a new instance. Below is an example showcasing custom providers:
20
21
```python
22
from g4f.client import AsyncClient
23
from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
18
24
19
- **Image Generation and Vision Models:** The G4F Client API also supports image generation and vision models, expanding its utility beyond text-based interactions.
25
client = AsyncClient(
26
provider=OpenaiChat,
27
image_provider=Gemini,
28
...
29
)
30
```
31
32
## Configuration
20
33
34
You can set an "api_key" for your provider in the client. You also have the option to define a proxy for all outgoing requests:
35
36
```python
37
from g4f.client import AsyncClient
38
39
client = AsyncClient(
40
api_key="...",
41
proxies="http://user:pass@host",
42
...
43
)
44
```
21
45
22
46
## Using AsyncClient
23
47
@@ -62,6 +86,17 @@ response = await client.images.generate(
62
86
image_url = response.data[0].url
63
87
```
64
88
89
#### Base64 as the response format
90
91
```python
92
response = await client.images.generate(
93
prompt="a cool cat",
94
response_format="b64_json"
95
)
96
97
base64_text = response.data[0].b64_json
98
```
99
65
100
### Example usage with asyncio.gather
66
101
67
102
Start two tasks at the same time:
@@ -0,0 +1,9 @@
1
import requests
2
url = "http://localhost:1337/v1/images/generations"
3
body = {
4
"prompt": "heaven for dogs",
5
"provider": "OpenaiAccount",
6
"response_format": "b64_json",
7
}
8
data = requests.post(url, json=body, stream=True).json()
9
print(data)
@@ -1143,4 +1143,7 @@ a:-webkit-any-link {
1143
1143
.message .user {
1144
1144
display: none;
1145
1145
}
1146
.message.regenerate {
1147
opacity: 1;
1148
}
1146
1149
}