Update client docs, Enable some tests

This commit is contained in:
Heiner Lohaus 2024-02-22 04:35:11 +01:00
parent e5b7f72b71
commit 1b4a86a857
3 changed files with 36 additions and 23 deletions

View File

@ -2,13 +2,13 @@
#### Introduction
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.
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.
#### Getting Started
**Switching to G4F Client:**
Replace the OpenAI client import statement in your Python code as follows:
To begin using the G4F Client, simply update your import statement in your Python code:
Old Import:
```python
@ -20,11 +20,11 @@ New Import:
from g4f.client import Client as OpenAI
```
The G4F Client maintains the same API interface as OpenAI, ensuring a seamless transition.
The G4F Client preserves the same familiar API interface as OpenAI, ensuring a smooth transition process.
#### Initializing the Client
### Initializing the Client
To use the G4F Client, create an instance with customized providers:
To utilize the G4F Client, create an new instance. Below is an example showcasing custom providers:
```python
from g4f.client import Client
@ -33,7 +33,18 @@ from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
client = Client(
provider=OpenaiChat,
image_provider=Gemini,
proxies=None
...
)
```
You also have the option to define a proxy in the client for all outgoing requests:
```python
from g4f.client import Client
client = Client(
proxies="http://user:pass@host",
...
)
```

View File

@ -1,11 +1,11 @@
import unittest
# import asyncio
import asyncio
from unittest.mock import MagicMock
from .mocks import ProviderMock
import g4f
try:
from g4f.gui.server.backend import Backend_Api, get_error_message
# from g4f.gui.server.internet import search
has_requirements = True
except:
has_requirements = False
@ -18,10 +18,10 @@ class TestBackendApi(unittest.TestCase):
self.app = MagicMock()
self.api = Backend_Api(self.app)
# def test_version(self):
# response = self.api.get_version()
# self.assertIn("version", response)
# self.assertIn("latest_version", response)
def test_version(self):
response = self.api.get_version()
self.assertIn("version", response)
self.assertIn("latest_version", response)
def test_get_models(self):
response = self.api.get_models()
@ -33,9 +33,11 @@ class TestBackendApi(unittest.TestCase):
self.assertIsInstance(response, list)
self.assertTrue(len(response) > 0)
# def test_search(self):
# result = asyncio.run(search("Hello"))
# self.assertEqual(5, len(result))
def test_search(self):
# Task was destroyed but it is pending!
from g4f.gui.server.internet import search
result = asyncio.run(search("Hello"))
self.assertEqual(5, len(result))
class TestUtilityFunctions(unittest.TestCase):

View File

@ -8,8 +8,6 @@ from g4f.image import is_allowed_extension, to_image
from g4f.errors import VersionNotFoundError
from g4f.Provider import __providers__
from g4f.Provider.bing.create_images import patch_provider
from .internet import get_search_message
class Backend_Api:
"""
@ -157,6 +155,8 @@ class Backend_Api:
if provider == "Bing":
kwargs['web_search'] = True
else:
# ResourceWarning: unclosed event loop
from .internet import get_search_message
messages[-1]["content"] = get_search_message(messages[-1]["content"])
model = json_data.get('model')