1
0

test_template.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. import pytest
  3. # ensure grandparent path is in sys.path
  4. from pathlib import Path
  5. import sys
  6. from unit.test_tool_call import TEST_TOOL
  7. path = Path(__file__).resolve().parents[1]
  8. sys.path.insert(0, str(path))
  9. import datetime
  10. from utils import *
  11. server: ServerProcess
  12. @pytest.fixture(autouse=True)
  13. def create_server():
  14. global server
  15. server = ServerPreset.tinyllama2()
  16. server.model_alias = "tinyllama-2"
  17. server.n_slots = 1
  18. @pytest.mark.parametrize("tools", [None, [], [TEST_TOOL]])
  19. @pytest.mark.parametrize("template_name,reasoning_budget,expected_end", [
  20. ("deepseek-ai-DeepSeek-R1-Distill-Qwen-32B", None, "<think>\n"),
  21. ("deepseek-ai-DeepSeek-R1-Distill-Qwen-32B", -1, "<think>\n"),
  22. ("deepseek-ai-DeepSeek-R1-Distill-Qwen-32B", 0, "<think>\n</think>"),
  23. ("Qwen-Qwen3-0.6B", -1, "<|im_start|>assistant\n"),
  24. ("Qwen-Qwen3-0.6B", 0, "<|im_start|>assistant\n<think>\n\n</think>\n\n"),
  25. ("Qwen-QwQ-32B", -1, "<|im_start|>assistant\n<think>\n"),
  26. ("Qwen-QwQ-32B", 0, "<|im_start|>assistant\n<think>\n</think>"),
  27. ("CohereForAI-c4ai-command-r7b-12-2024-tool_use", -1, "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>"),
  28. ("CohereForAI-c4ai-command-r7b-12-2024-tool_use", 0, "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|><|START_THINKING|><|END_THINKING|>"),
  29. ])
  30. def test_reasoning_budget(template_name: str, reasoning_budget: int | None, expected_end: str, tools: list[dict]):
  31. global server
  32. server.jinja = True
  33. server.reasoning_budget = reasoning_budget
  34. server.chat_template_file = f'../../../models/templates/{template_name}.jinja'
  35. server.start()
  36. res = server.make_request("POST", "/apply-template", data={
  37. "messages": [
  38. {"role": "user", "content": "What is today?"},
  39. ],
  40. "tools": tools,
  41. })
  42. assert res.status_code == 200
  43. prompt = res.body["prompt"]
  44. assert prompt.endswith(expected_end), f"Expected prompt to end with '{expected_end}', got '{prompt}'"
  45. @pytest.mark.parametrize("tools", [None, [], [TEST_TOOL]])
  46. @pytest.mark.parametrize("template_name,format", [
  47. ("meta-llama-Llama-3.3-70B-Instruct", "%d %b %Y"),
  48. ("fireworks-ai-llama-3-firefunction-v2", "%b %d %Y"),
  49. ])
  50. def test_date_inside_prompt(template_name: str, format: str, tools: list[dict]):
  51. global server
  52. server.jinja = True
  53. server.chat_template_file = f'../../../models/templates/{template_name}.jinja'
  54. server.start()
  55. res = server.make_request("POST", "/apply-template", data={
  56. "messages": [
  57. {"role": "user", "content": "What is today?"},
  58. ],
  59. "tools": tools,
  60. })
  61. assert res.status_code == 200
  62. prompt = res.body["prompt"]
  63. today_str = datetime.date.today().strftime(format)
  64. assert today_str in prompt, f"Expected today's date ({today_str}) in content ({prompt})"
  65. @pytest.mark.parametrize("add_generation_prompt", [False, True])
  66. @pytest.mark.parametrize("template_name,expected_generation_prompt", [
  67. ("meta-llama-Llama-3.3-70B-Instruct", "<|start_header_id|>assistant<|end_header_id|>"),
  68. ])
  69. def test_add_generation_prompt(template_name: str, expected_generation_prompt: str, add_generation_prompt: bool):
  70. global server
  71. server.jinja = True
  72. server.chat_template_file = f'../../../models/templates/{template_name}.jinja'
  73. server.start()
  74. res = server.make_request("POST", "/apply-template", data={
  75. "messages": [
  76. {"role": "user", "content": "What is today?"},
  77. ],
  78. "add_generation_prompt": add_generation_prompt,
  79. })
  80. assert res.status_code == 200
  81. prompt = res.body["prompt"]
  82. if add_generation_prompt:
  83. assert expected_generation_prompt in prompt, f"Expected generation prompt ({expected_generation_prompt}) in content ({prompt})"
  84. else:
  85. assert expected_generation_prompt not in prompt, f"Did not expect generation prompt ({expected_generation_prompt}) in content ({prompt})"