返回提交历史
Modified
docs/client.md
+17
-6
Modified
etc/unittest/backend.py
+17
-15
Modified
g4f/gui/server/backend.py
+2
-2
XFEstudio/gpt4free
Update client docs, Enable some tests
1b4a86a8
代码差异
3 个文件
+36
-23
@@ -2,13 +2,13 @@
2
2
3
3
#### Introduction
4
4
5
The G4F Client API introduces a new way to integrate advanced AI functionalities into your Python applications. This guide will help you transition from using the OpenAI client to the new G4F Client, offering compatibility with the existing OpenAI API alongside additional features.
5
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.
6
6
7
7
#### Getting Started
8
8
9
9
**Switching to G4F Client:**
10
10
11
Replace the OpenAI client import statement in your Python code as follows:
11
To begin using the G4F Client, simply update your import statement in your Python code:
12
12
13
13
Old Import:
14
14
```python
@@ -20,11 +20,11 @@ New Import:
20
20
from g4f.client import Client as OpenAI
21
21
```
22
22
23
The G4F Client maintains the same API interface as OpenAI, ensuring a seamless transition.
23
The G4F Client preserves the same familiar API interface as OpenAI, ensuring a smooth transition process.
24
24
25
#### Initializing the Client
25
### Initializing the Client
26
26
27
To use the G4F Client, create an instance with customized providers:
27
To utilize the G4F Client, create an new instance. Below is an example showcasing custom providers:
28
28
29
29
```python
30
30
from g4f.client import Client
@@ -33,7 +33,18 @@ from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
33
33
client = Client(
34
34
provider=OpenaiChat,
35
35
image_provider=Gemini,
36
proxies=None
36
...
37
)
38
```
39
40
You also have the option to define a proxy in the client for all outgoing requests:
41
42
```python
43
from g4f.client import Client
44
45
client = Client(
46
proxies="http://user:pass@host",
47
...
37
48
)
38
49
```
39
50
@@ -1,15 +1,15 @@
1
1
import unittest
2
# import asyncio
2
import asyncio
3
3
from unittest.mock import MagicMock
4
4
from .mocks import ProviderMock
5
5
import g4f
6
6
7
try:
7
8
from g4f.gui.server.backend import Backend_Api, get_error_message
8
# from g4f.gui.server.internet import search
9
9
has_requirements = True
10
10
except:
11
11
has_requirements = False
12
12
13
13
class TestBackendApi(unittest.TestCase):
14
14
15
15
def setUp(self):
@@ -18,25 +18,27 @@ class TestBackendApi(unittest.TestCase):
18
18
self.app = MagicMock()
19
19
self.api = Backend_Api(self.app)
20
20
21
# def test_version(self):
22
# response = self.api.get_version()
23
# self.assertIn("version", response)
24
# self.assertIn("latest_version", response)
25
21
def test_version(self):
22
response = self.api.get_version()
23
self.assertIn("version", response)
24
self.assertIn("latest_version", response)
25
26
26
def test_get_models(self):
27
27
response = self.api.get_models()
28
28
self.assertIsInstance(response, list)
29
29
self.assertTrue(len(response) > 0)
30
30
31
31
def test_get_providers(self):
32
32
response = self.api.get_providers()
33
33
self.assertIsInstance(response, list)
34
34
self.assertTrue(len(response) > 0)
35
36
# def test_search(self):
37
# result = asyncio.run(search("Hello"))
38
# self.assertEqual(5, len(result))
39
35
36
def test_search(self):
37
# Task was destroyed but it is pending!
38
from g4f.gui.server.internet import search
39
result = asyncio.run(search("Hello"))
40
self.assertEqual(5, len(result))
41
40
42
class TestUtilityFunctions(unittest.TestCase):
41
43
42
44
def setUp(self):
@@ -48,6 +50,6 @@ class TestUtilityFunctions(unittest.TestCase):
48
50
exception = Exception("Message")
49
51
result = get_error_message(exception)
50
52
self.assertEqual("ProviderMock: Exception: Message", result)
51
53
52
54
if __name__ == '__main__':
53
55
unittest.main()
@@ -8,8 +8,6 @@ from g4f.image import is_allowed_extension, to_image
8
8
from g4f.errors import VersionNotFoundError
9
9
from g4f.Provider import __providers__
10
10
from g4f.Provider.bing.create_images import patch_provider
11
from .internet import get_search_message
12
13
11
14
12
class Backend_Api:
15
13
"""
@@ -157,6 +155,8 @@ class Backend_Api:
157
155
if provider == "Bing":
158
156
kwargs['web_search'] = True
159
157
else:
158
# ResourceWarning: unclosed event loop
159
from .internet import get_search_message
160
160
messages[-1]["content"] = get_search_message(messages[-1]["content"])
161
161
162
162
model = json_data.get('model')