File size: 3,388 Bytes
300911c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
727cb75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300911c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
727cb75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd75839
727cb75
 
300911c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from pathlib import Path

import pytest

from inference.config import load_app_config


def test_load_app_config_from_models_yaml(tmp_path, monkeypatch):
    presets = tmp_path / "models.yaml"
    presets.write_text(
        """
defaults:
  active_model: demo
  allow_model_switch: true
models:
  demo:
    label: Demo preset
    backend: llama_cpp
    model_repo: org/model-GGUF
    model_file: demo.gguf
"""
    )
    monkeypatch.chdir(tmp_path)
    monkeypatch.delenv("ACTIVE_MODEL", raising=False)

    config = load_app_config()

    assert config.active_model == "demo"
    assert config.allow_model_switch is True
    assert config.get_model("demo").model_repo == "org/model-GGUF"


def test_allow_model_switch_defaults_true_without_env(tmp_path, monkeypatch):
    presets = tmp_path / "models.yaml"
    presets.write_text(
        """
defaults:
  active_model: demo
models:
  demo:
    label: Demo preset
    backend: llama_cpp
    model_repo: org/model-GGUF
    model_file: demo.gguf
"""
    )
    monkeypatch.chdir(tmp_path)
    monkeypatch.delenv("ALLOW_MODEL_SWITCH", raising=False)

    config = load_app_config()

    assert config.allow_model_switch is True


def test_allow_model_switch_env_false_overrides(tmp_path, monkeypatch):
    presets = tmp_path / "models.yaml"
    presets.write_text(
        """
defaults:
  active_model: demo
  allow_model_switch: true
models:
  demo:
    label: Demo preset
    backend: llama_cpp
    model_repo: org/model-GGUF
    model_file: demo.gguf
"""
    )
    monkeypatch.chdir(tmp_path)
    monkeypatch.setenv("ALLOW_MODEL_SWITCH", "false")

    config = load_app_config()

    assert config.allow_model_switch is False


def test_legacy_env_overrides_active_preset(tmp_path, monkeypatch):
    presets = tmp_path / "models.yaml"
    presets.write_text(
        """
defaults:
  active_model: demo
models:
  demo:
    label: Demo
    backend: llama_cpp
    model_repo: org/original
    model_file: original.gguf
"""
    )
    monkeypatch.chdir(tmp_path)
    monkeypatch.setenv("MODEL_REPO", "org/override")
    monkeypatch.setenv("MODEL_FILE", "override.gguf")

    model = load_app_config().get_model("demo")

    assert model.model_repo == "org/override"
    assert model.model_file == "override.gguf"


def test_minicpm_v_gguf_preset_from_repo(monkeypatch):
    repo_root = Path(__file__).resolve().parents[3]
    models_yaml = repo_root / "models.yaml"
    if not models_yaml.is_file():
        pytest.skip("repo models.yaml not found")

    monkeypatch.chdir(repo_root)
    monkeypatch.delenv("ACTIVE_MODEL", raising=False)
    monkeypatch.delenv("ALLOW_MODEL_SWITCH", raising=False)

    model = load_app_config().get_model("minicpm-v-4.6-gguf")

    assert model.backend == "llama_cpp"
    assert model.multimodal is True
    assert model.model_repo == "openbmb/MiniCPM-V-4.6-gguf"
    assert model.model_file == "MiniCPM-V-4_6-Q4_K_M.gguf"


def test_resolve_relative_model_path(tmp_path, monkeypatch):
    local_dir = tmp_path / "gemma_merged_model"
    local_dir.mkdir()
    presets = tmp_path / "models.yaml"
    presets.write_text(
        f"""
defaults:
  active_model: local
models:
  local:
    label: Local merged
    backend: transformers
    model_id: ./{local_dir.name}
"""
    )
    monkeypatch.chdir(tmp_path)

    model = load_app_config().get_model("local")

    assert model.model_id == str(local_dir.resolve())