返回提交历史
Modified
README.md
+0
-13
Modified
docs/async_client.md
+42
-6
XFEstudio/gpt4free
Improve readme
7eb41cfd
代码差异
2 个文件
+42
-19
@@ -441,19 +441,6 @@ While we wait for gpt-5, here is a list of new models that are at least better t
441
441
| Replicate | `g4f.Provider.Replicate` | stability-ai/sdxl| llava-v1.6-34b | [replicate.com](https://replicate.com) |
442
442
| You.com | `g4f.Provider.You` | dall-e-3| ✔️ | [you.com](https://you.com) |
443
443
444
```python
445
import requests
446
from g4f.client import Client
447
448
client = Client()
449
image = requests.get("https://change_me.jpg", stream=True).raw
450
response = client.chat.completions.create(
451
"",
452
messages=[{"role": "user", "content": "what is in this picture?"}],
453
image=image
454
)
455
print(response.choices[0].message.content)
456
```
457
444
458
445
## 🔗 Powered by gpt4free
459
446
@@ -16,7 +16,7 @@ The G4F AsyncClient API offers several key features:
16
16
17
17
## Initializing the Client
18
18
19
To utilize the G4F AsyncClient, create a new instance. Below is an example showcasing custom providers:
19
To utilize the G4F `AsyncClient`, you need to create a new instance. Below is an example showcasing how to initialize the client with custom providers:
20
20
21
21
```python
22
22
from g4f.client import AsyncClient
@@ -29,25 +29,32 @@ client = AsyncClient(
29
29
)
30
30
```
31
31
32
In this example:
33
- `provider` specifies the primary provider for generating text completions.
34
- `image_provider` specifies the provider for image-related functionalities.
35
32
36
## Configuration
33
37
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:
38
You can configure the `AsyncClient` with additional settings, such as an API key for your provider and a proxy for all outgoing requests:
35
39
36
40
```python
37
41
from g4f.client import AsyncClient
38
42
39
43
client = AsyncClient(
40
api_key="...",
44
api_key="your_api_key_here",
41
45
proxies="http://user:pass@host",
42
46
...
43
47
)
44
48
```
45
49
50
- `api_key`: Your API key for the provider.
51
- `proxies`: The proxy configuration for routing requests.
52
46
53
## Using AsyncClient
47
54
48
### Text Completions:
55
### Text Completions
49
56
50
You can use the ChatCompletions endpoint to generate text completions as follows:
57
You can use the `ChatCompletions` endpoint to generate text completions. Here’s how you can do it:
51
58
52
59
```python
53
60
response = await client.chat.completions.create(
@@ -58,7 +65,9 @@ response = await client.chat.completions.create(
58
65
print(response.choices[0].message.content)
59
66
```
60
67
61
Streaming completions are also supported:
68
### Streaming Completions
69
70
The `AsyncClient` also supports streaming completions. This allows you to process the response incrementally as it is generated:
62
71
63
72
```python
64
73
stream = client.chat.completions.create(
@@ -72,6 +81,33 @@ async for chunk in stream:
72
81
print(chunk.choices[0].delta.content or "", end="")
73
82
```
74
83
84
In this example:
85
- `stream=True` enables streaming of the response.
86
87
### Example: Using a Vision Model
88
89
The following code snippet demonstrates how to use a vision model to analyze an image and generate a description based on the content of the image. This example shows how to fetch an image, send it to the model, and then process the response.
90
91
```python
92
import requests
93
from g4f.client import Client
94
from g4f.Provider import Bing
95
96
client = AsyncClient(
97
provider=Bing
98
)
99
100
image = requests.get("https://my_website/image.jpg", stream=True).raw
101
# Or: image = open("local_path/image.jpg", "rb")
102
103
response = client.chat.completions.create(
104
"",
105
messages=[{"role": "user", "content": "what is in this picture?"}],
106
image=image
107
)
108
print(response.choices[0].message.content)
109
```
110
75
111
### Image Generation:
76
112
77
113
You can generate images using a specified prompt: