client.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from __future__ import annotations
  2. import unittest
  3. from g4f.client import Client, AsyncClient, ChatCompletion, ChatCompletionChunk
  4. from .mocks import AsyncGeneratorProviderMock, ModelProviderMock, YieldProviderMock
  5. DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
  6. class AsyncTestPassModel(unittest.IsolatedAsyncioTestCase):
  7. async def test_response(self):
  8. client = AsyncClient(provider=AsyncGeneratorProviderMock)
  9. response = await client.chat.completions.create(DEFAULT_MESSAGES, "")
  10. self.assertIsInstance(response, ChatCompletion)
  11. self.assertEqual("Mock", response.choices[0].message.content)
  12. async def test_pass_model(self):
  13. client = AsyncClient(provider=ModelProviderMock)
  14. response = await client.chat.completions.create(DEFAULT_MESSAGES, "Hello")
  15. self.assertIsInstance(response, ChatCompletion)
  16. self.assertEqual("Hello", response.choices[0].message.content)
  17. async def test_max_tokens(self):
  18. client = AsyncClient(provider=YieldProviderMock)
  19. messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
  20. response = await client.chat.completions.create(messages, "Hello", max_tokens=1)
  21. self.assertIsInstance(response, ChatCompletion)
  22. self.assertEqual("How ", response.choices[0].message.content)
  23. response = await client.chat.completions.create(messages, "Hello", max_tokens=2)
  24. self.assertIsInstance(response, ChatCompletion)
  25. self.assertEqual("How are ", response.choices[0].message.content)
  26. async def test_max_stream(self):
  27. client = AsyncClient(provider=YieldProviderMock)
  28. messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
  29. response = client.chat.completions.create(messages, "Hello", stream=True)
  30. async for chunk in response:
  31. chunk: ChatCompletionChunk = chunk
  32. self.assertIsInstance(chunk, ChatCompletionChunk)
  33. if chunk.choices[0].delta.content is not None:
  34. self.assertIsInstance(chunk.choices[0].delta.content, str)
  35. messages = [{'role': 'user', 'content': chunk} for chunk in ["You ", "You ", "Other", "?"]]
  36. response = client.chat.completions.create(messages, "Hello", stream=True, max_tokens=2)
  37. response_list = []
  38. async for chunk in response:
  39. response_list.append(chunk)
  40. self.assertEqual(len(response_list), 3)
  41. for chunk in response_list:
  42. if chunk.choices[0].delta.content is not None:
  43. self.assertEqual(chunk.choices[0].delta.content, "You ")
  44. async def test_stop(self):
  45. client = AsyncClient(provider=YieldProviderMock)
  46. messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
  47. response = await client.chat.completions.create(messages, "Hello", stop=["and"])
  48. self.assertIsInstance(response, ChatCompletion)
  49. self.assertEqual("How are you?", response.choices[0].message.content)
  50. class TestPassModel(unittest.TestCase):
  51. def test_response(self):
  52. client = Client(provider=AsyncGeneratorProviderMock)
  53. response = client.chat.completions.create(DEFAULT_MESSAGES, "")
  54. self.assertIsInstance(response, ChatCompletion)
  55. self.assertEqual("Mock", response.choices[0].message.content)
  56. def test_pass_model(self):
  57. client = Client(provider=ModelProviderMock)
  58. response = client.chat.completions.create(DEFAULT_MESSAGES, "Hello")
  59. self.assertIsInstance(response, ChatCompletion)
  60. self.assertEqual("Hello", response.choices[0].message.content)
  61. def test_max_tokens(self):
  62. client = Client(provider=YieldProviderMock)
  63. messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
  64. response = client.chat.completions.create(messages, "Hello", max_tokens=1)
  65. self.assertIsInstance(response, ChatCompletion)
  66. self.assertEqual("How ", response.choices[0].message.content)
  67. response = client.chat.completions.create(messages, "Hello", max_tokens=2)
  68. self.assertIsInstance(response, ChatCompletion)
  69. self.assertEqual("How are ", response.choices[0].message.content)
  70. def test_max_stream(self):
  71. client = Client(provider=YieldProviderMock)
  72. messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
  73. response = client.chat.completions.create(messages, "Hello", stream=True)
  74. for chunk in response:
  75. self.assertIsInstance(chunk, ChatCompletionChunk)
  76. if chunk.choices[0].delta.content is not None:
  77. self.assertIsInstance(chunk.choices[0].delta.content, str)
  78. messages = [{'role': 'user', 'content': chunk} for chunk in ["You ", "You ", "Other", "?"]]
  79. response = client.chat.completions.create(messages, "Hello", stream=True, max_tokens=2)
  80. response_list = list(response)
  81. self.assertEqual(len(response_list), 3)
  82. for chunk in response_list:
  83. if chunk.choices[0].delta.content is not None:
  84. self.assertEqual(chunk.choices[0].delta.content, "You ")
  85. def test_stop(self):
  86. client = Client(provider=YieldProviderMock)
  87. messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
  88. response = client.chat.completions.create(messages, "Hello", stop=["and"])
  89. self.assertIsInstance(response, ChatCompletion)
  90. self.assertEqual("How are you?", response.choices[0].message.content)
  91. if __name__ == '__main__':
  92. unittest.main()