| # Licensed to the Apache Software Foundation (ASF) under one | |
| # or more contributor license agreements. See the NOTICE file | |
| # distributed with this work for additional information | |
| # regarding copyright ownership. The ASF licenses this file | |
| # to you under the Apache License, Version 2.0 (the | |
| # "License"); you may not use this file except in compliance | |
| # with the License. You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, | |
| # software distributed under the License is distributed on an | |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| # KIND, either express or implied. See the License for the | |
| # specific language governing permissions and limitations | |
| # under the License. | |
| """Model inventory: shared YAML + LiteLLM model_info extras.""" | |
| from __future__ import annotations | |
| import pathlib | |
| import re | |
| import pytest | |
| import yaml | |
| from easydict import EasyDict as edict # noqa: N813 | |
| from litellm.types.router import Deployment | |
| from llmao.models import ( | |
| VllmRecipe, | |
| load_models, | |
| model_available_for, | |
| model_in_service, | |
| models_path_from_cfg, | |
| public_models, | |
| ux_models, | |
| validate_models, | |
| warn_deprecated, | |
| ) | |
| ROOT = pathlib.Path(__file__).resolve().parent.parent | |
| EXAMPLE = ROOT / "models.yaml" | |
| def test_models_yaml_loads_for_ux(): | |
| models = load_models(EXAMPLE) | |
| assert len(models) >= 2 | |
| names = {m.model_name for m in models} | |
| assert models[0].model_info.vllm.model | |
| assert "gemma4-26b" in names | |
| assert "qwen3-8b" in names | |
| pub = public_models(models) | |
| for p in pub: | |
| assert "model_name" in p | |
| assert "api_key" not in p | |
| assert "api_base" not in p | |
| assert "litellm_params" not in p | |
| assert p.get("license") | |
| assert p.get("openness") | |
| def test_missing_models_yaml_fails_fast(): | |
| missing = ROOT / "models.yaml.does-not-exist" | |
| with pytest.raises(FileNotFoundError, match="Missing"): | |
| load_models(missing) | |
| def test_models_path_from_cfg(): | |
| cfg = edict({"models_path": "models.yaml"}) | |
| p = models_path_from_cfg(cfg) | |
| assert p.name == "models.yaml" | |
| assert p.is_file() | |
| def test_ux_models_redacts_supply_path_for_non_admins(): | |
| models = load_models(EXAMPLE) | |
| redacted = ux_models(models, reveal_supply=False) | |
| full = ux_models(models, reveal_supply=True) | |
| assert len(redacted) >= 2 | |
| assert len(full) == len(redacted) | |
| for r in redacted: | |
| assert r["model_name"] | |
| assert r["display_name"] | |
| assert r["weights_distribution"] == "" | |
| assert model_available_for(None, r) is True | |
| # Free-text flattened for data-* attributes (no raw newlines). | |
| assert "\n" not in r["notes"] | |
| for f in full: | |
| # Example inventory includes weights_distribution for self-host models. | |
| assert f.get("weights_distribution") | |
| assert "\n" not in f["notes"] | |
| def test_a_model_need_not_say_where_it_is_served(): | |
| """Who serves a model is each deployment's, so a model may carry no recipe.""" | |
| row = edict({"model_name": "grok-4", "model_info": {"license": "proprietary"}}) | |
| validate_models([row]) | |
| assert VllmRecipe.from_model(row) is None | |
| ours = edict(yaml.safe_load(EXAMPLE.read_text(encoding="utf-8"))["models"][0]) | |
| validate_models([ours]) | |
| assert VllmRecipe.from_model(ours).hf_model | |
| def test_a_recipe_needs_the_template_for_our_rentals(): | |
| row = edict(yaml.safe_load(EXAMPLE.read_text(encoding="utf-8"))["models"][0]) | |
| row.pop("litellm_params") | |
| with pytest.raises(ValueError, match=re.escape("litellm_params.model is required")): | |
| validate_models([row]) | |
| def test_a_recipe_is_served_as_hosted_vllm_and_nothing_else(): | |
| """openai/<name> would make LiteLLM talk to vLLM as something it is not.""" | |
| row = edict(yaml.safe_load(EXAMPLE.read_text(encoding="utf-8"))["models"][0]) | |
| row.litellm_params.model = "openai/gemma4-26b" | |
| with pytest.raises( | |
| ValueError, match=re.escape("must be hosted_vllm/<name> for a vLLM recipe, not openai/gemma4-26b") | |
| ): | |
| validate_models([row]) | |
| row.litellm_params.model = "hosted_vllm/gemma4-26b" | |
| validate_models([row]) | |
| def test_deprecated_keys_are_warned_and_not_refused(caplog): | |
| row = edict( | |
| { | |
| "model_name": "old-style", | |
| "litellm_params": {"model": "xai/grok-4", "api_base": "https://api.x.ai/v1", "api_key": "secret"}, | |
| "model_info": {"provider": "xai", "self_hosted": False}, | |
| } | |
| ) | |
| validate_models([row]) | |
| with caplog.at_level("WARNING", logger="llmao.models"): | |
| warn_deprecated([row]) | |
| text = caplog.text | |
| for key in ("litellm_params.api_base", "litellm_params.api_key", "model_info.provider", "model_info.self_hosted"): | |
| assert f"old-style: {key} is ignored" in text | |
| # Says where the value went, and never repeats the value itself. | |
| assert "config.yaml providers" in text | |
| assert "secret" not in text | |
| def test_the_committed_models_yaml_uses_no_deprecated_key(caplog): | |
| with caplog.at_level("WARNING", logger="llmao.models"): | |
| warn_deprecated(load_models(EXAMPLE)) | |
| assert caplog.text == "" | |
| def test_model_in_service(): | |
| assert model_in_service("available") is True | |
| assert model_in_service("degraded") is True | |
| assert model_in_service("unavailable") is False | |
| assert model_in_service("") is False | |
| def test_litellm_deployment_preserves_model_info_extras(): | |
| """Regression: LiteLLM Deployment/ModelInfo must accept ASF metadata fields. | |
| Inventory UX stores governance fields under model_info (flat). If a future | |
| litellm version rejects extras, this test fails and we must revisit. | |
| """ | |
| entry = { | |
| "model_name": "gemma4-26b", | |
| "litellm_params": { | |
| "model": "openai/gemma4-26b", | |
| "api_base": "http://127.0.0.1:8001", | |
| "api_key": "sk-test", | |
| }, | |
| "model_info": { | |
| "display_name": "Gemma test", | |
| "license": "Apache-2.0", | |
| "openness": "open-weight", | |
| "weights_distribution": "google/gemma", | |
| "training_data_provenance": "undisclosed", | |
| "provenance_record": "absent", | |
| "self_hosted": True, | |
| "supports_thinking": True, | |
| "thinks_by_default": False, | |
| "notes": "regression probe", | |
| }, | |
| } | |
| dep = Deployment(**entry) | |
| info = dep.model_info | |
| assert info is not None | |
| assert getattr(info, "display_name", None) == "Gemma test" | |
| assert getattr(info, "license", None) == "Apache-2.0" | |
| assert getattr(info, "openness", None) == "open-weight" | |
| assert getattr(info, "provenance_record", None) == "absent" | |
| assert getattr(info, "self_hosted", None) is True | |
| assert getattr(info, "supports_thinking", None) is True | |
| def test_roundtrip_yaml_through_deployment(): | |
| """models.yaml rows must construct as LiteLLM Deployments.""" | |
| data = yaml.safe_load(EXAMPLE.read_text(encoding="utf-8")) | |
| for entry in data["models"]: | |
| # Deployment requires api_key etc.; example has CHANGE_ME placeholders — fine. | |
| dep = Deployment(**entry) | |
| assert dep.model_name | |
| assert dep.model_info is not None | |
| assert getattr(dep.model_info, "license", None) |