2024-02-14 08:21:57 +00:00
|
|
|
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)
|
2024-02-23 01:51:10 +00:00
|
|
|
if chunk.choices[0].delta.content is not None:
|
|
|
|
self.assertIsInstance(chunk.choices[0].delta.content, str)
|
2024-02-14 08:21:57 +00:00
|
|
|
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)
|
2024-02-23 01:51:10 +00:00
|
|
|
self.assertEqual(len(response), 3)
|
2024-02-14 08:21:57 +00:00
|
|
|
for chunk in response:
|
2024-02-23 01:51:10 +00:00
|
|
|
if chunk.choices[0].delta.content is not None:
|
|
|
|
self.assertEqual(chunk.choices[0].delta.content, "You ")
|
2024-02-14 08:21:57 +00:00
|
|
|
|
2024-02-21 23:16:58 +00:00
|
|
|
def test_stop(self):
|
2024-02-14 08:21:57 +00:00
|
|
|
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()
|