2024-01-14 17:04:37 +03:00
|
|
|
import unittest
|
2024-01-21 04:20:23 +03:00
|
|
|
import asyncio
|
2024-01-14 17:04:37 +03:00
|
|
|
import g4f
|
|
|
|
from g4f import ChatCompletion, get_last_provider
|
2024-01-21 04:20:23 +03:00
|
|
|
from g4f.Provider import RetryProvider
|
|
|
|
from .mocks import ProviderMock
|
2024-01-14 17:04:37 +03:00
|
|
|
|
2024-01-23 14:17:16 +03:00
|
|
|
DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
|
|
|
|
|
2024-01-21 19:31:57 +03:00
|
|
|
class NoTestChatCompletion(unittest.TestCase):
|
2024-01-14 17:04:37 +03:00
|
|
|
|
2024-01-21 19:31:57 +03:00
|
|
|
def no_test_create_default(self):
|
2024-01-21 04:20:23 +03:00
|
|
|
result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES)
|
2024-01-20 20:16:45 +03:00
|
|
|
if "Good" not in result and "Hi" not in result:
|
|
|
|
self.assertIn("Hello", result)
|
2024-01-21 04:20:23 +03:00
|
|
|
|
2024-01-21 19:31:57 +03:00
|
|
|
def no_test_bing_provider(self):
|
2024-01-14 17:04:37 +03:00
|
|
|
provider = g4f.Provider.Bing
|
2024-01-21 04:20:23 +03:00
|
|
|
result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, provider)
|
2024-01-20 20:16:45 +03:00
|
|
|
self.assertIn("Bing", result)
|
2024-01-14 17:04:37 +03:00
|
|
|
|
2024-01-21 04:20:23 +03:00
|
|
|
class TestGetLastProvider(unittest.TestCase):
|
2024-01-14 17:04:37 +03:00
|
|
|
|
2024-01-21 04:20:23 +03:00
|
|
|
def test_get_last_provider(self):
|
|
|
|
ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
|
|
|
|
self.assertEqual(get_last_provider(), ProviderMock)
|
2024-01-29 22:13:54 +03:00
|
|
|
|
2024-01-21 04:20:23 +03:00
|
|
|
def test_get_last_provider_retry(self):
|
|
|
|
ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, RetryProvider([ProviderMock]))
|
|
|
|
self.assertEqual(get_last_provider(), ProviderMock)
|
2024-01-29 22:13:54 +03:00
|
|
|
|
2024-01-21 04:20:23 +03:00
|
|
|
def test_get_last_provider_async(self):
|
|
|
|
coroutine = ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
|
|
|
|
asyncio.run(coroutine)
|
2024-01-29 22:13:54 +03:00
|
|
|
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'])
|