-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
403 lines (364 loc) · 11.8 KB
/
server.go
File metadata and controls
403 lines (364 loc) · 11.8 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"time"
)
// llamaServer wraps a long-running llama-server subprocess and the HTTP
// client that talks to it. One instance per (model, context-size). The
// model stays loaded in memory so subsequent /completion calls don't pay
// the GGUF mmap + warmup cost on every turn.
type llamaServer struct {
cmd *exec.Cmd
port int
model Model
ctxN int
client *http.Client
waitOnce sync.Once
waitErr chan error
}
var (
serverMu sync.Mutex
activeServer *llamaServer
)
// ensureServer returns a running llamaServer for the current model, spawning
// one if needed and restarting if the caller switched models. Safe to call
// concurrently.
func ensureServer() (*llamaServer, error) {
serverMu.Lock()
defer serverMu.Unlock()
m, err := currentModel()
if err != nil {
return nil, err
}
if activeServer != nil && activeServer.model.Name == m.Name {
return activeServer, nil
}
if activeServer != nil {
activeServer.stopLocked()
activeServer = nil
}
s, err := startLlamaServer(m)
if err != nil {
return nil, err
}
activeServer = s
return s, nil
}
// shutdownServer is called from startChat's defer so the subprocess doesn't
// outlive the TUI session.
func shutdownServer() {
serverMu.Lock()
defer serverMu.Unlock()
if activeServer != nil {
activeServer.stopLocked()
activeServer = nil
}
}
func startLlamaServer(m Model) (*llamaServer, error) {
bin, err := findEngineServer()
if err != nil {
return nil, fmt.Errorf("llama-server: %w", err)
}
modelPath, err := requireModel(m)
if err != nil {
return nil, err
}
port, err := pickFreePort()
if err != nil {
return nil, fmt.Errorf("pick port: %w", err)
}
threads := runtime.NumCPU() - 1
if threads < 1 {
threads = 1
}
if threads > 6 {
threads = 6
}
args := []string{
"-m", modelPath,
"--host", "127.0.0.1",
"--port", fmt.Sprintf("%d", port),
"-c", "16384",
"-t", fmt.Sprintf("%d", threads),
"-ngl", "0",
"--log-disable",
}
cmd := exec.Command(bin, args...)
cmd.Stdin = bytes.NewReader(nil)
cmd.Stdout = newLogWriter("llama-server stdout")
cmd.Stderr = newLogWriter("llama-server stderr")
cmd.Env = append(os.Environ(), "OMP_STACKSIZE=64M")
applyEngineSysProcAttr(cmd)
log.Printf("starting llama-server on :%d (model=%s threads=%d)", port, m.Name, threads)
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("start llama-server: %w", err)
}
s := &llamaServer{
cmd: cmd,
port: port,
model: m,
ctxN: 16384,
client: &http.Client{Timeout: 10 * time.Minute},
waitErr: make(chan error, 1),
}
// Single background Wait(); result is broadcast via waitErr so both
// waitReady and stopLocked can observe exit without double-calling Wait.
go func() { s.waitErr <- cmd.Wait() }()
if err := s.waitReady(90 * time.Second); err != nil {
s.stopLocked()
return nil, err
}
log.Printf("llama-server ready on :%d", port)
return s, nil
}
func pickFreePort() (int, error) {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
// waitReady polls GET /health until the server reports ready, or gives up.
// If the subprocess exits early, returns that error instead of timing out.
func (s *llamaServer) waitReady(maxWait time.Duration) error {
deadline := time.Now().Add(maxWait)
url := fmt.Sprintf("http://127.0.0.1:%d/health", s.port)
for time.Now().Before(deadline) {
select {
case err := <-s.waitErr:
// Process exited before becoming ready — put the error back for
// stopLocked(), then report to caller.
s.waitErr <- err
return fmt.Errorf("llama-server exited before ready: %v", err)
default:
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
resp, err := s.client.Do(req)
cancel()
if err == nil {
body := make([]byte, 256)
n, _ := resp.Body.Read(body)
resp.Body.Close()
if resp.StatusCode == 200 && bytes.Contains(body[:n], []byte(`"ok"`)) {
return nil
}
}
time.Sleep(250 * time.Millisecond)
}
return fmt.Errorf("llama-server did not become ready in %s", maxWait)
}
func (s *llamaServer) stopLocked() {
if s.cmd == nil || s.cmd.Process == nil {
return
}
log.Printf("stopping llama-server pid=%d", s.cmd.Process.Pid)
_ = s.cmd.Process.Kill()
// Drain the background Wait goroutine so resources are released.
select {
case <-s.waitErr:
case <-time.After(5 * time.Second):
}
}
// ChatMsg is a single turn passed to /v1/chat/completions. Role is one of
// "system", "user", "assistant", or "tool". The tool-call fields only apply
// to assistant messages (ToolCalls) and tool messages (Name, ToolCallID).
type ChatMsg struct {
Role string `json:"role"`
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
Name string `json:"name,omitempty"`
}
// ToolCall is one requested function invocation emitted by the assistant.
// Arguments is a JSON-encoded string per the OpenAI schema, not a decoded
// object — the caller is responsible for unmarshalling.
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ToolCallFunction `json:"function"`
}
type ToolCallFunction struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}
type chatRequest struct {
Messages []ChatMsg `json:"messages"`
MaxTokens int `json:"max_tokens"`
Temperature float64 `json:"temperature"`
Stream bool `json:"stream"`
CachePrompt bool `json:"cache_prompt"`
Tools []map[string]any `json:"tools,omitempty"`
}
type chatChoice struct {
Message assistantMessage `json:"message"`
FinishReason string `json:"finish_reason"`
}
// assistantMessage is the response-side shape of an assistant reply. Content
// can be null when the model chose to emit tool_calls instead.
type assistantMessage struct {
Role string `json:"role"`
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}
type chatUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
type chatResponse struct {
Choices []chatChoice `json:"choices"`
Usage chatUsage `json:"usage"`
Error struct {
Message string `json:"message"`
} `json:"error"`
}
// UsageStats mirrors the last /v1/chat/completions usage block so the TUI
// can render a context-usage indicator. Total == prompt + completion.
type UsageStats struct {
PromptTokens int
CompletionTokens int
TotalTokens int
ContextSize int
}
var (
lastUsageMu sync.RWMutex
lastUsage UsageStats
)
func setLastUsage(u UsageStats) {
lastUsageMu.Lock()
lastUsage = u
lastUsageMu.Unlock()
}
// GetLastUsage returns the most recent token usage reported by
// llama-server, including the active context-window size. ContextSize
// defaults to 0 until the first request completes.
func GetLastUsage() UsageStats {
lastUsageMu.RLock()
defer lastUsageMu.RUnlock()
return lastUsage
}
// ResetUsage zeroes the usage counter (e.g. after /reset). Preserves the
// context-size hint so the TUI can still render the denominator.
func ResetUsage() {
lastUsageMu.Lock()
ctx := lastUsage.ContextSize
lastUsage = UsageStats{ContextSize: ctx}
lastUsageMu.Unlock()
}
// ChatComplete sends a chat-style request to the running server. llama-server
// applies the model's own chat template from the GGUF metadata (Gemma-3's
// <start_of_turn>/<end_of_turn> sentinels, ChatML for other families, etc.)
// and returns only the assistant's reply — so the model stops at the turn
// boundary instead of spewing fake "User:/Assistant:" continuations.
func (s *llamaServer) ChatComplete(msgs []ChatMsg, maxTokens int) (string, error) {
content, _, err := s.chatCompleteCore(msgs, maxTokens, nil)
return content, err
}
// ChatCompleteWithTools is the tool-enabled variant: advertises `tools` on
// the request and returns the (possibly empty) list of tool_calls the model
// emitted in addition to any assistant content. The caller is responsible
// for executing the calls and re-invoking with the tool results appended.
func (s *llamaServer) ChatCompleteWithTools(msgs []ChatMsg, tools []map[string]any, maxTokens int) (string, []ToolCall, error) {
return s.chatCompleteCore(msgs, maxTokens, tools)
}
func (s *llamaServer) chatCompleteCore(msgs []ChatMsg, maxTokens int, tools []map[string]any) (string, []ToolCall, error) {
reqBody, _ := json.Marshal(chatRequest{
Messages: msgs,
MaxTokens: maxTokens,
Temperature: 0.2,
Stream: false,
CachePrompt: true,
Tools: tools,
})
log.Printf("→ POST /v1/chat/completions port=%d max_tokens=%d msgs=%d", s.port, maxTokens, len(msgs))
for i, m := range msgs {
log.Printf(" msg[%d] %s (%d bytes): %s", i, m.Role, len(m.Content), truncateForLog(m.Content, 500))
}
url := fmt.Sprintf("http://127.0.0.1:%d/v1/chat/completions", s.port)
start := time.Now()
resp, err := s.client.Post(url, "application/json", bytes.NewReader(reqBody))
if err != nil {
return "", nil, fmt.Errorf("POST /v1/chat/completions: %w", err)
}
defer resp.Body.Close()
raw, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return "", nil, fmt.Errorf("read chat completion body: %w", readErr)
}
log.Printf("← HTTP %d in %s (body=%d bytes)", resp.StatusCode, time.Since(start), len(raw))
log.Printf(" body: %s", truncateForLog(string(raw), 2000))
if resp.StatusCode != 200 {
return "", nil, fmt.Errorf("llama-server HTTP %d: %s", resp.StatusCode, truncateForLog(string(raw), 300))
}
var cr chatResponse
if err := json.Unmarshal(raw, &cr); err != nil {
return "", nil, fmt.Errorf("decode chat completion: %w", err)
}
if len(cr.Choices) == 0 {
return "", nil, fmt.Errorf("empty chat completion response")
}
msg := cr.Choices[0].Message
content := msg.Content
if content == "" && len(msg.ToolCalls) == 0 && cr.Choices[0].FinishReason == "length" {
log.Printf(" WARNING: reply was empty and finish_reason=length — max_tokens=%d likely too small", maxTokens)
}
setLastUsage(UsageStats{
PromptTokens: cr.Usage.PromptTokens,
CompletionTokens: cr.Usage.CompletionTokens,
TotalTokens: cr.Usage.TotalTokens,
ContextSize: s.ctxN,
})
return content, msg.ToolCalls, nil
}
// DropKVCache asks llama-server to forget any cached prompt prefix so the
// next request starts from a clean slate. Called from the TUI's /reset so
// the server doesn't silently reuse tokens from the previous conversation.
func (s *llamaServer) DropKVCache() error {
url := fmt.Sprintf("http://127.0.0.1:%d/slots/0?action=erase", s.port)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
return err
}
resp, err := s.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
// Older llama-server builds don't expose this; don't treat it as fatal.
log.Printf("/slots erase returned %d (probably unsupported on this llama-server)", resp.StatusCode)
return nil
}
return nil
}
func truncateForLog(s string, n int) string {
// Collapse newlines so each log line stays one line.
s = strings.ReplaceAll(s, "\r", " ")
s = strings.ReplaceAll(s, "\n", " ⏎ ")
if len(s) > n {
return s[:n] + "…(+" + fmt.Sprintf("%d", len(s)-n) + " more)"
}
return s
}
// logWriter forwards subprocess stdout/stderr into the atlas.llm log with a
// tag prefix so server messages show up alongside our own.
type logWriter struct{ tag string }
func newLogWriter(tag string) *logWriter { return &logWriter{tag: tag} }
func (w *logWriter) Write(p []byte) (int, error) {
log.Printf("[%s] %s", w.tag, bytes.TrimRight(p, "\r\n "))
return len(p), nil
}