client.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import unittest
  2. from g4f.client import Client, ChatCompletion, ChatCompletionChunk
  3. from .mocks import AsyncGeneratorProviderMock, ModelProviderMock, YieldProviderMock
  4. DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
  5. class AsyncTestPassModel(unittest.IsolatedAsyncioTestCase):
  6. async def test_response(self):
  7. client = Client(provider=AsyncGeneratorProviderMock)
  8. response = await client.chat.completions.async_create(DEFAULT_MESSAGES, "")
  9. self.assertIsInstance(response, ChatCompletion)
  10. self.assertEqual("Mock", response.choices[0].message.content)
  11. async def test_pass_model(self):
  12. client = Client(provider=ModelProviderMock)
  13. response = await client.chat.completions.async_create(DEFAULT_MESSAGES, "Hello")
  14. self.assertIsInstance(response, ChatCompletion)
  15. self.assertEqual("Hello", response.choices[0].message.content)
  16. async def test_max_tokens(self):
  17. client = Client(provider=YieldProviderMock)
  18. messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
  19. response = await client.chat.completions.async_create(messages, "Hello", max_tokens=1)
  20. self.assertIsInstance(response, ChatCompletion)
  21. self.assertEqual("How ", response.choices[0].message.content)
  22. response = await client.chat.completions.async_create(messages, "Hello", max_tokens=2)
  23. self.assertIsInstance(response, ChatCompletion)
  24. self.assertEqual("How are ", response.choices[0].message.content)
  25. async def test_max_stream(self):
  26. client = Client(provider=YieldProviderMock)
  27. messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
  28. response = await client.chat.completions.async_create(messages, "Hello", stream=True)
  29. async for chunk in response:
  30. self.assertIsInstance(chunk, ChatCompletionChunk)
  31. if chunk.choices[0].delta.content is not None:
  32. self.assertIsInstance(chunk.choices[0].delta.content, str)
  33. messages = [{'role': 'user', 'content': chunk} for chunk in ["You ", "You ", "Other", "?"]]
  34. response = await client.chat.completions.async_create(messages, "Hello", stream=True, max_tokens=2)
  35. response_list = []
  36. async for chunk in response:
  37. response_list.append(chunk)
  38. self.assertEqual(len(response_list), 3)
  39. for chunk in response_list:
  40. if chunk.choices[0].delta.content is not None:
  41. self.assertEqual(chunk.choices[0].delta.content, "You ")
  42. async def test_stop(self):
  43. client = Client(provider=YieldProviderMock)
  44. messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
  45. response = await client.chat.completions.async_create(messages, "Hello", stop=["and"])
  46. self.assertIsInstance(response, ChatCompletion)
  47. self.assertEqual("How are you?", response.choices[0].message.content)
  48. if __name__ == '__main__':
  49. unittest.main()