返回提交历史
Modified
docs/async_client.md
+1
-3
Modified
docs/client.md
+2
-5
Modified
docs/interference-api.md
+1
-1
Modified
g4f/client/client.py
+5
-26
XFEstudio/gpt4free
Update (docs/async_client.md docs/client.md docs/interference-api.md g4f/client/client.py)
e79c8b01
代码差异
4 个文件
+9
-35
@@ -3,7 +3,7 @@ The G4F async client API is a powerful asynchronous interface for interacting wi
3
3
4
4
5
5
## Compatibility Note
6
The G4F async client API is designed to be compatible with the OpenAI and Anthropic API, making it easy for developers familiar with OpenAI's or Anthropic's interface to transition to G4F.
6
The G4F async client API is designed to be compatible with the OpenAI API, making it easy for developers familiar with OpenAI's interface to transition to G4F.
7
7
8
8
## Table of Contents
9
9
- [Introduction](#introduction)
@@ -57,7 +57,6 @@ client = Client(
57
57
**Here’s an improved example of creating chat completions:**
58
58
```python
59
59
response = await async_client.chat.completions.create(
60
system="You are a helpful assistant.",
61
60
model="gpt-3.5-turbo",
62
61
messages=[
63
62
{
@@ -70,7 +69,6 @@ response = await async_client.chat.completions.create(
70
69
```
71
70
72
71
**This example:**
73
- Sets a system message to define the assistant's role
74
72
- Asks a specific question `Say this is a test`
75
73
- Configures various parameters like temperature and max_tokens for more control over the output
76
74
- Disables streaming for a complete response
@@ -23,7 +23,7 @@
23
23
24
24
25
25
## Introduction
26
Welcome to the G4F Client API, a cutting-edge tool for seamlessly integrating advanced AI capabilities into your Python applications. This guide is designed to facilitate your transition from using the OpenAI or Anthropic client to the G4F Client, offering enhanced features while maintaining compatibility with the existing OpenAI and Anthropic API.
26
Welcome to the G4F Client API, a cutting-edge tool for seamlessly integrating advanced AI capabilities into your Python applications. This guide is designed to facilitate your transition from using the OpenAI client to the G4F Client, offering enhanced features while maintaining compatibility with the existing OpenAI API.
27
27
28
28
## Getting Started
29
29
### Switching to G4F Client
@@ -43,7 +43,7 @@ from g4f.client import Client as OpenAI
43
43
44
44
45
45
46
The G4F Client preserves the same familiar API interface as OpenAI or Anthropic, ensuring a smooth transition process.
46
The G4F Client preserves the same familiar API interface as OpenAI, ensuring a smooth transition process.
47
47
48
48
## Initializing the Client
49
49
To utilize the G4F Client, create a new instance. **Below is an example showcasing custom providers:**
@@ -62,7 +62,6 @@ client = Client(
62
62
**Here’s an improved example of creating chat completions:**
63
63
```python
64
64
response = client.chat.completions.create(
65
system="You are a helpful assistant.",
66
65
model="gpt-3.5-turbo",
67
66
messages=[
68
67
{
@@ -75,7 +74,6 @@ response = client.chat.completions.create(
75
74
```
76
75
77
76
**This example:**
78
- Sets a system message to define the assistant's role
79
77
- Asks a specific question `Say this is a test`
80
78
- Configures various parameters like temperature and max_tokens for more control over the output
81
79
- Disables streaming for a complete response
@@ -313,7 +311,6 @@ while True:
313
311
try:
314
312
# Get GPT's response
315
313
response = client.chat.completions.create(
316
system="You are a helpful assistant.",
317
314
messages=messages,
318
315
model=g4f.models.default,
319
316
)
@@ -15,7 +15,7 @@
15
15
16
16
17
17
## Introduction
18
The G4F Interference API is a powerful tool that allows you to serve other OpenAI integrations using G4F (Gpt4free). It acts as a proxy, translating requests intended for the OpenAI and Anthropic API into requests compatible with G4F providers. This guide will walk you through the process of setting up, running, and using the Interference API effectively.
18
The G4F Interference API is a powerful tool that allows you to serve other OpenAI integrations using G4F (Gpt4free). It acts as a proxy, translating requests intended for the OpenAI API into requests compatible with G4F providers. This guide will walk you through the process of setting up, running, and using the Interference API effectively.
19
19
20
20
21
21
## Running the Interference API
@@ -149,7 +149,6 @@ class Completions:
149
149
self,
150
150
messages: Messages,
151
151
model: str,
152
system: str = None, # Added system parameter
153
152
provider: ProviderType = None,
154
153
stream: bool = False,
155
154
proxy: str = None,
@@ -162,12 +161,6 @@ class Completions:
162
161
ignore_stream: bool = False,
163
162
**kwargs
164
163
) -> Union[ChatCompletion, Iterator[ChatCompletionChunk]]:
165
# If a system prompt is provided, prepend it to the messages
166
if system:
167
system_message = {"role": "system", "content": system}
168
messages = [system_message] + messages
169
170
# Existing implementation continues...
171
164
model, provider = get_model_and_provider(
172
165
model,
173
166
self.provider if provider is None else provider,
@@ -228,7 +221,6 @@ class Completions:
228
221
self,
229
222
messages: Messages,
230
223
model: str,
231
system: str = None, # Added system parameter
232
224
provider: ProviderType = None,
233
225
stream: bool = False,
234
226
proxy: str = None,
@@ -241,12 +233,6 @@ class Completions:
241
233
ignore_stream: bool = False,
242
234
**kwargs
243
235
) -> Union[ChatCompletion, AsyncIterator[ChatCompletionChunk]]:
244
# If a system prompt is provided, prepend it to the messages
245
if system:
246
system_message = {"role": "system", "content": system}
247
messages = [system_message] + messages
248
249
# Existing implementation continues...
250
236
model, provider = get_model_and_provider(
251
237
model,
252
238
self.provider if provider is None else provider,
@@ -285,18 +271,16 @@ class Completions:
285
271
**kwargs
286
272
)
287
273
288
# Handle streaming or non-streaming responses
274
# Removed 'await' here since 'async_iter_response' returns an async generator
275
response = async_iter_response(response, stream, response_format, max_tokens, stop)
276
response = async_iter_append_model_and_provider(response)
277
289
278
if stream:
290
response = async_iter_response(response, stream, response_format, max_tokens, stop)
291
response = async_iter_append_model_and_provider(response)
292
279
return response
293
280
else:
294
response = async_iter_response(response, stream, response_format, max_tokens, stop)
295
response = async_iter_append_model_and_provider(response)
296
281
async for result in response:
297
282
return result
298
283
299
300
284
class Chat:
301
285
completions: Completions
302
286
@@ -417,12 +401,6 @@ class Image:
417
401
def __repr__(self):
418
402
return f"Image(url={self.url}, b64_json={'<base64 data>' if self.b64_json else None})"
419
403
420
def to_dict(self):
421
return {
422
"url": self.url,
423
"b64_json": self.b64_json
424
}
425
426
404
class ImagesResponse:
427
405
def __init__(self, data: list[Image]):
428
406
self.data = data
@@ -530,3 +508,4 @@ class Images:
530
508
async def create_variation(self, image: Union[str, bytes], model: str = None, response_format: str = "url", **kwargs):
531
509
# Existing implementation, adjust if you want to support b64_json here as well
532
510
pass
511