test_acompletion.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import asyncio
  2. from unittest.mock import AsyncMock, patch
  3. import pytest
  4. from openhands.core.config import load_app_config
  5. from openhands.core.exceptions import UserCancelledError
  6. from openhands.llm.async_llm import AsyncLLM
  7. from openhands.llm.llm import LLM
  8. from openhands.llm.streaming_llm import StreamingLLM
  9. config = load_app_config()
  10. @pytest.fixture
  11. def test_llm():
  12. # Create a mock config for testing
  13. return LLM(config=config.get_llm_config())
  14. @pytest.fixture
  15. def mock_response():
  16. return [
  17. {'choices': [{'delta': {'content': 'This is a'}}]},
  18. {'choices': [{'delta': {'content': ' test'}}]},
  19. {'choices': [{'delta': {'content': ' message.'}}]},
  20. {'choices': [{'delta': {'content': ' It is'}}]},
  21. {'choices': [{'delta': {'content': ' a bit'}}]},
  22. {'choices': [{'delta': {'content': ' longer'}}]},
  23. {'choices': [{'delta': {'content': ' than'}}]},
  24. {'choices': [{'delta': {'content': ' the'}}]},
  25. {'choices': [{'delta': {'content': ' previous'}}]},
  26. {'choices': [{'delta': {'content': ' one,'}}]},
  27. {'choices': [{'delta': {'content': ' but'}}]},
  28. {'choices': [{'delta': {'content': ' hopefully'}}]},
  29. {'choices': [{'delta': {'content': ' still'}}]},
  30. {'choices': [{'delta': {'content': ' short'}}]},
  31. {'choices': [{'delta': {'content': ' enough.'}}]},
  32. ]
  33. @pytest.mark.asyncio
  34. async def test_acompletion_non_streaming():
  35. with patch.object(AsyncLLM, '_call_acompletion') as mock_call_acompletion:
  36. mock_response = {
  37. 'choices': [{'message': {'content': 'This is a test message.'}}]
  38. }
  39. mock_call_acompletion.return_value = mock_response
  40. test_llm = AsyncLLM(config=config.get_llm_config())
  41. response = await test_llm.async_completion(
  42. messages=[{'role': 'user', 'content': 'Hello!'}],
  43. stream=False,
  44. drop_params=True,
  45. )
  46. # Assertions for non-streaming completion
  47. assert response['choices'][0]['message']['content'] != ''
  48. @pytest.mark.asyncio
  49. async def test_acompletion_streaming(mock_response):
  50. with patch.object(StreamingLLM, '_call_acompletion') as mock_call_acompletion:
  51. mock_call_acompletion.return_value.__aiter__.return_value = iter(mock_response)
  52. test_llm = StreamingLLM(config=config.get_llm_config())
  53. async for chunk in test_llm.async_streaming_completion(
  54. messages=[{'role': 'user', 'content': 'Hello!'}], stream=True
  55. ):
  56. print(f"Chunk: {chunk['choices'][0]['delta']['content']}")
  57. # Assertions for streaming completion
  58. assert chunk['choices'][0]['delta']['content'] in [
  59. r['choices'][0]['delta']['content'] for r in mock_response
  60. ]
  61. @pytest.mark.asyncio
  62. async def test_completion(test_llm):
  63. with patch.object(LLM, 'completion') as mock_completion:
  64. mock_completion.return_value = {
  65. 'choices': [{'message': {'content': 'This is a test message.'}}]
  66. }
  67. response = test_llm.completion(messages=[{'role': 'user', 'content': 'Hello!'}])
  68. assert response['choices'][0]['message']['content'] == 'This is a test message.'
  69. @pytest.mark.asyncio
  70. @pytest.mark.parametrize('cancel_delay', [0.1, 0.3, 0.5, 0.7, 0.9])
  71. async def test_async_completion_with_user_cancellation(cancel_delay):
  72. cancel_event = asyncio.Event()
  73. async def mock_on_cancel_requested():
  74. is_set = cancel_event.is_set()
  75. print(f'Cancel requested: {is_set}')
  76. return is_set
  77. config = load_app_config()
  78. config.on_cancel_requested_fn = mock_on_cancel_requested
  79. async def mock_acompletion(*args, **kwargs):
  80. print('Starting mock_acompletion')
  81. for i in range(20): # Increased iterations for longer running task
  82. print(f'mock_acompletion iteration {i}')
  83. await asyncio.sleep(0.1)
  84. if await mock_on_cancel_requested():
  85. print('Cancellation detected in mock_acompletion')
  86. raise UserCancelledError('LLM request cancelled by user')
  87. print('Completing mock_acompletion without cancellation')
  88. return {'choices': [{'message': {'content': 'This is a test message.'}}]}
  89. with patch.object(
  90. AsyncLLM, '_call_acompletion', new_callable=AsyncMock
  91. ) as mock_call_acompletion:
  92. mock_call_acompletion.side_effect = mock_acompletion
  93. test_llm = AsyncLLM(config=config.get_llm_config())
  94. async def cancel_after_delay():
  95. print(f'Starting cancel_after_delay with delay {cancel_delay}')
  96. await asyncio.sleep(cancel_delay)
  97. print('Setting cancel event')
  98. cancel_event.set()
  99. with pytest.raises(UserCancelledError):
  100. await asyncio.gather(
  101. test_llm.async_completion(
  102. messages=[{'role': 'user', 'content': 'Hello!'}],
  103. stream=False,
  104. ),
  105. cancel_after_delay(),
  106. )
  107. # Ensure the mock was called
  108. mock_call_acompletion.assert_called_once()
  109. @pytest.mark.asyncio
  110. @pytest.mark.parametrize('cancel_after_chunks', [1, 3, 5, 7, 9])
  111. async def test_async_streaming_completion_with_user_cancellation(cancel_after_chunks):
  112. cancel_requested = False
  113. async def mock_on_cancel_requested():
  114. nonlocal cancel_requested
  115. return cancel_requested
  116. config = load_app_config()
  117. config.on_cancel_requested_fn = mock_on_cancel_requested
  118. test_messages = [
  119. 'This is ',
  120. 'a test ',
  121. 'message ',
  122. 'with ',
  123. 'multiple ',
  124. 'chunks ',
  125. 'to ',
  126. 'simulate ',
  127. 'a ',
  128. 'longer ',
  129. 'streaming ',
  130. 'response.',
  131. ]
  132. async def mock_acompletion(*args, **kwargs):
  133. for i, content in enumerate(test_messages):
  134. yield {'choices': [{'delta': {'content': content}}]}
  135. if i + 1 == cancel_after_chunks:
  136. nonlocal cancel_requested
  137. cancel_requested = True
  138. if cancel_requested:
  139. raise UserCancelledError('LLM request cancelled by user')
  140. await asyncio.sleep(0.05) # Simulate some delay between chunks
  141. with patch.object(
  142. AsyncLLM, '_call_acompletion', new_callable=AsyncMock
  143. ) as mock_call_acompletion:
  144. mock_call_acompletion.return_value = mock_acompletion()
  145. test_llm = StreamingLLM(config=config.get_llm_config())
  146. received_chunks = []
  147. with pytest.raises(UserCancelledError):
  148. async for chunk in test_llm.async_streaming_completion(
  149. messages=[{'role': 'user', 'content': 'Hello!'}], stream=True
  150. ):
  151. received_chunks.append(chunk['choices'][0]['delta']['content'])
  152. print(f"Chunk: {chunk['choices'][0]['delta']['content']}")
  153. # Assert that we received the expected number of chunks before cancellation
  154. assert len(received_chunks) == cancel_after_chunks
  155. assert received_chunks == test_messages[:cancel_after_chunks]
  156. # Ensure the mock was called
  157. mock_call_acompletion.assert_called_once()