diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-04-12 18:01:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-12 18:01:54 +0200 |
commit | 0b712c2bde77fe7e3e347d71d5de3c9e62f26738 (patch) | |
tree | bd93b8b64705c74be6feff9cc2f7cba7837c8da8 /etc/unittest | |
parent | Merge pull request #1825 from hlohaus/ws (diff) | |
download | gpt4free-0b712c2bde77fe7e3e347d71d5de3c9e62f26738.tar gpt4free-0b712c2bde77fe7e3e347d71d5de3c9e62f26738.tar.gz gpt4free-0b712c2bde77fe7e3e347d71d5de3c9e62f26738.tar.bz2 gpt4free-0b712c2bde77fe7e3e347d71d5de3c9e62f26738.tar.lz gpt4free-0b712c2bde77fe7e3e347d71d5de3c9e62f26738.tar.xz gpt4free-0b712c2bde77fe7e3e347d71d5de3c9e62f26738.tar.zst gpt4free-0b712c2bde77fe7e3e347d71d5de3c9e62f26738.zip |
Diffstat (limited to 'etc/unittest')
-rw-r--r-- | etc/unittest/__main__.py | 1 | ||||
-rw-r--r-- | etc/unittest/async_client.py | 56 | ||||
-rw-r--r-- | etc/unittest/integration.py | 10 |
3 files changed, 66 insertions, 1 deletions
diff --git a/etc/unittest/__main__.py b/etc/unittest/__main__.py index 3a459dba..351c2bb3 100644 --- a/etc/unittest/__main__.py +++ b/etc/unittest/__main__.py @@ -4,6 +4,7 @@ from .backend import * from .main import * from .model import * from .client import * +from .async_client import * from .include import * from .integration import * diff --git a/etc/unittest/async_client.py b/etc/unittest/async_client.py new file mode 100644 index 00000000..a49b90ed --- /dev/null +++ b/etc/unittest/async_client.py @@ -0,0 +1,56 @@ +import unittest + +from g4f.client import AsyncClient, ChatCompletion, ChatCompletionChunk +from .mocks import AsyncGeneratorProviderMock, ModelProviderMock, YieldProviderMock + +DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}] + +class AsyncTestPassModel(unittest.IsolatedAsyncioTestCase): + + async def test_response(self): + client = AsyncClient(provider=AsyncGeneratorProviderMock) + response = await client.chat.completions.create(DEFAULT_MESSAGES, "") + self.assertIsInstance(response, ChatCompletion) + self.assertEqual("Mock", response.choices[0].message.content) + + async def test_pass_model(self): + client = AsyncClient(provider=ModelProviderMock) + response = await client.chat.completions.create(DEFAULT_MESSAGES, "Hello") + self.assertIsInstance(response, ChatCompletion) + self.assertEqual("Hello", response.choices[0].message.content) + + async def test_max_tokens(self): + client = AsyncClient(provider=YieldProviderMock) + messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]] + response = await client.chat.completions.create(messages, "Hello", max_tokens=1) + self.assertIsInstance(response, ChatCompletion) + self.assertEqual("How ", response.choices[0].message.content) + response = await client.chat.completions.create(messages, "Hello", max_tokens=2) + self.assertIsInstance(response, ChatCompletion) + self.assertEqual("How are ", response.choices[0].message.content) + + async def test_max_stream(self): + client = AsyncClient(provider=YieldProviderMock) + messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]] + response = client.chat.completions.create(messages, "Hello", stream=True) + async for chunk in response: + self.assertIsInstance(chunk, ChatCompletionChunk) + if chunk.choices[0].delta.content is not None: + self.assertIsInstance(chunk.choices[0].delta.content, str) + messages = [{'role': 'user', 'content': chunk} for chunk in ["You ", "You ", "Other", "?"]] + response = client.chat.completions.create(messages, "Hello", stream=True, max_tokens=2) + response = [chunk async for chunk in response] + self.assertEqual(len(response), 3) + for chunk in response: + if chunk.choices[0].delta.content is not None: + self.assertEqual(chunk.choices[0].delta.content, "You ") + + async def test_stop(self): + client = AsyncClient(provider=YieldProviderMock) + messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]] + response = await client.chat.completions.create(messages, "Hello", stop=["and"]) + self.assertIsInstance(response, ChatCompletion) + self.assertEqual("How are you?", response.choices[0].message.content) + +if __name__ == '__main__': + unittest.main()
\ No newline at end of file diff --git a/etc/unittest/integration.py b/etc/unittest/integration.py index f69d4770..d87a7f6b 100644 --- a/etc/unittest/integration.py +++ b/etc/unittest/integration.py @@ -8,7 +8,7 @@ except ImportError: has_nest_asyncio = False from g4f.client import Client, ChatCompletion -from g4f.Provider import Bing, OpenaiChat +from g4f.Provider import Bing, OpenaiChat, DuckDuckGo DEFAULT_MESSAGES = [{"role": "system", "content": 'Response in json, Example: {"success: true"}'}, {"role": "user", "content": "Say success true in json"}] @@ -19,11 +19,19 @@ class TestProviderIntegration(unittest.TestCase): self.skipTest("nest_asyncio is not installed") def test_bing(self): + self.skipTest("Not stable") 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_duckduckgo(self): + self.skipTest("Not working") + client = Client(provider=DuckDuckGo) + 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"}) |