test_thinking_alias.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. """Host-side tests for chat_template_kwargs.thinking / enable_thinking aliasing."""
  3. import sys
  4. from pathlib import Path
  5. from types import SimpleNamespace
  6. ROOT = Path(__file__).resolve().parents[1]
  7. sys.path.insert(0, str(ROOT / 'adapter'))
  8. from encoding_compat import ( # noqa: E402
  9. PUBLISHER_REASONING_EFFORT,
  10. install_encoder,
  11. install_serving_chat,
  12. normalize_chat_template_kwargs,
  13. )
  14. def test_normalize_copies_enable_thinking():
  15. got = normalize_chat_template_kwargs({'enable_thinking': False})
  16. assert got == {'enable_thinking': False, 'thinking': False}
  17. got = normalize_chat_template_kwargs({'enable_thinking': True})
  18. assert got == {'enable_thinking': True, 'thinking': True}
  19. got = normalize_chat_template_kwargs({'enable_thinking': 0})
  20. assert got == {'enable_thinking': False, 'thinking': False}
  21. def test_normalize_copies_thinking():
  22. got = normalize_chat_template_kwargs({'thinking': False})
  23. assert got == {'thinking': False, 'enable_thinking': False}
  24. got = normalize_chat_template_kwargs({'thinking': True, 'foo': 1})
  25. assert got == {'thinking': True, 'enable_thinking': True, 'foo': 1}
  26. def test_thinking_wins_on_conflict():
  27. import logging
  28. logging.disable(logging.WARNING)
  29. try:
  30. got = normalize_chat_template_kwargs(
  31. {'thinking': False, 'enable_thinking': True})
  32. finally:
  33. logging.disable(logging.NOTSET)
  34. assert got['thinking'] is False and got['enable_thinking'] is False
  35. def test_empty_and_none_pass_through():
  36. assert normalize_chat_template_kwargs(None) is None
  37. assert normalize_chat_template_kwargs({}) == {}
  38. def test_install_serving_chat_rewrites_request():
  39. class FakeServing:
  40. class OpenAIServingChat:
  41. def _process_messages(self, request, is_multimodal):
  42. return request.chat_template_kwargs
  43. install_serving_chat(FakeServing)
  44. serving = FakeServing.OpenAIServingChat()
  45. request = SimpleNamespace(chat_template_kwargs={'enable_thinking': False})
  46. out = serving._process_messages(request, False)
  47. assert out == {'enable_thinking': False, 'thinking': False}
  48. assert request.chat_template_kwargs['thinking'] is False
  49. def test_install_encoder_publisher_table():
  50. mappings = {'low': 25, 'high': 50, 'xhigh': 75, 'max': 100}
  51. module = SimpleNamespace(REASONING_EFFORT_MAPPINGS=mappings)
  52. install_encoder(module)
  53. assert module.REASONING_EFFORT_MAPPINGS == PUBLISHER_REASONING_EFFORT
  54. assert mappings is module.REASONING_EFFORT_MAPPINGS # in-place; readers keep the dict
  55. if __name__ == '__main__':
  56. tests = [v for k, v in list(globals().items()) if k.startswith('test_')]
  57. failed = 0
  58. for fn in tests:
  59. try:
  60. fn()
  61. print(f'[OK] {fn.__name__}')
  62. except Exception as exc:
  63. failed += 1
  64. print(f'[FAIL] {fn.__name__}: {exc}')
  65. print(f'{len(tests) - failed}/{len(tests)} passed')
  66. sys.exit(1 if failed else 0)