Improve readme / unittests

This commit is contained in:
Heiner Lohaus 2024-01-29 20:13:54 +01:00
parent a28bab9387
commit 770bdc54fc
3 changed files with 42 additions and 27 deletions

View File

@ -103,26 +103,30 @@ Install all supported tools / all used packages:
```
pip install -U g4f[all]
```
Install packages for uploading / generating images:
```
pip install -U g4f[image]
```
Install the packages required for providers with webdriver:
```
pip install -U g4f[webdriver]
```
Install the packages required for the OpenaiChat provider:
Install required packages for the OpenaiChat provider:
```
pip install -U g4f[openai]
```
Install the packages required for the interference api:
Install required packages for the interference api:
```
pip install -U g4f[api]
```
Install the packages required for the web gui:
Install required packages for the web interface:
```
pip install -U g4f[gui]
```
Install required packages for uploading / generating images:
```
pip install -U g4f[image]
```
Install required packages for providers with webdriver:
```
pip install -U g4f[webdriver]
```
Install required packages for proxy support:
```
pip install -U aiohttp_socks
```
##### or:

View File

@ -1,4 +1,3 @@
from .include import DEFAULT_MESSAGES
import asyncio
try:
import nest_asyncio
@ -6,55 +5,60 @@ try:
except:
has_nest_asyncio = False
import unittest
import g4f
from g4f import ChatCompletion
from .mocks import ProviderMock, AsyncProviderMock, AsyncGeneratorProviderMock
DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
class TestChatCompletion(unittest.TestCase):
async def run_exception(self):
return ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
def test_exception(self):
if hasattr(asyncio, '_nest_patched'):
self.skipTest('asyncio is already patched')
self.assertRaises(g4f.errors.NestAsyncioError, asyncio.run, self.run_exception())
def test_create(self):
result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
self.assertEqual("Mock",result)
def test_create_generator(self):
result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncGeneratorProviderMock)
self.assertEqual("Mock",result)
class TestChatCompletionAsync(unittest.IsolatedAsyncioTestCase):
async def test_base(self):
result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
self.assertEqual("Mock",result)
async def test_async(self):
result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
self.assertEqual("Mock",result)
async def test_create_generator(self):
result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, AsyncGeneratorProviderMock)
self.assertEqual("Mock",result)
class TestChatCompletionNestAsync(unittest.IsolatedAsyncioTestCase):
def setUp(self) -> None:
if not has_nest_asyncio:
self.skipTest('"nest_asyncio" not installed')
nest_asyncio.apply()
async def test_create(self):
result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
self.assertEqual("Mock",result)
async def test_nested(self):
result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
self.assertEqual("Mock",result)
async def test_nested_generator(self):
result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncGeneratorProviderMock)
self.assertEqual("Mock",result)

View File

@ -24,12 +24,19 @@ class TestGetLastProvider(unittest.TestCase):
def test_get_last_provider(self):
ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
self.assertEqual(get_last_provider(), ProviderMock)
def test_get_last_provider_retry(self):
ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, RetryProvider([ProviderMock]))
self.assertEqual(get_last_provider(), ProviderMock)
def test_get_last_provider_async(self):
coroutine = ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
asyncio.run(coroutine)
self.assertEqual(get_last_provider(), ProviderMock)
self.assertEqual(get_last_provider(), ProviderMock)
def test_get_last_provider_as_dict(self):
ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
last_provider_dict = get_last_provider(True)
self.assertIsInstance(last_provider_dict, dict)
self.assertIn('name', last_provider_dict)
self.assertEqual(ProviderMock.__name__, last_provider_dict['name'])