返回提交历史
Modified
README.md
+16
-253
Modified
docs/client.md
+3
-4
Added
docs/docker.md
+46
-0
Added
docs/leagcy.md
+182
-0
Added
docs/requirements.md
+46
-0
XFEstudio/gpt4free
Update readme / docs
5612ee4c
代码差异
5 个文件
+293
-257
@@ -91,7 +91,7 @@ docker run -p 8080:8080 -p 1337:1337 -p 7900:7900 --shm-size="2g" hlohaus789/g4f
91
91
or set the api base in your client to: [http://localhost:1337/v1](http://localhost:1337/v1)
92
92
4. (Optional) If you need to log in to a provider, you can view the desktop from the container here: http://localhost:7900/?autoconnect=1&resize=scale&password=secret.
93
93
94
#### Use python package
94
#### Use python
95
95
96
96
##### Prerequisites:
97
97
@@ -104,32 +104,11 @@ Install all supported tools / all used packages:
104
104
```
105
105
pip install -U g4f[all]
106
106
```
107
Install required packages for the OpenaiChat provider:
108
```
109
pip install -U g4f[openai]
110
```
111
Install required packages for the interference api:
112
```
113
pip install -U g4f[api]
114
```
115
Install required packages for the web interface:
116
```
117
pip install -U g4f[gui]
118
```
119
Install required packages for uploading / generating images:
120
```
121
pip install -U g4f[image]
122
```
123
Install required packages for providers with webdriver:
124
```
125
pip install -U g4f[webdriver]
126
```
127
Install required packages for proxy support:
128
```
129
pip install -U aiohttp_socks
130
```
131
107
132
##### or:
108
Or use: [Partially Requirements](/docs/requirements.md)
109
110
111
##### Install from source:
133
112
134
113
1. Clone the GitHub repository:
135
114
@@ -179,56 +158,14 @@ import g4f
179
158
...
180
159
```
181
160
182
#### Docker for Developers
183
184
If you have Docker installed, you can easily set up and run the project without manually installing dependencies.
161
##### Install using Docker
185
162
186
1. First, ensure you have both Docker and Docker Compose installed.
163
Or use: [Build Docker](/docs/docker.md)
187
164
188
- [Install Docker](https://docs.docker.com/get-docker/)
189
- [Install Docker Compose](https://docs.docker.com/compose/install/)
190
191
2. Clone the GitHub repo:
192
193
```bash
194
git clone https://github.com/xtekky/gpt4free.git
195
```
196
197
3. Navigate to the project directory:
198
199
```bash
200
cd gpt4free
201
```
202
203
4. Build the Docker image:
204
205
```bash
206
docker pull selenium/node-chrome
207
docker-compose build
208
```
209
210
5. Start the service using Docker Compose:
211
212
```bash
213
docker-compose up
214
```
215
216
Your server will now be running at `http://localhost:1337`. You can interact with the API or run your tests as you would normally.
217
218
To stop the Docker containers, simply run:
219
220
```bash
221
docker-compose down
222
```
223
224
> [!Note]
225
> When using Docker, any changes you make to your local files will be reflected in the Docker container thanks to the volume mapping in the `docker-compose.yml` file. If you add or remove dependencies, however, you'll need to rebuild the Docker image using `docker-compose build`.
226
165
227
166
## 💡 Usage
228
167
229
### New Client with Image Generation
230
231
#### not jet released
168
#### Image Generation
232
169
233
170
```python
234
171
from g4f.client import Client
@@ -245,90 +182,21 @@ Result:
245
182
246
183
[](/docs/client.md)
247
184
248
[to the client API](/docs/client.md)
249
250
### The Web UI
251
252
To start the web interface, type the following codes in the command line.
253
254
```python
255
from g4f.gui import run_gui
256
run_gui()
257
```
258
259
### The `g4f` Package
260
261
#### ChatCompletion
262
263
```python
264
import g4f
265
266
g4f.debug.logging = True # Enable debug logging
267
g4f.debug.version_check = False # Disable automatic version checking
268
print(g4f.Provider.Bing.params) # Print supported args for Bing
269
270
# Using automatic a provider for the given model
271
## Streamed completion
272
response = g4f.ChatCompletion.create(
273
model="gpt-3.5-turbo",
274
messages=[{"role": "user", "content": "Hello"}],
275
stream=True,
276
)
277
for message in response:
278
print(message, flush=True, end='')
279
280
## Normal response
281
response = g4f.ChatCompletion.create(
282
model=g4f.models.gpt_4,
283
messages=[{"role": "user", "content": "Hello"}],
284
) # Alternative model setting
185
#### Text Generation
285
186
286
print(response)
287
```
187
and more:
288
188
289
##### Completion
189
- [Documentation for new Client](/docs/client.md)
190
- [Documentation for leagcy API](/docs/leagcy.md)
290
191
291
```python
292
import g4f
293
294
allowed_models = [
295
'code-davinci-002',
296
'text-ada-001',
297
'text-babbage-001',
298
'text-curie-001',
299
'text-davinci-002',
300
'text-davinci-003'
301
]
302
192
303
response = g4f.Completion.create(
304
model='text-davinci-003',
305
prompt='say this is a test'
306
)
307
308
print(response)
309
```
193
#### Web UI
310
194
311
##### Providers
195
To start the web interface, type the following codes in the command line.
312
196
313
197
```python
314
import g4f
315
316
# Print all available providers
317
print([
318
provider.__name__
319
for provider in g4f.Provider.__providers__
320
if provider.working
321
])
322
323
# Execute with a specific provider
324
response = g4f.ChatCompletion.create(
325
model="gpt-3.5-turbo",
326
provider=g4f.Provider.Aichat,
327
messages=[{"role": "user", "content": "Hello"}],
328
stream=True,
329
)
330
for message in response:
331
print(message)
198
from g4f.gui import run_gui
199
run_gui()
332
200
```
333
201
334
202
##### Cookies / Access Token
@@ -358,111 +226,6 @@ pip install browser_cookie3
358
226
pip install g4f[webdriver]
359
227
```
360
228
361
##### Image Upload & Generation
362
363
Image upload and generation are supported by three main providers:
364
365
- **Bing & Other GPT-4 Providers:** Utilizes Microsoft's Image Creator.
366
- **Google Gemini:** Available for free accounts with IP addresses outside Europe.
367
- **OpenaiChat with GPT-4:** Accessible for users with a Plus subscription.
368
369
```python
370
import g4f
371
372
# Setting up the request for image creation
373
response = g4f.ChatCompletion.create(
374
model=g4f.models.default, # Using the default model
375
provider=g4f.Provider.Gemini, # Specifying the provider as Gemini
376
messages=[{"role": "user", "content": "Create an image like this"}],
377
image=open("images/g4f.png", "rb"), # Image input can be a data URI, bytes, PIL Image, or IO object
378
image_name="g4f.png" # Optional: specifying the filename
379
)
380
381
# Displaying the response
382
print(response)
383
384
from g4f.image import ImageResponse
385
386
# Get image links from response
387
for chunk in g4f.ChatCompletion.create(
388
model=g4f.models.default, # Using the default model
389
provider=g4f.Provider.OpenaiChat, # Specifying the provider as OpenaiChat
390
messages=[{"role": "user", "content": "Create images with dogs"}],
391
access_token="...", # Need a access token from a plus user
392
stream=True,
393
ignore_stream=True
394
):
395
if isinstance(chunk, ImageResponse):
396
print(chunk.images) # Print generated image links
397
print(chunk.alt) # Print used prompt for image generation
398
```
399
400
##### Using Browser
401
402
Some providers using a browser to bypass the bot protection. They using the selenium webdriver to control the browser. The browser settings and the login data are saved in a custom directory. If the headless mode is enabled, the browser windows are loaded invisibly. For performance reasons, it is recommended to reuse the browser instances and close them yourself at the end:
403
404
```python
405
import g4f
406
from undetected_chromedriver import Chrome, ChromeOptions
407
from g4f.Provider import (
408
Bard,
409
Poe,
410
AItianhuSpace,
411
MyShell,
412
PerplexityAi,
413
)
414
415
options = ChromeOptions()
416
options.add_argument("--incognito");
417
webdriver = Chrome(options=options, headless=True)
418
for idx in range(10):
419
response = g4f.ChatCompletion.create(
420
model=g4f.models.default,
421
provider=g4f.Provider.MyShell,
422
messages=[{"role": "user", "content": "Suggest me a name."}],
423
webdriver=webdriver
424
)
425
print(f"{idx}:", response)
426
webdriver.quit()
427
```
428
429
##### Async Support
430
431
To enhance speed and overall performance, execute providers asynchronously. The total execution time will be determined by the duration of the slowest provider's execution.
432
433
```python
434
import g4f
435
import asyncio
436
437
_providers = [
438
g4f.Provider.Aichat,
439
g4f.Provider.ChatBase,
440
g4f.Provider.Bing,
441
g4f.Provider.GptGo,
442
g4f.Provider.You,
443
g4f.Provider.Yqcloud,
444
]
445
446
async def run_provider(provider: g4f.Provider.BaseProvider):
447
try:
448
response = await g4f.ChatCompletion.create_async(
449
model=g4f.models.default,
450
messages=[{"role": "user", "content": "Hello"}],
451
provider=provider,
452
)
453
print(f"{provider.__name__}:", response)
454
except Exception as e:
455
print(f"{provider.__name__}:", e)
456
457
async def run_all():
458
calls = [
459
run_provider(provider) for provider in _providers
460
]
461
await asyncio.gather(*calls)
462
463
asyncio.run(run_all())
464
```
465
466
229
##### Proxy and Timeout Support
467
230
468
231
All providers support specifying a proxy and increasing timeout in the create functions.
@@ -1,4 +1,4 @@
1
### G4F Client API Documentation (Beta Version)
1
### G4F - Client API (Beta Version)
2
2
3
3
#### Introduction
4
4
@@ -31,7 +31,7 @@ from g4f.client import Client
31
31
from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
32
32
33
33
client = Client(
34
text_provider=OpenaiChat,
34
provider=OpenaiChat,
35
35
image_provider=Gemini,
36
36
proxies=None
37
37
)
@@ -89,5 +89,4 @@ Original / Variant:
89
89
[](/docs/client.md)
90
90
[](/docs/client.md)
91
91
92
[Return to Documentation Home](/)
93
```
92
[Return to Home](/)
@@ -0,0 +1,46 @@
1
### G4F - Docker
2
3
If you have Docker installed, you can easily set up and run the project without manually installing dependencies.
4
5
1. First, ensure you have both Docker and Docker Compose installed.
6
7
- [Install Docker](https://docs.docker.com/get-docker/)
8
- [Install Docker Compose](https://docs.docker.com/compose/install/)
9
10
2. Clone the GitHub repo:
11
12
```bash
13
git clone https://github.com/xtekky/gpt4free.git
14
```
15
16
3. Navigate to the project directory:
17
18
```bash
19
cd gpt4free
20
```
21
22
4. Build the Docker image:
23
24
```bash
25
docker pull selenium/node-chrome
26
docker-compose build
27
```
28
29
5. Start the service using Docker Compose:
30
31
```bash
32
docker-compose up
33
```
34
35
Your server will now be running at `http://localhost:1337`. You can interact with the API or run your tests as you would normally.
36
37
To stop the Docker containers, simply run:
38
39
```bash
40
docker-compose down
41
```
42
43
> [!Note]
44
> When using Docker, any changes you make to your local files will be reflected in the Docker container thanks to the volume mapping in the `docker-compose.yml` file. If you add or remove dependencies, however, you'll need to rebuild the Docker image using `docker-compose build`.
45
46
[Return to Home](/)
@@ -0,0 +1,182 @@
1
### G4F - Leagcy API
2
3
#### ChatCompletion
4
5
```python
6
import g4f
7
8
g4f.debug.logging = True # Enable debug logging
9
g4f.debug.version_check = False # Disable automatic version checking
10
print(g4f.Provider.Bing.params) # Print supported args for Bing
11
12
# Using automatic a provider for the given model
13
## Streamed completion
14
response = g4f.ChatCompletion.create(
15
model="gpt-3.5-turbo",
16
messages=[{"role": "user", "content": "Hello"}],
17
stream=True,
18
)
19
for message in response:
20
print(message, flush=True, end='')
21
22
## Normal response
23
response = g4f.ChatCompletion.create(
24
model=g4f.models.gpt_4,
25
messages=[{"role": "user", "content": "Hello"}],
26
) # Alternative model setting
27
28
print(response)
29
```
30
31
##### Completion
32
33
```python
34
import g4f
35
36
allowed_models = [
37
'code-davinci-002',
38
'text-ada-001',
39
'text-babbage-001',
40
'text-curie-001',
41
'text-davinci-002',
42
'text-davinci-003'
43
]
44
45
response = g4f.Completion.create(
46
model='text-davinci-003',
47
prompt='say this is a test'
48
)
49
50
print(response)
51
```
52
53
##### Providers
54
55
```python
56
import g4f
57
58
# Print all available providers
59
print([
60
provider.__name__
61
for provider in g4f.Provider.__providers__
62
if provider.working
63
])
64
65
# Execute with a specific provider
66
response = g4f.ChatCompletion.create(
67
model="gpt-3.5-turbo",
68
provider=g4f.Provider.Aichat,
69
messages=[{"role": "user", "content": "Hello"}],
70
stream=True,
71
)
72
for message in response:
73
print(message)
74
```
75
76
77
##### Image Upload & Generation
78
79
Image upload and generation are supported by three main providers:
80
81
- **Bing & Other GPT-4 Providers:** Utilizes Microsoft's Image Creator.
82
- **Google Gemini:** Available for free accounts with IP addresses outside Europe.
83
- **OpenaiChat with GPT-4:** Accessible for users with a Plus subscription.
84
85
```python
86
import g4f
87
88
# Setting up the request for image creation
89
response = g4f.ChatCompletion.create(
90
model=g4f.models.default, # Using the default model
91
provider=g4f.Provider.Gemini, # Specifying the provider as Gemini
92
messages=[{"role": "user", "content": "Create an image like this"}],
93
image=open("images/g4f.png", "rb"), # Image input can be a data URI, bytes, PIL Image, or IO object
94
image_name="g4f.png" # Optional: specifying the filename
95
)
96
97
# Displaying the response
98
print(response)
99
100
from g4f.image import ImageResponse
101
102
# Get image links from response
103
for chunk in g4f.ChatCompletion.create(
104
model=g4f.models.default, # Using the default model
105
provider=g4f.Provider.OpenaiChat, # Specifying the provider as OpenaiChat
106
messages=[{"role": "user", "content": "Create images with dogs"}],
107
access_token="...", # Need a access token from a plus user
108
stream=True,
109
ignore_stream=True
110
):
111
if isinstance(chunk, ImageResponse):
112
print(chunk.images) # Print generated image links
113
print(chunk.alt) # Print used prompt for image generation
114
```
115
116
##### Using Browser
117
118
Some providers using a browser to bypass the bot protection. They using the selenium webdriver to control the browser. The browser settings and the login data are saved in a custom directory. If the headless mode is enabled, the browser windows are loaded invisibly. For performance reasons, it is recommended to reuse the browser instances and close them yourself at the end:
119
120
```python
121
import g4f
122
from undetected_chromedriver import Chrome, ChromeOptions
123
from g4f.Provider import (
124
Bard,
125
Poe,
126
AItianhuSpace,
127
MyShell,
128
PerplexityAi,
129
)
130
131
options = ChromeOptions()
132
options.add_argument("--incognito");
133
webdriver = Chrome(options=options, headless=True)
134
for idx in range(10):
135
response = g4f.ChatCompletion.create(
136
model=g4f.models.default,
137
provider=g4f.Provider.MyShell,
138
messages=[{"role": "user", "content": "Suggest me a name."}],
139
webdriver=webdriver
140
)
141
print(f"{idx}:", response)
142
webdriver.quit()
143
```
144
145
##### Async Support
146
147
To enhance speed and overall performance, execute providers asynchronously. The total execution time will be determined by the duration of the slowest provider's execution.
148
149
```python
150
import g4f
151
import asyncio
152
153
_providers = [
154
g4f.Provider.Aichat,
155
g4f.Provider.ChatBase,
156
g4f.Provider.Bing,
157
g4f.Provider.GptGo,
158
g4f.Provider.You,
159
g4f.Provider.Yqcloud,
160
]
161
162
async def run_provider(provider: g4f.Provider.BaseProvider):
163
try:
164
response = await g4f.ChatCompletion.create_async(
165
model=g4f.models.default,
166
messages=[{"role": "user", "content": "Hello"}],
167
provider=provider,
168
)
169
print(f"{provider.__name__}:", response)
170
except Exception as e:
171
print(f"{provider.__name__}:", e)
172
173
async def run_all():
174
calls = [
175
run_provider(provider) for provider in _providers
176
]
177
await asyncio.gather(*calls)
178
179
asyncio.run(run_all())
180
```
181
182
[Return to Home](/)
@@ -0,0 +1,46 @@
1
### G4F - Additional Requirements
2
3
#### Introduction
4
5
You can install requirements partially or completely. So G4F can be used as you wish. You have the following options for this:
6
7
#### Options
8
9
Install required packages for the OpenaiChat provider:
10
```
11
pip install -U g4f[openai]
12
```
13
Install required packages for the interference api:
14
```
15
pip install -U g4f[api]
16
```
17
Install required packages for the web interface:
18
```
19
pip install -U g4f[gui]
20
```
21
Install required packages for uploading / generating images:
22
```
23
pip install -U g4f[image]
24
```
25
Install required packages for using the webdriver:
26
```
27
pip install -U g4f[webdriver]
28
```
29
Install required package for proxy support with aiohttp:
30
```
31
pip install -U aiohttp_socks
32
```
33
Install required package for loading cookies from browser:
34
```
35
pip install browser_cookie3
36
```
37
Install curl_cffi for better protection from being blocked:
38
```
39
pip install curl_cffi
40
```
41
Install all packages and uninstall this package for disabling the webdriver:
42
```
43
pip uninstall undetected-chromedriver
44
```
45
46
[Return to Home](/)