Knowledge bases administered: llmgw ctl kb and /api/v1/kb

Collections made for an organization, team, project or person, their
dimension asked of the embedding model; granted and revoked by path, never
outside their organization; their documents listed and removed; and a search
an operator can try. In the OpenAPI document.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-08 13:49:44 +02:00
1 parent ffab3c99e3
commit 6466f37d50
8 files changed
+1405 -1

No files matched your search

+41
View File
@@ -5,6 +5,7 @@ import (
"crypto/rand"
"crypto/tls"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log/slog"
@@ -374,6 +375,9 @@ func newServerCmd() *cobra.Command {
api.SetAgentRestarter(agentSvc.RestartAgents)
}
api.SetAudit(st)
// Knowledge bases, with the cluster's own models to embed a
// probe (a collection's dimension) and an operator's search.
api.SetKB(&adminapi.KBAdmin{Store: st, Embed: embedder(rt.Replay)})
// The cluster's settings, each change checked against this
// machine's own keys before it is kept.
api.SetSettings(&adminapi.Settings{Store: st, LocalKeys: config.LocalKeys(),
@@ -1183,3 +1187,40 @@ func federationID(ctx context.Context, st *storepkg.DB) (string, error) {
// more of a secret than needed.
return secret[:16], nil
}
// embedder has a model embed one text through the gateway itself, the way a
// kept request is replayed: for the administrator's knowledge-base
// endpoints, not for anybody's traffic.
func embedder(replay func(ctx context.Context, path, model string, body []byte) (int, []byte, time.Duration, error)) func(context.Context, string, string) ([]float32, error) {
return func(ctx context.Context, model, text string) ([]float32, error) {
body, _ := json.Marshal(map[string]any{"model": model, "input": []string{text}})
status, out, _, err := replay(ctx, "/v1/embeddings", model, body)
if err != nil {
return nil, err
}
if status != http.StatusOK {
msg := strings.TrimSpace(string(out))
var e struct {
Error struct {
Message string `json:"message"`
} `json:"error"`
}
if json.Unmarshal(out, &e) == nil && e.Error.Message != "" {
msg = e.Error.Message
}
return nil, fmt.Errorf("%d: %s", status, msg)
}
var res struct {
Data []struct {
Embedding []float32 `json:"embedding"`
} `json:"data"`
}
if err := json.Unmarshal(out, &res); err != nil {
return nil, err
}
if len(res.Data) != 1 || len(res.Data[0].Embedding) == 0 {
return nil, fmt.Errorf("%s answered no embedding", model)
}
return res.Data[0].Embedding, nil
}
}