File size: 7,018 Bytes
f0013ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f6b4a0
 
 
 
f0013ff
2f6b4a0
f0013ff
 
 
 
 
 
3f9808f
2f6b4a0
 
3f9808f
 
f0013ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f9808f
2f6b4a0
 
 
3f9808f
 
 
 
 
 
 
2f6b4a0
 
3f9808f
 
 
 
 
 
 
 
 
 
 
2f6b4a0
 
3f9808f
 
 
 
 
 
 
 
 
 
 
 
 
f0013ff
3f9808f
f0013ff
3f9808f
 
f0013ff
 
 
 
 
 
 
 
3f9808f
f0013ff
 
 
 
 
 
 
 
 
 
2f6b4a0
 
 
 
 
 
f0013ff
 
 
 
 
 
3f9808f
 
 
 
 
 
 
 
2f6b4a0
 
 
 
 
 
 
3f9808f
 
 
 
 
 
 
 
 
 
 
 
f0013ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f9808f
 
 
 
 
 
f0013ff
3f9808f
 
f0013ff
 
 
 
 
 
3f9808f
f0013ff
3f9808f
f0013ff
3f9808f
 
f0013ff
 
 
 
 
 
 
3f9808f
f0013ff
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from __future__ import annotations

import logging
import os
import shlex
import signal
import subprocess
import sys
import time
from urllib.error import URLError
from urllib.request import Request, urlopen

import uvicorn

from app.config import Settings, get_settings


logger = logging.getLogger(__name__)


def _existing_model_path(path: str) -> str:
    return path if path and os.path.isfile(path) else ""


def build_llama_cpp_command(settings: Settings) -> list[str]:
    model_path = _existing_model_path(settings.llama_cpp_model_path)
    command = [
        settings.llama_cpp_server_bin,
        "--host",
        settings.llama_cpp_host,
        "--port",
        str(settings.llama_cpp_port),
        *(
            ["--model", model_path]
            if model_path
            else ["-hf", settings.llama_cpp_model_ref]
        ),
        "--ctx-size",
        str(settings.llama_cpp_ctx_size),
        "--n-gpu-layers",
        str(settings.llama_cpp_gpu_layers),
        "--threads",
        str(settings.llama_cpp_threads),
        "--parallel",
        str(settings.llama_cpp_parallel),
    ]
    if settings.llama_cpp_api_key:
        command.extend(["--api-key", settings.llama_cpp_api_key])
    if settings.llama_cpp_extra_args:
        command.extend(shlex.split(settings.llama_cpp_extra_args))
    return command


def build_vision_llama_cpp_command(settings: Settings) -> list[str]:
    model_path = _existing_model_path(settings.vision_llama_cpp_model_path)
    mmproj_path = _existing_model_path(settings.vision_llama_cpp_mmproj_path)
    use_persistent_files = bool(model_path and mmproj_path)
    command = [
        settings.llama_cpp_server_bin,
        "--host",
        settings.llama_cpp_host,
        "--port",
        str(settings.vision_llama_cpp_port),
        *(
            ["--model", model_path]
            if use_persistent_files
            else ["-hf", settings.vision_llama_cpp_model_ref]
        ),
        "--ctx-size",
        str(settings.vision_llama_cpp_ctx_size),
        "--n-gpu-layers",
        str(settings.vision_llama_cpp_gpu_layers),
        "--threads",
        str(settings.llama_cpp_threads),
        "--parallel",
        "1",
    ]
    if use_persistent_files:
        command.extend(["--mmproj", mmproj_path])
    if settings.vision_llm_api_key:
        command.extend(["--api-key", settings.vision_llm_api_key])
    if settings.vision_llama_cpp_extra_args:
        command.extend(shlex.split(settings.vision_llama_cpp_extra_args))
    return command


def wait_for_llama_cpp(
    settings: Settings,
    process: subprocess.Popen,
    base_url: str,
    api_key: str,
) -> None:
    deadline = time.monotonic() + settings.llama_cpp_startup_timeout
    health_url = f"{base_url}/health"
    headers = {}
    if api_key:
        headers["Authorization"] = f"Bearer {api_key}"

    while time.monotonic() < deadline:
        return_code = process.poll()
        if return_code is not None:
            raise RuntimeError(f"llama-server exited during startup with code {return_code}.")
        try:
            with urlopen(Request(health_url, headers=headers), timeout=5) as response:
                if response.status < 500:
                    logger.info("Embedded llama.cpp is ready at %s", base_url)
                    return
        except (OSError, URLError):
            time.sleep(2)
    raise TimeoutError(
        f"llama-server did not become ready within {settings.llama_cpp_startup_timeout}s."
    )


def start_embedded_llama_cpp(settings: Settings) -> subprocess.Popen:
    command = build_llama_cpp_command(settings)
    if settings.llama_cpp_model_path and "-hf" in command:
        logger.warning(
            "Text model file is missing at %s; downloading %s instead.",
            settings.llama_cpp_model_path,
            settings.llama_cpp_model_ref,
        )
    printable = [
        "<redacted>" if part == settings.llama_cpp_api_key else part
        for part in command
    ]
    logger.info("Starting embedded llama.cpp: %s", " ".join(printable))
    process = subprocess.Popen(command, start_new_session=True)
    wait_for_llama_cpp(
        settings, process, settings.llm_base_url, settings.llama_cpp_api_key
    )
    return process


def start_embedded_vision_llama_cpp(settings: Settings) -> subprocess.Popen:
    command = build_vision_llama_cpp_command(settings)
    if settings.vision_llama_cpp_model_path and "-hf" in command:
        logger.warning(
            "Vision model or projector is missing at %s / %s; downloading %s instead.",
            settings.vision_llama_cpp_model_path,
            settings.vision_llama_cpp_mmproj_path,
            settings.vision_llama_cpp_model_ref,
        )
    printable = [
        "<redacted>" if part == settings.vision_llm_api_key else part
        for part in command
    ]
    logger.info("Starting embedded vision llama.cpp: %s", " ".join(printable))
    process = subprocess.Popen(command, start_new_session=True)
    wait_for_llama_cpp(
        settings,
        process,
        settings.vision_llm_base_url,
        settings.vision_llm_api_key,
    )
    return process


def stop_process(process: subprocess.Popen | None) -> None:
    if process is None or process.poll() is not None:
        return
    try:
        os.killpg(process.pid, signal.SIGTERM)
        process.wait(timeout=15)
    except (ProcessLookupError, subprocess.TimeoutExpired):
        try:
            os.killpg(process.pid, signal.SIGKILL)
        except ProcessLookupError:
            pass


def main() -> None:
    logging.basicConfig(
        level=os.getenv("LOG_LEVEL", "INFO").upper(),
        format="%(asctime)s %(levelname)s %(name)s: %(message)s",
    )
    settings = get_settings()
    if settings.llm_runtime not in {
        "mock",
        "external",
        "embedded_llamacpp",
        "embedded_dual_llamacpp",
    }:
        raise ValueError(
            "LLM_RUNTIME must be mock, external, embedded_llamacpp, or "
            "embedded_dual_llamacpp; "
            f"received {settings.llm_runtime!r}."
        )
    if settings.llm_runtime == "external" and not settings.llm_base_url:
        raise ValueError("LLM_BASE_URL is required when LLM_RUNTIME=external.")

    model_process: subprocess.Popen | None = None
    vision_process: subprocess.Popen | None = None
    try:
        if settings.llm_runtime in {"embedded_llamacpp", "embedded_dual_llamacpp"}:
            model_process = start_embedded_llama_cpp(settings)
        if settings.llm_runtime == "embedded_dual_llamacpp":
            vision_process = start_embedded_vision_llama_cpp(settings)
        uvicorn.run(
            "main:app",
            host=os.getenv("HOST", "0.0.0.0"),
            port=int(os.getenv("PORT", "7860")),
            log_level=os.getenv("LOG_LEVEL", "info").lower(),
        )
    finally:
        stop_process(vision_process)
        stop_process(model_process)


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        sys.exit(130)