-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllama_cpp_standalone.py
More file actions
265 lines (215 loc) · 7.85 KB
/
llama_cpp_standalone.py
File metadata and controls
265 lines (215 loc) · 7.85 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python3
"""
llama.cpp Python Standalone - Simple wrapper for llama.cpp server binary.
Author: Gregor Koch (@cronos3k)
License: MIT
Use this when llama-cpp-python doesn't support your model architecture yet.
Provides OpenAI-compatible API via the official llama.cpp server.
Example:
server = LlamaCppServer("/path/to/llama-server")
server.start(model_path="model.gguf", port=8080)
# Use with OpenAI client at http://localhost:8080/v1
server.stop()
"""
import subprocess
import time
import signal
import atexit
from pathlib import Path
from typing import Optional, List
import requests
class LlamaCppServer:
"""
Manages llama.cpp server binary lifecycle from Python.
This wrapper launches the official llama.cpp server and provides
a simple Python interface for starting, stopping, and health checking.
"""
def __init__(self, binary_path: str = "llama-server"):
"""
Initialize server manager.
Args:
binary_path: Path to llama-server binary. Can be absolute path
or command name if in PATH.
"""
self.binary_path = binary_path
self.process: Optional[subprocess.Popen] = None
self.port: Optional[int] = None
# Verify binary exists
if not self._check_binary():
raise FileNotFoundError(
f"llama-server binary not found at: {binary_path}\n"
"Build it with: scripts/build_llama_cpp.sh"
)
# Ensure cleanup on exit
atexit.register(self.stop)
def _check_binary(self) -> bool:
"""Check if llama-server binary exists and is executable."""
try:
result = subprocess.run(
[self.binary_path, "--help"],
capture_output=True,
timeout=5
)
return result.returncode == 0
except (FileNotFoundError, subprocess.TimeoutExpired):
return False
def start(
self,
model_path: str,
port: int = 8080,
host: str = "0.0.0.0",
n_gpu_layers: int = -1,
n_ctx: int = 4096,
n_batch: int = 512,
mmproj_path: Optional[str] = None,
extra_args: Optional[List[str]] = None,
timeout: int = 60
) -> bool:
"""
Start llama.cpp server with specified configuration.
Args:
model_path: Path to GGUF model file
port: Port to listen on (default: 8080)
host: Host to bind to (default: 0.0.0.0)
n_gpu_layers: GPU layers to offload. -1 = all (default: -1)
n_ctx: Context window size (default: 4096)
n_batch: Batch size for prompt processing (default: 512)
mmproj_path: Path to multimodal projector for vision models
extra_args: Additional arguments to pass to server
timeout: Seconds to wait for server startup (default: 60)
Returns:
True if server started successfully
Raises:
FileNotFoundError: If model file doesn't exist
RuntimeError: If server fails to start
TimeoutError: If server doesn't respond within timeout
"""
if self.process is not None:
raise RuntimeError("Server already running. Call stop() first.")
# Verify model exists
if not Path(model_path).exists():
raise FileNotFoundError(f"Model not found: {model_path}")
if mmproj_path and not Path(mmproj_path).exists():
raise FileNotFoundError(f"Multimodal projector not found: {mmproj_path}")
# Build command
cmd = [
self.binary_path,
"-m", model_path,
"--port", str(port),
"--host", host,
"--n-gpu-layers", str(n_gpu_layers),
"-c", str(n_ctx),
"-b", str(n_batch),
]
if mmproj_path:
cmd.extend(["--mmproj", mmproj_path])
if extra_args:
cmd.extend(extra_args)
print(f"Starting llama.cpp server...")
print(f"Command: {' '.join(cmd)}")
# Start process
self.process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
self.port = port
# Wait for server to be ready
print(f"Waiting for server on port {port}...")
start_time = time.time()
while time.time() - start_time < timeout:
# Check if process crashed
if self.process.poll() is not None:
stdout = self.process.stdout.read() if self.process.stdout else ""
raise RuntimeError(
f"Server process exited with code {self.process.returncode}\n"
f"Output: {stdout}"
)
# Check health
if self.check_health():
print(f"✓ Server ready at http://{host}:{port}")
print(f"✓ OpenAI-compatible API: http://{host}:{port}/v1")
return True
time.sleep(1)
# Timeout - kill process
self.stop()
raise TimeoutError(f"Server did not start within {timeout} seconds")
def stop(self) -> bool:
"""
Stop the llama.cpp server gracefully.
Returns:
True if stopped successfully
"""
if self.process is None:
return True
print("Stopping llama.cpp server...")
try:
# Try graceful shutdown first
self.process.terminate()
self.process.wait(timeout=10)
print("✓ Server stopped")
except subprocess.TimeoutExpired:
# Force kill if needed
print("Force killing server...")
self.process.kill()
self.process.wait()
print("✓ Server killed")
finally:
self.process = None
self.port = None
return True
def check_health(self) -> bool:
"""
Check if server is responding.
Returns:
True if server is healthy
"""
if self.port is None:
return False
try:
response = requests.get(
f"http://localhost:{self.port}/health",
timeout=2
)
return response.status_code == 200
except (requests.RequestException, Exception):
return False
def get_endpoint(self) -> str:
"""
Get the OpenAI-compatible API endpoint URL.
Returns:
API endpoint URL (e.g., "http://localhost:8080/v1")
Raises:
RuntimeError: If server is not running
"""
if self.port is None:
raise RuntimeError("Server not running")
return f"http://localhost:{self.port}/v1"
def __enter__(self):
"""Context manager support."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager cleanup."""
self.stop()
def main():
"""Example usage."""
import sys
if len(sys.argv) < 2:
print("Usage: python llama_cpp_standalone.py <model.gguf> [port]")
sys.exit(1)
model_path = sys.argv[1]
port = int(sys.argv[2]) if len(sys.argv) > 2 else 8080
# Example: Start server and keep it running
server = LlamaCppServer()
server.start(model_path=model_path, port=port)
print("\nServer is running. Press Ctrl+C to stop...")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nShutting down...")
server.stop()
if __name__ == "__main__":
main()