integration.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import unittest
  2. import json
  3. try:
  4. import nest_asyncio
  5. has_nest_asyncio = True
  6. except ImportError:
  7. has_nest_asyncio = False
  8. from g4f.client import Client, ChatCompletion
  9. from g4f.Provider import Bing, OpenaiChat
  10. DEFAULT_MESSAGES = [{"role": "system", "content": 'Response in json, Example: {"success": false}'},
  11. {"role": "user", "content": "Say success true in json"}]
  12. class TestProviderIntegration(unittest.TestCase):
  13. def setUp(self):
  14. if not has_nest_asyncio:
  15. self.skipTest("nest_asyncio is not installed")
  16. def test_bing(self):
  17. self.skipTest("Not working")
  18. client = Client(provider=Bing)
  19. response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
  20. self.assertIsInstance(response, ChatCompletion)
  21. self.assertIn("success", json.loads(response.choices[0].message.content))
  22. def test_openai(self):
  23. self.skipTest("not working in this network")
  24. client = Client(provider=OpenaiChat)
  25. response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
  26. self.assertIsInstance(response, ChatCompletion)
  27. self.assertIn("success", json.loads(response.choices[0].message.content))
  28. if __name__ == '__main__':
  29. unittest.main()