2024-01-20 20:20:23 -05:00
|
|
|
import asyncio
|
2024-01-26 01:54:13 -05:00
|
|
|
try:
|
|
|
|
import nest_asyncio
|
|
|
|
has_nest_asyncio = True
|
|
|
|
except:
|
|
|
|
has_nest_asyncio = False
|
2024-01-20 20:20:23 -05:00
|
|
|
import unittest
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
import g4f
|
|
|
|
from g4f import ChatCompletion
|
2024-02-14 03:21:57 -05:00
|
|
|
from g4f.client import Client
|
2024-01-20 20:20:23 -05:00
|
|
|
from .mocks import ProviderMock, AsyncProviderMock, AsyncGeneratorProviderMock
|
2024-01-29 14:13:54 -05:00
|
|
|
|
|
|
|
DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
|
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
class TestChatCompletion(unittest.TestCase):
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
async def run_exception(self):
|
|
|
|
return ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
def test_exception(self):
|
2024-04-05 19:18:45 -04:00
|
|
|
if has_nest_asyncio:
|
|
|
|
self.skipTest('has nest_asyncio')
|
2024-01-20 20:20:23 -05:00
|
|
|
self.assertRaises(g4f.errors.NestAsyncioError, asyncio.run, self.run_exception())
|
|
|
|
|
|
|
|
def test_create(self):
|
|
|
|
result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
|
2024-02-14 03:21:57 -05:00
|
|
|
self.assertEqual("Mock", result)
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
def test_create_generator(self):
|
|
|
|
result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncGeneratorProviderMock)
|
2024-02-14 03:21:57 -05:00
|
|
|
self.assertEqual("Mock", result)
|
|
|
|
|
|
|
|
def test_await_callback(self):
|
|
|
|
client = Client(provider=AsyncGeneratorProviderMock)
|
|
|
|
response = client.chat.completions.create(DEFAULT_MESSAGES, "", max_tokens=0)
|
|
|
|
self.assertEqual("Mock", response.choices[0].message.content)
|
2024-01-20 20:20:23 -05:00
|
|
|
|
|
|
|
class TestChatCompletionAsync(unittest.IsolatedAsyncioTestCase):
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
async def test_base(self):
|
|
|
|
result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
|
|
|
|
self.assertEqual("Mock",result)
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
async def test_async(self):
|
|
|
|
result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
|
|
|
|
self.assertEqual("Mock",result)
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
async def test_create_generator(self):
|
|
|
|
result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, AsyncGeneratorProviderMock)
|
|
|
|
self.assertEqual("Mock",result)
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
class TestChatCompletionNestAsync(unittest.IsolatedAsyncioTestCase):
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
def setUp(self) -> None:
|
2024-01-26 01:54:13 -05:00
|
|
|
if not has_nest_asyncio:
|
|
|
|
self.skipTest('"nest_asyncio" not installed')
|
2024-01-20 20:20:23 -05:00
|
|
|
nest_asyncio.apply()
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
async def test_create(self):
|
|
|
|
result = await ChatCompletion.create_async(g4f.models.default, DEFAULT_MESSAGES, ProviderMock)
|
|
|
|
self.assertEqual("Mock",result)
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
async def test_nested(self):
|
|
|
|
result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncProviderMock)
|
|
|
|
self.assertEqual("Mock",result)
|
2024-01-29 14:13:54 -05:00
|
|
|
|
2024-01-20 20:20:23 -05:00
|
|
|
async def test_nested_generator(self):
|
|
|
|
result = ChatCompletion.create(g4f.models.default, DEFAULT_MESSAGES, AsyncGeneratorProviderMock)
|
|
|
|
self.assertEqual("Mock",result)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|