diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-02-14 09:22:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-14 09:22:32 +0100 |
commit | 51686409f86e187d9c13cbea9388f31f567a118c (patch) | |
tree | c4291ff5375b77d70622cfa25490f5a02a5ee970 /etc/unittest/client.py | |
parent | Update client.md (diff) | |
parent | Add unitests for the client (diff) | |
download | gpt4free-51686409f86e187d9c13cbea9388f31f567a118c.tar gpt4free-51686409f86e187d9c13cbea9388f31f567a118c.tar.gz gpt4free-51686409f86e187d9c13cbea9388f31f567a118c.tar.bz2 gpt4free-51686409f86e187d9c13cbea9388f31f567a118c.tar.lz gpt4free-51686409f86e187d9c13cbea9388f31f567a118c.tar.xz gpt4free-51686409f86e187d9c13cbea9388f31f567a118c.tar.zst gpt4free-51686409f86e187d9c13cbea9388f31f567a118c.zip |
Diffstat (limited to 'etc/unittest/client.py')
-rw-r--r-- | etc/unittest/client.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/etc/unittest/client.py b/etc/unittest/client.py new file mode 100644 index 00000000..c63edbd2 --- /dev/null +++ b/etc/unittest/client.py @@ -0,0 +1,54 @@ +import unittest + +from g4f.client import Client, ChatCompletion, ChatCompletionChunk +from .mocks import AsyncGeneratorProviderMock, ModelProviderMock, YieldProviderMock + +DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}] + +class TestPassModel(unittest.TestCase): + + def test_response(self): + client = Client(provider=AsyncGeneratorProviderMock) + response = client.chat.completions.create(DEFAULT_MESSAGES, "") + self.assertIsInstance(response, ChatCompletion) + self.assertEqual("Mock", response.choices[0].message.content) + + def test_pass_model(self): + client = Client(provider=ModelProviderMock) + response = client.chat.completions.create(DEFAULT_MESSAGES, "Hello") + self.assertIsInstance(response, ChatCompletion) + self.assertEqual("Hello", response.choices[0].message.content) + + def test_max_tokens(self): + client = Client(provider=YieldProviderMock) + messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]] + response = client.chat.completions.create(messages, "Hello", max_tokens=1) + self.assertIsInstance(response, ChatCompletion) + self.assertEqual("How ", response.choices[0].message.content) + response = client.chat.completions.create(messages, "Hello", max_tokens=2) + self.assertIsInstance(response, ChatCompletion) + self.assertEqual("How are ", response.choices[0].message.content) + + def test_max_stream(self): + client = Client(provider=YieldProviderMock) + messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]] + response = client.chat.completions.create(messages, "Hello", stream=True) + for chunk in response: + self.assertIsInstance(chunk, ChatCompletionChunk) + 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 = list(response) + self.assertEqual(len(response), 2) + for chunk in response: + self.assertEqual(chunk.choices[0].delta.content, "You ") + + def no_test_stop(self): + client = Client(provider=YieldProviderMock) + messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]] + response = 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 |