ad074cea31
Loading a different gguf was a one-line edit to phraser.model_path plus a
restart. It is now an owner-triggered IPC call, off unless configured.
internal/phraser/swap.go holds the safety properties as code:
- Never two models resident. The old llama-server is killed and reaped
before the new one is launched. One 1.7B fits the Vega iGPU; a
blue/green overlap would OOM the box, so it is not offered.
- Atomic from a turn's point of view. Swap drains the in-flight turns
(they finish on the old model), then refuses arrivals with ErrSwapping
until the new server has answered /v1/models. No turn ever sees half a
swap; refused turns fall back to the classifier cascade.
- A failed load rolls back. If the new model does not start or does not
probe, the previous one is reloaded and the call returns RolledBack
with the error. If the rollback also fails the daemon says so and
degrades to the classifier rather than pretending to serve.
Holders of the completion client are re-pointed, not rebuilt: llm.Client
guards its base URL and LLMPhraser.OnSwap re-points it, so the router, the
replier, the mail extractor and the memory evaluator follow the new port
without knowing a swap happened.
Reach is deliberately narrow. phraser.swap_models is an exact-match
allowlist of absolute paths a human wrote, rejected at startup otherwise,
so "swap the model" can never mean "load any file on my disk"; the running
model is always swappable back to. MethodSwapModel is AuthStepUp, the same
rung as mutating the tool allowlist, and /models gates POST through the
same stepUpOK the tools page uses. Nothing calls Swap on a timer and no
act, intent or utterance reaches it.
Vikunja #250
118 lines
3.7 KiB
Go
118 lines
3.7 KiB
Go
// Package llm is the shared llama-server completion client — one seam both the
|
|
// phraser (talking back) and the router (routing) call. It does NOT spawn the
|
|
// server; the daemon owns one llama-server (spawned by the phraser) and hands
|
|
// its base URL here, so a single resident model serves both callers.
|
|
package llm
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type Client struct {
|
|
// mu guards base only. The base URL changes when the daemon swaps the
|
|
// resident model (Vikunja #250): llama-server is relaunched on a fresh
|
|
// port, and every holder of this client — the LLM router, the replier, the
|
|
// mail extractor — must follow without being rebuilt. One mutexed field is
|
|
// the whole mechanism; a swap re-points the client, it does not replace it.
|
|
mu sync.RWMutex
|
|
base string
|
|
http *http.Client
|
|
}
|
|
|
|
func New(baseURL string, timeout time.Duration) *Client {
|
|
return &Client{base: baseURL, http: &http.Client{Timeout: timeout}}
|
|
}
|
|
|
|
// SetBaseURL re-points the client at another llama-server. Safe to call while
|
|
// requests are in flight: a request that already read the old base finishes
|
|
// against the old base (or fails, and every caller of Complete has a fallback),
|
|
// and the next one uses the new base. It is deliberately NOT a queue-and-retry —
|
|
// the phraser quiesces around a swap, so the window is small and a lost turn
|
|
// degrades to the classifier rather than hanging.
|
|
func (c *Client) SetBaseURL(base string) {
|
|
c.mu.Lock()
|
|
c.base = base
|
|
c.mu.Unlock()
|
|
}
|
|
|
|
// BaseURL is the server this client currently talks to.
|
|
func (c *Client) BaseURL() string {
|
|
c.mu.RLock()
|
|
defer c.mu.RUnlock()
|
|
return c.base
|
|
}
|
|
|
|
type Req struct {
|
|
System string
|
|
User string
|
|
Grammar string // GBNF; empty ⇒ unconstrained
|
|
MaxTokens int
|
|
// RepeatPenalty > 0 ⇒ penalize token repetition (curbs the sub-1B "тоже
|
|
// тоже тоже" loop). 0 ⇒ server default (no extra penalty).
|
|
RepeatPenalty float64
|
|
// Stop — sequences that end generation early (e.g. newline for a one-liner).
|
|
Stop []string
|
|
}
|
|
|
|
type msg struct {
|
|
Role string `json:"role"`
|
|
Content string `json:"content"`
|
|
Reasoning string `json:"reasoning,omitempty"`
|
|
ReasoningContent string `json:"reasoning_content,omitempty"`
|
|
}
|
|
type body struct {
|
|
Messages []msg `json:"messages"`
|
|
MaxTokens int `json:"max_tokens,omitempty"`
|
|
Grammar string `json:"grammar,omitempty"`
|
|
Temp float64 `json:"temperature"`
|
|
RepeatPenalty float64 `json:"repeat_penalty,omitempty"`
|
|
Stop []string `json:"stop,omitempty"`
|
|
}
|
|
type resp struct {
|
|
Choices []struct {
|
|
Message msg `json:"message"`
|
|
} `json:"choices"`
|
|
}
|
|
|
|
func (c *Client) Complete(ctx context.Context, r Req) (string, error) {
|
|
b, _ := json.Marshal(body{
|
|
Messages: []msg{{Role: "system", Content: r.System}, {Role: "user", Content: r.User}},
|
|
MaxTokens: r.MaxTokens,
|
|
Grammar: r.Grammar,
|
|
Temp: 0,
|
|
RepeatPenalty: r.RepeatPenalty,
|
|
Stop: r.Stop,
|
|
})
|
|
req, err := http.NewRequestWithContext(ctx, "POST", c.BaseURL()+"/v1/chat/completions", bytes.NewReader(b))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
httpResp, err := c.http.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer httpResp.Body.Close()
|
|
if httpResp.StatusCode != 200 {
|
|
return "", fmt.Errorf("llm: status %d", httpResp.StatusCode)
|
|
}
|
|
var out resp
|
|
if err := json.NewDecoder(httpResp.Body).Decode(&out); err != nil {
|
|
return "", err
|
|
}
|
|
if len(out.Choices) == 0 {
|
|
return "", fmt.Errorf("llm: no choices")
|
|
}
|
|
content := out.Choices[0].Message.Content
|
|
if content == "" {
|
|
content = out.Choices[0].Message.ReasoningContent
|
|
}
|
|
return content, nil
|
|
}
|