|
| 1 | +""" |
| 2 | +Usage: |
| 3 | +python3 -m unittest test_enable_thinking.TestEnableThinking.test_chat_completion_with_reasoning |
| 4 | +python3 -m unittest test_enable_thinking.TestEnableThinking.test_chat_completion_without_reasoning |
| 5 | +python3 -m unittest test_enable_thinking.TestEnableThinking.test_stream_chat_completion_with_reasoning |
| 6 | +python3 -m unittest test_enable_thinking.TestEnableThinking.test_stream_chat_completion_without_reasoning |
| 7 | +""" |
| 8 | + |
| 9 | +import asyncio |
| 10 | +import json |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import time |
| 14 | +import unittest |
| 15 | + |
| 16 | +import requests |
| 17 | + |
| 18 | +from sglang.srt.utils import kill_process_tree |
| 19 | +from sglang.test.test_utils import ( |
| 20 | + DEFAULT_ENABLE_THINKING_MODEL_NAME_FOR_TEST, |
| 21 | + DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH, |
| 22 | + DEFAULT_URL_FOR_TEST, |
| 23 | + CustomTestCase, |
| 24 | + popen_launch_server, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class TestEnableThinking(CustomTestCase): |
| 29 | + @classmethod |
| 30 | + def setUpClass(cls): |
| 31 | + cls.model = DEFAULT_ENABLE_THINKING_MODEL_NAME_FOR_TEST |
| 32 | + cls.base_url = DEFAULT_URL_FOR_TEST |
| 33 | + cls.api_key = "sk-1234" |
| 34 | + cls.process = popen_launch_server( |
| 35 | + cls.model, |
| 36 | + cls.base_url, |
| 37 | + timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH, |
| 38 | + api_key=cls.api_key, |
| 39 | + other_args=[ |
| 40 | + "--reasoning-parser", |
| 41 | + "qwen3", |
| 42 | + ], |
| 43 | + ) |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def tearDownClass(cls): |
| 47 | + kill_process_tree(cls.process.pid) |
| 48 | + |
| 49 | + def test_chat_completion_with_reasoning(self): |
| 50 | + # Test non-streaming with "enable_thinking": True, reasoning_content should not be empty |
| 51 | + client = requests.post( |
| 52 | + f"{self.base_url}/v1/chat/completions", |
| 53 | + headers={"Authorization": f"Bearer {self.api_key}"}, |
| 54 | + json={ |
| 55 | + "model": self.model, |
| 56 | + "messages": [{"role": "user", "content": "Hello"}], |
| 57 | + "temperature": 0, |
| 58 | + "separate_reasoning": True, |
| 59 | + "chat_template_kwargs": {"enable_thinking": True}, |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + self.assertEqual(client.status_code, 200, f"Failed with: {client.text}") |
| 64 | + data = client.json() |
| 65 | + |
| 66 | + self.assertIn("choices", data) |
| 67 | + self.assertTrue(len(data["choices"]) > 0) |
| 68 | + self.assertIn("message", data["choices"][0]) |
| 69 | + self.assertIn("reasoning_content", data["choices"][0]["message"]) |
| 70 | + self.assertIsNotNone(data["choices"][0]["message"]["reasoning_content"]) |
| 71 | + |
| 72 | + def test_chat_completion_without_reasoning(self): |
| 73 | + # Test non-streaming with "enable_thinking": False, reasoning_content should be empty |
| 74 | + client = requests.post( |
| 75 | + f"{self.base_url}/v1/chat/completions", |
| 76 | + headers={"Authorization": f"Bearer {self.api_key}"}, |
| 77 | + json={ |
| 78 | + "model": self.model, |
| 79 | + "messages": [{"role": "user", "content": "Hello"}], |
| 80 | + "temperature": 0, |
| 81 | + "separate_reasoning": True, |
| 82 | + "chat_template_kwargs": {"enable_thinking": False}, |
| 83 | + }, |
| 84 | + ) |
| 85 | + |
| 86 | + self.assertEqual(client.status_code, 200, f"Failed with: {client.text}") |
| 87 | + data = client.json() |
| 88 | + |
| 89 | + self.assertIn("choices", data) |
| 90 | + self.assertTrue(len(data["choices"]) > 0) |
| 91 | + self.assertIn("message", data["choices"][0]) |
| 92 | + |
| 93 | + if "reasoning_content" in data["choices"][0]["message"]: |
| 94 | + self.assertIsNone(data["choices"][0]["message"]["reasoning_content"]) |
| 95 | + |
| 96 | + def test_stream_chat_completion_with_reasoning(self): |
| 97 | + # Test streaming with "enable_thinking": True, reasoning_content should not be empty |
| 98 | + response = requests.post( |
| 99 | + f"{self.base_url}/v1/chat/completions", |
| 100 | + headers={"Authorization": f"Bearer {self.api_key}"}, |
| 101 | + json={ |
| 102 | + "model": self.model, |
| 103 | + "messages": [{"role": "user", "content": "Hello"}], |
| 104 | + "temperature": 0, |
| 105 | + "separate_reasoning": True, |
| 106 | + "stream": True, |
| 107 | + "chat_template_kwargs": {"enable_thinking": True}, |
| 108 | + }, |
| 109 | + stream=True, |
| 110 | + ) |
| 111 | + |
| 112 | + self.assertEqual(response.status_code, 200, f"Failed with: {response.text}") |
| 113 | + |
| 114 | + has_reasoning = False |
| 115 | + has_content = False |
| 116 | + |
| 117 | + print("\n=== Stream With Reasoning ===") |
| 118 | + for line in response.iter_lines(): |
| 119 | + if line: |
| 120 | + line = line.decode("utf-8") |
| 121 | + if line.startswith("data:") and not line.startswith("data: [DONE]"): |
| 122 | + data = json.loads(line[6:]) |
| 123 | + if "choices" in data and len(data["choices"]) > 0: |
| 124 | + delta = data["choices"][0].get("delta", {}) |
| 125 | + |
| 126 | + if "reasoning_content" in delta and delta["reasoning_content"]: |
| 127 | + has_reasoning = True |
| 128 | + |
| 129 | + if "content" in delta and delta["content"]: |
| 130 | + has_content = True |
| 131 | + |
| 132 | + self.assertTrue( |
| 133 | + has_reasoning, |
| 134 | + "The reasoning content is not included in the stream response", |
| 135 | + ) |
| 136 | + self.assertTrue( |
| 137 | + has_content, "The stream response does not contain normal content" |
| 138 | + ) |
| 139 | + |
| 140 | + def test_stream_chat_completion_without_reasoning(self): |
| 141 | + # Test streaming with "enable_thinking": False, reasoning_content should be empty |
| 142 | + response = requests.post( |
| 143 | + f"{self.base_url}/v1/chat/completions", |
| 144 | + headers={"Authorization": f"Bearer {self.api_key}"}, |
| 145 | + json={ |
| 146 | + "model": self.model, |
| 147 | + "messages": [{"role": "user", "content": "Hello"}], |
| 148 | + "temperature": 0, |
| 149 | + "separate_reasoning": True, |
| 150 | + "stream": True, |
| 151 | + "chat_template_kwargs": {"enable_thinking": False}, |
| 152 | + }, |
| 153 | + stream=True, |
| 154 | + ) |
| 155 | + |
| 156 | + self.assertEqual(response.status_code, 200, f"Failed with: {response.text}") |
| 157 | + |
| 158 | + has_reasoning = False |
| 159 | + has_content = False |
| 160 | + |
| 161 | + print("\n=== Stream Without Reasoning ===") |
| 162 | + for line in response.iter_lines(): |
| 163 | + if line: |
| 164 | + line = line.decode("utf-8") |
| 165 | + if line.startswith("data:") and not line.startswith("data: [DONE]"): |
| 166 | + data = json.loads(line[6:]) |
| 167 | + if "choices" in data and len(data["choices"]) > 0: |
| 168 | + delta = data["choices"][0].get("delta", {}) |
| 169 | + |
| 170 | + if "reasoning_content" in delta and delta["reasoning_content"]: |
| 171 | + has_reasoning = True |
| 172 | + |
| 173 | + if "content" in delta and delta["content"]: |
| 174 | + has_content = True |
| 175 | + |
| 176 | + self.assertFalse( |
| 177 | + has_reasoning, |
| 178 | + "The reasoning content should not be included in the stream response", |
| 179 | + ) |
| 180 | + self.assertTrue( |
| 181 | + has_content, "The stream response does not contain normal content" |
| 182 | + ) |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == "__main__": |
| 186 | + unittest.main() |
0 commit comments