integration.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import unittest
  2. import json
  3. from g4f.client import Client, AsyncClient, ChatCompletion
  4. from g4f.Provider import Copilot, DDG
  5. DEFAULT_MESSAGES = [{"role": "system", "content": 'Response in json, Example: {"success": false}'},
  6. {"role": "user", "content": "Say success true in json"}]
  7. class TestProviderIntegration(unittest.TestCase):
  8. def test_bing(self):
  9. client = Client(provider=Copilot)
  10. response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
  11. self.assertIsInstance(response, ChatCompletion)
  12. self.assertIn("success", json.loads(response.choices[0].message.content))
  13. def test_openai(self):
  14. client = Client(provider=DDG)
  15. response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
  16. self.assertIsInstance(response, ChatCompletion)
  17. self.assertIn("success", json.loads(response.choices[0].message.content))
  18. class TestChatCompletionAsync(unittest.IsolatedAsyncioTestCase):
  19. async def test_bing(self):
  20. client = AsyncClient(provider=Copilot)
  21. response = await client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
  22. self.assertIsInstance(response, ChatCompletion)
  23. self.assertIn("success", json.loads(response.choices[0].message.content))
  24. async def test_openai(self):
  25. client = AsyncClient(provider=DDG)
  26. response = await client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
  27. self.assertIsInstance(response, ChatCompletion)
  28. self.assertIn("success", json.loads(response.choices[0].message.content))
  29. if __name__ == '__main__':
  30. unittest.main()