main.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import unittest
  2. import asyncio
  3. import g4f
  4. from g4f import ChatCompletion, get_last_provider
  5. from g4f.Provider import RetryProvider
  6. from .mocks import ProviderMock
  7. DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
  8. class NoTestChatCompletion(unittest.TestCase):
  9. def no_test_create_default(self):
  10. result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES)
  11. if "Good" not in result and "Hi" not in result:
  12. self.assertIn("Hello", result)
  13. def no_test_bing_provider(self):
  14. provider = g4f.Provider.Bing
  15. result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, provider)
  16. self.assertIn("Bing", result)
  17. class TestGetLastProvider(unittest.TestCase):
  18. def test_get_last_provider(self):
  19. ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
  20. self.assertEqual(get_last_provider(), ProviderMock)
  21. def test_get_last_provider_retry(self):
  22. ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, RetryProvider([ProviderMock]))
  23. self.assertEqual(get_last_provider(), ProviderMock)
  24. def test_get_last_provider_async(self):
  25. coroutine = ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
  26. asyncio.run(coroutine)
  27. self.assertEqual(get_last_provider(), ProviderMock)
  28. def test_get_last_provider_as_dict(self):
  29. ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
  30. last_provider_dict = get_last_provider(True)
  31. self.assertIsInstance(last_provider_dict, dict)
  32. self.assertIn('name', last_provider_dict)
  33. self.assertEqual(ProviderMock.__name__, last_provider_dict['name'])