retry_provider.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from __future__ import annotations
  2. import unittest
  3. from g4f.client import AsyncClient, ChatCompletion, ChatCompletionChunk
  4. from g4f.providers.retry_provider import IterListProvider
  5. from .mocks import YieldProviderMock, RaiseExceptionProviderMock, AsyncRaiseExceptionProviderMock, YieldNoneProviderMock
  6. DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
  7. class TestIterListProvider(unittest.IsolatedAsyncioTestCase):
  8. async def test_skip_provider(self):
  9. client = AsyncClient(provider=IterListProvider([RaiseExceptionProviderMock, YieldProviderMock], False))
  10. response = await client.chat.completions.create(DEFAULT_MESSAGES, "")
  11. self.assertIsInstance(response, ChatCompletion)
  12. self.assertEqual("Hello", response.choices[0].message.content)
  13. async def test_only_one_result(self):
  14. client = AsyncClient(provider=IterListProvider([YieldProviderMock, YieldProviderMock]))
  15. response = await client.chat.completions.create(DEFAULT_MESSAGES, "")
  16. self.assertIsInstance(response, ChatCompletion)
  17. self.assertEqual("Hello", response.choices[0].message.content)
  18. async def test_stream_skip_provider(self):
  19. client = AsyncClient(provider=IterListProvider([AsyncRaiseExceptionProviderMock, YieldProviderMock], False))
  20. messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
  21. response = client.chat.completions.create(messages, "Hello", stream=True)
  22. async for chunk in response:
  23. chunk: ChatCompletionChunk = chunk
  24. self.assertIsInstance(chunk, ChatCompletionChunk)
  25. if chunk.choices[0].delta.content is not None:
  26. self.assertIsInstance(chunk.choices[0].delta.content, str)
  27. async def test_stream_only_one_result(self):
  28. client = AsyncClient(provider=IterListProvider([YieldProviderMock, YieldProviderMock], False))
  29. messages = [{'role': 'user', 'content': chunk} for chunk in ["You ", "You "]]
  30. response = client.chat.completions.create(messages, "Hello", stream=True, max_tokens=2)
  31. response_list = []
  32. async for chunk in response:
  33. response_list.append(chunk)
  34. self.assertEqual(len(response_list), 3)
  35. for chunk in response_list:
  36. if chunk.choices[0].delta.content is not None:
  37. self.assertEqual(chunk.choices[0].delta.content, "You ")
  38. async def test_skip_none(self):
  39. client = AsyncClient(provider=IterListProvider([YieldNoneProviderMock, YieldProviderMock], False))
  40. response = await client.chat.completions.create(DEFAULT_MESSAGES, "")
  41. self.assertIsInstance(response, ChatCompletion)
  42. self.assertEqual("Hello", response.choices[0].message.content)
  43. async def test_stream_skip_none(self):
  44. client = AsyncClient(provider=IterListProvider([YieldNoneProviderMock, YieldProviderMock], False))
  45. response = client.chat.completions.create(DEFAULT_MESSAGES, "", stream=True)
  46. response_list = [chunk async for chunk in response]
  47. self.assertEqual(len(response_list), 2)
  48. for chunk in response_list:
  49. if chunk.choices[0].delta.content is not None:
  50. self.assertEqual(chunk.choices[0].delta.content, "Hello")