|
3 | 3 | from tests.model_serving.model_runtime.vllm.constant import VLLM_SUPPORTED_QUANTIZATION |
4 | 4 |
|
5 | 5 |
|
| 6 | +def extract_content_field(output: Any) -> str: |
| 7 | + """ |
| 8 | + Extract the 'content' field from a typical VLLM chat response. |
| 9 | + Returns an empty string if not found. |
| 10 | + """ |
| 11 | + try: |
| 12 | + return output["choices"][0]["message"].get("content", "").strip() |
| 13 | + except (KeyError, IndexError, TypeError): |
| 14 | + return "" |
| 15 | + |
| 16 | + |
6 | 17 | def validate_supported_quantization_schema(q_type: str) -> None: |
7 | 18 | if q_type not in VLLM_SUPPORTED_QUANTIZATION: |
8 | 19 | raise ValueError(f"Unsupported quantization type: {q_type}") |
9 | 20 |
|
10 | 21 |
|
11 | | -def validate_inference_output(*args: tuple[str, ...] | list[Any], response_snapshot: Any) -> None: |
12 | | - for data in args: |
13 | | - assert data == response_snapshot, f"output mismatch for {data}" |
14 | | - |
| 22 | +def validate_inference_output(response_output: Any, expected_keywords: list[str]) -> None: |
| 23 | + """ |
| 24 | + Validate inference response output using regex-based keyword checks. |
15 | 25 |
|
16 | | -def safe_k8s_name(model_name: str, max_length: int = 20) -> str: |
| 26 | + - Extracts 'content' field from model output. |
| 27 | + - Fails if content is empty. |
| 28 | + - Passes if any of the expected keywords/phrases are found in the content (case-insensitive). |
17 | 29 | """ |
18 | | - Create a safe Kubernetes name from model_name by truncating to max_length characters |
19 | | - and ensuring it follows Kubernetes naming conventions. |
20 | 30 |
|
21 | | - Args: |
22 | | - model_name: The original model name |
23 | | - max_length: Maximum length for the name (default: 20) |
| 31 | + content = extract_content_field(response_output) |
| 32 | + assert content, "Inference output is empty or missing 'content' field." |
| 33 | + found_keywords = [kw for kw in expected_keywords if re.search(rf"\b{re.escape(kw)}\b", content, re.IGNORECASE)] |
| 34 | + assert found_keywords, ( |
| 35 | + f"Expected one of {expected_keywords} in response content, " |
| 36 | + f"but got: {content[:900]}..." |
| 37 | + ) |
| 38 | + |
| 39 | + print(f"Output validation passed. Found keywords: {found_keywords}") |
| 40 | + |
24 | 41 |
|
25 | | - Returns: |
26 | | - A valid Kubernetes name truncated to max_length characters |
| 42 | +def safe_k8s_name(model_name: str, max_length: int = 20) -> str: |
| 43 | + """ |
| 44 | + Generate a Kubernetes-safe model name. |
27 | 45 | """ |
28 | 46 | if not model_name: |
29 | 47 | return "default-model" |
|
0 commit comments