From 1b4a86a857d9f9b77e0c7f0cd87468b627a250c8 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Thu, 22 Feb 2024 04:35:11 +0100 Subject: Update client docs, Enable some tests --- docs/client.md | 23 +++++++++++++++++------ etc/unittest/backend.py | 32 +++++++++++++++++--------------- g4f/gui/server/backend.py | 4 ++-- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/docs/client.md b/docs/client.md index f2ba9bcd..6cc08ac3 100644 --- a/docs/client.md +++ b/docs/client.md @@ -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", + ... ) ``` diff --git a/etc/unittest/backend.py b/etc/unittest/backend.py index 846c3554..c4ab219e 100644 --- a/etc/unittest/backend.py +++ b/etc/unittest/backend.py @@ -1,15 +1,15 @@ 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 - + class TestBackendApi(unittest.TestCase): def setUp(self): @@ -18,25 +18,27 @@ 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() self.assertIsInstance(response, list) self.assertTrue(len(response) > 0) - + def test_get_providers(self): response = self.api.get_providers() 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): def setUp(self): @@ -48,6 +50,6 @@ class TestUtilityFunctions(unittest.TestCase): exception = Exception("Message") result = get_error_message(exception) self.assertEqual("ProviderMock: Exception: Message", result) - + if __name__ == '__main__': unittest.main() \ No newline at end of file diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index 9788e5f5..454ed1c6 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -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') -- cgit v1.2.3