2024-04-05 19:05:00 -04:00
|
|
|
import unittest
|
|
|
|
import json
|
|
|
|
|
2024-04-05 19:18:45 -04:00
|
|
|
try:
|
|
|
|
import nest_asyncio
|
|
|
|
has_nest_asyncio = True
|
2024-04-06 04:47:43 -04:00
|
|
|
except ImportError:
|
2024-04-05 19:18:45 -04:00
|
|
|
has_nest_asyncio = False
|
|
|
|
|
2024-04-05 19:05:00 -04:00
|
|
|
from g4f.client import Client, ChatCompletion
|
|
|
|
from g4f.Provider import Bing, OpenaiChat
|
|
|
|
|
2024-04-06 04:47:43 -04:00
|
|
|
DEFAULT_MESSAGES = [{"role": "system", "content": 'Response in json, Example: {"success: true"}'},
|
2024-04-05 19:05:00 -04:00
|
|
|
{"role": "user", "content": "Say success true in json"}]
|
|
|
|
|
|
|
|
class TestProviderIntegration(unittest.TestCase):
|
2024-04-05 19:18:45 -04:00
|
|
|
def setUp(self):
|
|
|
|
if not has_nest_asyncio:
|
|
|
|
self.skipTest("nest_asyncio is not installed")
|
2024-04-05 19:05:00 -04:00
|
|
|
|
|
|
|
def test_bing(self):
|
|
|
|
client = Client(provider=Bing)
|
|
|
|
response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
|
|
|
|
self.assertIsInstance(response, ChatCompletion)
|
|
|
|
self.assertIn("success", json.loads(response.choices[0].message.content))
|
|
|
|
|
|
|
|
def test_openai(self):
|
|
|
|
client = Client(provider=OpenaiChat)
|
|
|
|
response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"})
|
|
|
|
self.assertIsInstance(response, ChatCompletion)
|
|
|
|
self.assertIn("success", json.loads(response.choices[0].message.content))
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|