|
| 1 | +from utilities.plugins.constant import RestHeader, OpenAIEnpoints |
| 2 | +from simple_logger.logger import get_logger |
| 3 | + |
| 4 | +LOGGER = get_logger(name=__name__) |
| 5 | +MODELS_INFO = OpenAIEnpoints.MODELS_INFO |
| 6 | +CHAT_COMPLETIONS = OpenAIEnpoints.CHAT_COMPLETIONS |
| 7 | + |
| 8 | + |
| 9 | +class TestMaasEndpoints: |
| 10 | + def test_model(self, request_session_http, base_url: str, minted_token: str) -> None: |
| 11 | + """Verify /v1/models endpoint is reachable and returns available models.""" |
| 12 | + headers = {"Authorization": f"Bearer {minted_token}", **RestHeader.HEADERS} |
| 13 | + url = f"{base_url}{MODELS_INFO}" |
| 14 | + |
| 15 | + resp = request_session_http.get(url, headers=headers, timeout=60) |
| 16 | + assert resp.status_code == 200, f"/v1/models failed: {resp.status_code} {resp.text[:200]}" |
| 17 | + |
| 18 | + body = resp.json() |
| 19 | + assert isinstance(body.get("data"), list), "'data' missing or not a list" |
| 20 | + assert body["data"], "no models found" |
| 21 | + |
| 22 | + def test_chat_completions( |
| 23 | + self, |
| 24 | + request_session_http, |
| 25 | + base_url: str, |
| 26 | + minted_token: str, |
| 27 | + model_url: str, |
| 28 | + ) -> None: |
| 29 | + """ |
| 30 | + Verify the chat completion endpoint /llm/<deployment>/v1/chat/completions |
| 31 | + responds correctly to a prompt request. |
| 32 | +
|
| 33 | + """ |
| 34 | + headers = {"Authorization": f"Bearer {minted_token}", **RestHeader.HEADERS} |
| 35 | + |
| 36 | + # 1) Pick a model id from /v1/models |
| 37 | + models_url = f"{base_url}{MODELS_INFO}" |
| 38 | + models_resp = request_session_http.get(models_url, headers=headers, timeout=60) |
| 39 | + assert models_resp.status_code == 200, f"/v1/models failed: {models_resp.status_code} {models_resp.text[:200]}" |
| 40 | + models = models_resp.json().get("data", []) |
| 41 | + assert models, "no models available" |
| 42 | + model_id = models[0].get("id", "") |
| 43 | + LOGGER.info("Using model_id=%s", model_id) |
| 44 | + |
| 45 | + # 2) Prepare the chat completion endpoint URL |
| 46 | + payload = {"model": model_id, "prompt": "Hello", "max_tokens": 50} |
| 47 | + LOGGER.info("POST %s with keys=%s", model_url, list(payload.keys())) |
| 48 | + resp = request_session_http.post(url=model_url, headers=headers, json=payload, timeout=60) |
| 49 | + LOGGER.info("POST %s -> %s", model_url, resp.status_code) |
| 50 | + assert resp.status_code == 200, ( |
| 51 | + f"/v1/chat/completions failed: {resp.status_code} {resp.text[:200]} (url={model_url})" |
| 52 | + ) |
| 53 | + |
| 54 | + body = resp.json() |
| 55 | + assert isinstance(body.get("choices"), list), "'choices' missing or not a list" |
| 56 | + if body["choices"]: |
| 57 | + msg = body["choices"][0].get("message", {}) or {} |
| 58 | + text = msg.get("content") or body["choices"][0].get("text", "") |
| 59 | + assert isinstance(text, str) and text.strip() != "", "first choice has no text content" |
0 commit comments