feat: LLM phraser, shared LLM client, and LLM replier

- Add llmphraser: LFM-based phraser implementing Phraser interface with
  PhraseChat, PhraseNudge, PhraseReactive, and PhraseReminder methods.
- Add shared internal/llm/client: llama-server completion client used by
  both the phraser (talking back) and router (routing), sharing one model.
- Add LLMReplier in mavend: replaces StubReplier for chat/nudge/reactive
  replies, falls back to stub on model errors.
- Update Phraser interface: add PhraseChat method, update stub to match.
- Wire LLM phaser into mavend voice init, plumb LLM config from JSON.
This commit is contained in:
kami
2026-07-10 15:48:55 +04:00
parent 28a940ebbe
commit c0c11cd36d
8 changed files with 703 additions and 32 deletions
+85
View File
@@ -0,0 +1,85 @@
// 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"
"time"
)
type Client struct {
base string
http *http.Client
}
func New(baseURL string, timeout time.Duration) *Client {
return &Client{base: baseURL, http: &http.Client{Timeout: timeout}}
}
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"`
}
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{{"system", r.System}, {"user", r.User}},
MaxTokens: r.MaxTokens,
Grammar: r.Grammar,
Temp: 0,
RepeatPenalty: r.RepeatPenalty,
Stop: r.Stop,
})
req, err := http.NewRequestWithContext(ctx, "POST", c.base+"/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")
}
return out.Choices[0].Message.Content, nil
}
+59
View File
@@ -0,0 +1,59 @@
package llm
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestComplete(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("method = %q, want POST", r.Method)
}
if !strings.HasSuffix(r.URL.Path, "/v1/chat/completions") {
t.Errorf("path = %q, want /v1/chat/completions", r.URL.Path)
}
var reqBody struct {
Messages []struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"messages"`
Grammar string `json:"grammar"`
MaxTokens int `json:"max_tokens"`
}
if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
t.Fatalf("decode request body: %v", err)
}
if len(reqBody.Messages) < 2 {
t.Fatalf("expected at least 2 messages, got %d", len(reqBody.Messages))
}
if reqBody.Messages[0].Role != "system" || reqBody.Messages[1].Content != "hi" {
t.Errorf("unexpected messages: %+v", reqBody.Messages)
}
if reqBody.Grammar != `root ::= "x"` {
t.Errorf("grammar = %q, want root ::= \"x\"", reqBody.Grammar)
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"choices":[{"message":{"content":"ok"}}]}`))
}))
defer srv.Close()
c := New(srv.URL, 5*time.Second)
got, err := c.Complete(context.Background(), Req{
System: "be helpful",
User: "hi",
Grammar: `root ::= "x"`,
MaxTokens: 42,
})
if err != nil {
t.Fatalf("Complete: %v", err)
}
if got != "ok" {
t.Errorf("got %q, want %q", got, "ok")
}
}
+81
View File
@@ -15,6 +15,7 @@ import (
"time"
"github.com/kami/maven/internal/delivery"
"github.com/kami/maven/internal/dialogue"
"github.com/kami/maven/internal/loop"
"github.com/kami/maven/internal/router"
)
@@ -142,6 +143,8 @@ func (p *LLMPhraser) start(ctx context.Context) error {
}
}
func (p *LLMPhraser) BaseURL() string { return p.port }
func (p *LLMPhraser) Close() error {
p.cancel()
if p.cmd != nil && p.cmd.Process != nil {
@@ -201,6 +204,84 @@ func (p *LLMPhraser) PhraseQuery(ctx context.Context, utterance string, notes []
return resp, nil
}
// PhraseChat uses the LLM to respond conversationally, building a multi-turn
// message array from dialogue history + the current user utterance. Falls back
// to a simple greeting on any LLM error — better to say something than nothing.
func (p *LLMPhraser) PhraseChat(ctx context.Context, utterance string, history []dialogue.Turn) (string, error) {
sys := chatSystemPrompt(p.cfg.Persona)
msgs := []chatMsg{
{Role: "system", Content: sys},
}
// Append dialogue history: user turns become "user" messages, and since we
// don't store assistant replies in the dialogue history, we reconstruct the
// pattern as alternating user messages. The model can infer maven's presence.
for _, t := range history {
msgs = append(msgs, chatMsg{Role: "user", Content: t.Text})
}
// Current utterance as the final user message.
msgs = append(msgs, chatMsg{Role: "user", Content: utterance})
resp, err := p.chatWithMessages(ctx, msgs, 512)
if err != nil {
return "поговорили.", nil
}
return resp, nil
}
// chatSystemPrompt returns the system prompt for conversational chat.
// Prepends the configured persona when set.
func chatSystemPrompt(persona string) string {
base := `You are maven, a self-hosted personal assistant. You're talking with your owner.
Keep replies brief (1-3 sentences) and natural. You're helpful, curious, and a little warm.
Respond in the user's language (Russian or English, matching their last message).
Never roleplay emotions you don't have, but stay friendly.
Just answer directly — no JSON wrapper, no meta-commentary.`
if persona != "" {
base = persona + "\n\n" + base
}
return base
}
// chatWithMessages sends a full message array (system + history + current) to
// the LLM completion endpoint. Like chatWithSystem but for an arbitrary message
// slice — the caller owns the system prompt placement.
func (p *LLMPhraser) chatWithMessages(ctx context.Context, msgs []chatMsg, maxTokens int) (string, error) {
req := chatReq{
Messages: msgs,
Temperature: 0.7,
MaxTokens: maxTokens,
}
body, err := json.Marshal(req)
if err != nil {
return "", fmt.Errorf("llm: marshal: %w", err)
}
httpReq, err := http.NewRequestWithContext(ctx, "POST", p.port+"/v1/chat/completions", bytes.NewReader(body))
if err != nil {
return "", fmt.Errorf("llm: request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
resp, err := p.client.Do(httpReq)
if err != nil {
return "", fmt.Errorf("llm: post: %w", err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("llm: read: %w", err)
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("llm: status %d: %s", resp.StatusCode, strings.TrimSpace(string(raw)))
}
var cr chatResp
if err := json.Unmarshal(raw, &cr); err != nil {
return "", fmt.Errorf("llm: parse: %w", err)
}
if len(cr.Choices) == 0 {
return "", fmt.Errorf("llm: no choices in response")
}
return cr.Choices[0].Message.Content, nil
}
func (p *LLMPhraser) PhraseReminder(ctx context.Context, d loop.ReminderDecision) (delivery.PhrasedReminder, error) {
text := extractReminderText(d.Reminder.Payload)
if text == "" {
+12 -2
View File
@@ -27,6 +27,7 @@ import (
"time"
"github.com/kami/maven/internal/delivery"
"github.com/kami/maven/internal/dialogue"
"github.com/kami/maven/internal/loop"
)
@@ -38,12 +39,14 @@ import (
// the daemon calls PhraseNudge with the loop's *Candidate (Rule + Severity +
// the State snapshot at evaluation time — exactly the (rule, severity,
// context) input the spec names). PhraseReminder with the ReminderDecision
// (Reminder + State). the phraser reads the State for context ("you haven't
// had water in 4h, you're at your desk, it's 2pm") — never touches the store.
// (Reminder + State). PhraseChat with a conversational utterance + dialogue
// history. the phraser reads the State for context ("you haven't had water in
// 4h, you're at your desk, it's 2pm") — never touches the store.
type Phraser interface {
PhraseNudge(ctx context.Context, c loop.Candidate) (delivery.PhrasedNudge, error)
PhraseReminder(ctx context.Context, d loop.ReminderDecision) (delivery.PhrasedReminder, error)
PhraseQuery(ctx context.Context, utterance string, notes []string) (string, error)
PhraseChat(ctx context.Context, utterance string, history []dialogue.Turn) (string, error)
Close() error
}
@@ -60,6 +63,13 @@ type Stub struct{}
// NewStub builds the floor phraser. no config — the Stub is stateless.
func NewStub() *Stub { return &Stub{} }
// PhraseChat returns a stub reply — the LLMPhraser replaces this with a
// prompted response from the model. The history parameter is accepted but
// ignored at the stub level (the production impl uses it for multi-turn).
func (s *Stub) PhraseChat(_ context.Context, _ string, _ []dialogue.Turn) (string, error) {
return "поговорили.", nil
}
// PhraseQuery returns a deterministic summary of the best matching notes.
func (s *Stub) PhraseQuery(_ context.Context, _ string, notes []string) (string, error) {
if len(notes) == 0 {
+32
View File
@@ -7,6 +7,7 @@ import (
"time"
"github.com/kami/maven/internal/delivery"
"github.com/kami/maven/internal/dialogue"
"github.com/kami/maven/internal/loop"
"github.com/kami/maven/internal/store"
)
@@ -192,6 +193,34 @@ func TestPhraseReminderEmptyPayload(t *testing.T) {
}
}
// ----------------------------- chat -----------------------------------------
func TestPhraseChatReturnsNonEmpty(t *testing.T) {
p := NewStub()
reply, err := p.PhraseChat(context.Background(), "как дела", nil)
if err != nil {
t.Fatalf("PhraseChat: %v", err)
}
if reply == "" {
t.Fatal("PhraseChat returned empty reply")
}
}
func TestPhraseChatWithHistory(t *testing.T) {
p := NewStub()
history := []dialogue.Turn{
{Text: "привет", Intent: dialogue.IntentChat},
{Text: "как тебя зовут", Intent: dialogue.IntentChat},
}
reply, err := p.PhraseChat(context.Background(), "расскажи о себе", history)
if err != nil {
t.Fatalf("PhraseChat with history: %v", err)
}
if reply == "" {
t.Fatal("PhraseChat with history returned empty reply")
}
}
// ----------------------------- interface guard ------------------------------
func TestStubSatisfiesPhraser(t *testing.T) {
@@ -206,6 +235,9 @@ func TestStubSatisfiesPhraser(t *testing.T) {
if _, err := p.PhraseReminder(context.Background(), loop.ReminderDecision{Reminder: store.Reminder{Payload: "{}"}, State: loop.State{Now: time.Now().UTC()}}); err != nil {
t.Fatalf("PhraseReminder: %v", err)
}
if _, err := p.PhraseChat(context.Background(), "как дела", nil); err != nil {
t.Fatalf("PhraseChat: %v", err)
}
}
func TestStubProducesDeliveryTypes(t *testing.T) {