Add overridable hooks for custom chat serving implementations (#25807)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -475,5 +475,20 @@ class TestValidationEdgeCases(unittest.TestCase):
|
||||
self.assertEqual(len(restored_request.messages), len(original_request.messages))
|
||||
|
||||
|
||||
class TestParsedResponseFieldsProtocol(unittest.TestCase):
|
||||
"""Test ParsedResponseFields protocol."""
|
||||
|
||||
def test_parsed_response_fields_protocol(self):
|
||||
"""ParsedResponseFields protocol works with isinstance."""
|
||||
from sglang.srt.entrypoints.openai.protocol import ParsedResponseFields
|
||||
|
||||
class MockFields:
|
||||
content = "hello"
|
||||
tool_calls = None
|
||||
reasoning_content = None
|
||||
|
||||
self.assertIsInstance(MockFields(), ParsedResponseFields)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
|
||||
@@ -1474,6 +1474,26 @@ class ServingChatTestCase(unittest.TestCase):
|
||||
result = self.chat._apply_conversation_template(req, is_multimodal=False)
|
||||
self.assertEqual(result.prompt, "BASE_PROMPT")
|
||||
|
||||
# ------------- hook method tests -------------
|
||||
def test_encode_messages_returns_none_by_default(self):
|
||||
"""Default _encode_messages returns None (use standard encoding)."""
|
||||
result = self.chat._encode_messages([], Mock(), False)
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_decode_response_returns_text(self):
|
||||
"""Default _decode_response returns ret_item['text']."""
|
||||
ret_item = {"text": "Hello world", "output_ids": [1, 2, 3]}
|
||||
result = self.chat._decode_response(ret_item)
|
||||
self.assertEqual(result, "Hello world")
|
||||
|
||||
def test_get_parsed_response_fields_passthrough(self):
|
||||
"""Default _get_parsed_response_fields passes through values."""
|
||||
reasoning = "thinking..."
|
||||
tool_calls = [{"name": "foo"}]
|
||||
r, t = self.chat._get_parsed_response_fields(reasoning, tool_calls)
|
||||
self.assertEqual(r, reasoning)
|
||||
self.assertEqual(t, tool_calls)
|
||||
|
||||
|
||||
class TestProcessToolCallsWithRequiredToolChoice(unittest.TestCase):
|
||||
"""Test _process_tool_calls with tool_choice='required' uses model-specific parser."""
|
||||
|
||||
Reference in New Issue
Block a user