From 5ee8e74de19d947e68c421c6cc14b78793a861d3 Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Thu, 8 Oct 2026 15:59:23 +0200 Subject: [PATCH] The portal's chat answers from the documents a person picks A Documents picker on /chat offers the collections the person may read in the chosen project, the default when there is one, or none; the sources an answer was given are listed under it. The page's choice becomes the X-Llmgw-Knowledge header; one the page sent itself is dropped. In English and Turkish. Co-Authored-By: Claude Opus 5.5 (1M context) --- BACKLOG.md | 6 +++-- docs/knowledge.md | 4 +++ internal/router/chat.go | 42 ++++++++++++++++++++++++++++- internal/router/chat_models_test.go | 29 ++++++++++++++++++++ internal/web/chat.go | 29 +++++++++++++++----- internal/web/chat_test.go | 2 +- internal/web/i18n_chat_test.go | 11 ++++++-- internal/web/locales/en/chat.json | 8 +++--- internal/web/locales/tr/chat.json | 8 +++--- internal/web/templates/chat.html | 19 +++++++++++++ 10 files changed, 140 insertions(+), 18 deletions(-) diff --git a/BACKLOG.md b/BACKLOG.md index da20c83f..dc96c28e 100644 --- a/BACKLOG.md +++ b/BACKLOG.md @@ -973,10 +973,12 @@ endpoints), 404 for one not readable; the question embedded through the ordinary path on the caller's principal; SearchChunks; passages trimmed to the model's context; added to the one leading system message (or a new one); `x-llmgw-sources`; before guardInput; never cached; metrics. Default collections built (kb_defaults on org/team/project/key, narrowest -wins, 30 s cache, `ctl kb default`, console action; never refuse). Left: +wins, 30 s cache, `ctl kb default`, console action; never refuse); the +portal chat's Documents picker (collection, default or none; sources listed +under the answer; a header the page sends itself is dropped). Left: `cache_salt` per organization (not sent: llama.cpp may refuse an unknown field, and the router does not know the engine before choosing the -replica); the chat playground has no collection picker; returned passages +replica); returned passages are not audited per request (the header and the kept request record them). ### F4. Adapter training (L, after F1–F3) diff --git a/docs/knowledge.md b/docs/knowledge.md index 2fba39a7..4b069cdb 100644 --- a/docs/knowledge.md +++ b/docs/knowledge.md @@ -295,6 +295,10 @@ What the gateway does with such a request: carrying instructions aimed at its reader is judged like anything else the model reads. +On the portal's chat page, **Documents** picks a collection the person may +read in the chosen project, the default, or none. Under each answer, the +sources it was given are listed by number. + How it is shaped: | | | diff --git a/internal/router/chat.go b/internal/router/chat.go index 8b344ef9..97dfbd38 100644 --- a/internal/router/chat.go +++ b/internal/router/chat.go @@ -37,7 +37,7 @@ func (r *Router) registerChat(mux *http.ServeMux, pages *web.AccountPages) { if current == "" && !r.personalOff { current = personalContext } - web.Chat(user, r.modelsFor(req, user), contexts, current, version.Version)(w, req) + web.Chat(user, r.modelsFor(req, user), contexts, current, version.Version, r.chatKnowledge(req, user, current))(w, req) })) mux.HandleFunc("POST /chat/send", pages.Guard(r.chatSend)) // Where a model stands while it loads, and a way to ask for it without @@ -75,6 +75,9 @@ func (r *Router) chatSend(w http.ResponseWriter, req *http.Request, user store.U // Temperature is the page's one sampling setting, held to the range // every engine takes. Temperature *float64 `json:"temperature"` + // Knowledge is a collection to answer from, "none", or "" for the + // default; whether it may be read is decided as for a key. + Knowledge string `json:"knowledge"` } if err := json.Unmarshal(body, &in); err != nil || in.Model == "" || len(in.Messages) == 0 { http.Error(w, "a model and at least one message are required", http.StatusBadRequest) @@ -112,6 +115,10 @@ func (r *Router) chatSend(w http.ResponseWriter, req *http.Request, user store.U next.Body = io.NopCloser(bytes.NewReader(upstream)) next.ContentLength = int64(len(upstream)) next.Header.Set("Content-Type", "application/json") + next.Header.Del(knowledgeHeader) + if k := strings.TrimSpace(in.Knowledge); k != "" { + next.Header.Set(knowledgeHeader, k) + } // The same handler the API serves, so there is exactly one request path and // no second place for its rules to drift. @@ -309,3 +316,36 @@ func (r *Router) modelsFor(req *http.Request, user store.User) []web.ChatModel { }) return out } + +// chatKnowledge is the collections the page offers in a context, and the +// default that answers when none is chosen. +func (r *Router) chatKnowledge(req *http.Request, user store.User, context string) web.ChatKnowledge { + var out web.ChatKnowledge + if r.store == nil || r.store.KnowledgeAvailable() != nil { + return out + } + p, err := r.principalFor(req, user, context) + if err != nil { + return out + } + e := store.Effective{KeyID: p.KeyID, UserID: p.UserID, ProjectID: p.ProjectID, TeamID: p.TeamID, OrgID: p.OrgID} + cs, err := r.store.CollectionsFor(req.Context(), e, false) + if err != nil { + return out + } + names := map[string]string{} + for _, c := range cs { + out.Collections = append(out.Collections, web.ChatCollection{ID: c.ID, Label: c.Name}) + names[c.ID] = c.Name + } + if def, err := r.store.KBDefaultFor(req.Context(), e); err == nil { + var shown []string + for _, id := range def { + if n, ok := names[id]; ok { + shown = append(shown, n) + } + } + out.Default = strings.Join(shown, ", ") + } + return out +} diff --git a/internal/router/chat_models_test.go b/internal/router/chat_models_test.go index 833040db..ffcf166a 100644 --- a/internal/router/chat_models_test.go +++ b/internal/router/chat_models_test.go @@ -2,6 +2,7 @@ package router import ( "encoding/json" + "net/http" "net/http/httptest" "path/filepath" "strings" @@ -207,3 +208,31 @@ func TestAChatTurnCarriesItsTemperatureAndAsksForUsage(t *testing.T) { t.Errorf("sent %s", raw) } } + +func TestTheChatPageAsksForTheDocumentsChosenAndOnlyThose(t *testing.T) { + // The picker's choice becomes the header; a header the page sent itself + // is not trusted. On SQLite, asking is enough to tell: it is refused as + // knowledge bases being off, before the model is asked. + e := newFakeEngine(t, "qwen2.5-coder-7b-instruct") + rt, user := chatRouter(t, e.srv.URL) + waitForEndpoint(t, rt) + send := func(knowledge, spoofed string) *httptest.ResponseRecorder { + body := `{"model":"qwen2.5-coder-7b-instruct","knowledge":"` + knowledge + `","messages":[{"role":"user","content":"hi"}]}` + req := httptest.NewRequest("POST", "/chat/send", strings.NewReader(body)) + if spoofed != "" { + req.Header.Set(knowledgeHeader, spoofed) + } + rec := httptest.NewRecorder() + rt.chatSend(rec, req, user) + return rec + } + if rec := send("politikalar", ""); rec.Code != http.StatusNotImplemented || !strings.Contains(rec.Body.String(), "SQLite") { + t.Errorf("a collection chosen: %d %s", rec.Code, rec.Body.String()) + } + if rec := send("", "politikalar"); rec.Code != http.StatusOK { + t.Errorf("a header the page set itself was used: %d %s", rec.Code, rec.Body.String()) + } + if rec := send("none", ""); rec.Code != http.StatusOK { + t.Errorf("none: %d %s", rec.Code, rec.Body.String()) + } +} diff --git a/internal/web/chat.go b/internal/web/chat.go index c7d2c978..9262e705 100644 --- a/internal/web/chat.go +++ b/internal/web/chat.go @@ -54,24 +54,41 @@ type ChatContext struct { Label string } +// ChatCollection is a knowledge-base collection the chat can answer from. +type ChatCollection struct { + ID, Label string +} + type chatData struct { Nav Nav Contexts []ChatContext Context string - User store.User - Models []ChatModel - CSRF string - Version string - Now string + // Collections are what this person may read in the chosen context; + // KnowledgeDefault names the ones its part of the tree answers from + // when none is chosen, if any. + Collections []ChatCollection + KnowledgeDefault string + User store.User + Models []ChatModel + CSRF string + Version string + Now string +} + +// ChatKnowledge is what the chat page offers to answer from. +type ChatKnowledge struct { + Collections []ChatCollection + Default string } // Chat renders the in-browser chat for one signed-in person. -func Chat(user store.User, models []ChatModel, contexts []ChatContext, current, version string) http.HandlerFunc { +func Chat(user store.User, models []ChatModel, contexts []ChatContext, current, version string, kb ChatKnowledge) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { noStore(w) render(w, r, "chat", chatData{ Nav: navFrom(r, "chat"), Contexts: contexts, Context: current, + Collections: kb.Collections, KnowledgeDefault: kb.Default, User: user, Models: models, CSRF: csrfFor(r), Version: version, Now: time.Now().UTC().Format(time.RFC3339), }) diff --git a/internal/web/chat_test.go b/internal/web/chat_test.go index c76c803e..611683ee 100644 --- a/internal/web/chat_test.go +++ b/internal/web/chat_test.go @@ -20,7 +20,7 @@ func chatPage(t *testing.T, names ...string) string { func chatPageWith(t *testing.T, models []ChatModel) string { t.Helper() - h := Chat(store.User{Email: "a@example.com"}, models, nil, "", "v1") + h := Chat(store.User{Email: "a@example.com"}, models, nil, "", "v1", ChatKnowledge{}) r := httptest.NewRequest(http.MethodGet, "http://x/chat", nil) w := httptest.NewRecorder() h(w, r) diff --git a/internal/web/i18n_chat_test.go b/internal/web/i18n_chat_test.go index 4b140800..8e9882eb 100644 --- a/internal/web/i18n_chat_test.go +++ b/internal/web/i18n_chat_test.go @@ -12,7 +12,8 @@ import ( func TestTheChatPageSpeaksTurkishWhenAsked(t *testing.T) { h := Chat(store.User{Email: "a@example.com"}, - []ChatModel{{Name: "qwen"}, {Name: "big-model", Simulated: true}}, nil, "", "v1") + []ChatModel{{Name: "qwen"}, {Name: "big-model", Simulated: true}}, nil, "", "v1", + ChatKnowledge{Collections: []ChatCollection{{ID: "kb_x", Label: "politikalar"}}, Default: "politikalar"}) r := httptest.NewRequest(http.MethodGet, "http://x/chat", nil) r.Header.Set("Accept-Language", "tr-TR,tr;q=0.9") w := httptest.NewRecorder() @@ -31,12 +32,18 @@ func TestTheChatPageSpeaksTurkishWhenAsked(t *testing.T) { // The script's own words come from the catalogue too. `"Uzunluk sınırında kesildi — devam etmesini isteyin."`, `decimal: ","`, + // The documents picker, with the default named. + "Belgeler", + ``, + ``, + ``, + `"Kaynaklar:"`, } { if !strings.Contains(page, want) { t.Errorf("the Turkish chat page does not have %q", want) } } - for _, not := range []string{"New conversation", "Cut off at the length limit", "Ask something"} { + for _, not := range []string{"New conversation", "Cut off at the length limit", "Ask something", "Documents", "Sources:"} { if strings.Contains(page, not) { t.Errorf("the Turkish chat page still says %q", not) } diff --git a/internal/web/locales/en/chat.json b/internal/web/locales/en/chat.json index dd3a8248..676b7d75 100644 --- a/internal/web/locales/en/chat.json +++ b/internal/web/locales/en/chat.json @@ -19,7 +19,6 @@ "chat.kept": "The conversation is kept in this browser, so a reload does not lose it; \"New conversation\" clears it. Nothing of it is stored on the gateway — what is recorded there is the same usage any request records: tokens, cost and latency.", "chat.empty": "No models are available to you right now.", "chat.empty_why": "Either nothing is placed yet, or your project's allowlist does not include anything that is. /admin shows the cluster if you have access to it.", - "chat.js.preparing": "Preparing…", "chat.js.download_failed": "download failed", "chat.js.stage.waiting": "Waiting for a node with room", @@ -41,7 +40,10 @@ "chat.js.stopped_by_policy_empty": "(stopped by the gateway's policy{policy} before anything was shown)", "chat.js.stopped_empty": "(stopped before anything came back)", "chat.js.no_content": "(no content came back)", - "download.err.read": "could not read the request", - "download.err.no_code": "there is no code in that reply to download" + "download.err.no_code": "there is no code in that reply to download", + "chat.knowledge": "Documents", + "chat.knowledge.default": "default", + "chat.knowledge.none": "none", + "chat.js.sources": "Sources:" } diff --git a/internal/web/locales/tr/chat.json b/internal/web/locales/tr/chat.json index 54225a51..c88607ce 100644 --- a/internal/web/locales/tr/chat.json +++ b/internal/web/locales/tr/chat.json @@ -19,7 +19,6 @@ "chat.kept": "Sohbet bu tarayıcıda saklanır; sayfayı yenilemek onu silmez, \"Yeni sohbet\" temizler. Ağ geçidinde sohbetten hiçbir şey saklanmaz — orada kaydedilen, her isteğin kaydettiği kullanımla aynıdır: token, maliyet ve gecikme.", "chat.empty": "Şu anda kullanabileceğiniz bir model yok.", "chat.empty_why": "Ya henüz hiçbir model yerleştirilmedi ya da projenizin izin listesi yerleştirilmiş olanların hiçbirini içermiyor. Erişiminiz varsa /admin kümeyi gösterir.", - "chat.js.preparing": "Hazırlanıyor…", "chat.js.download_failed": "indirme başarısız oldu", "chat.js.stage.waiting": "Yeri olan bir düğüm bekleniyor", @@ -41,7 +40,10 @@ "chat.js.stopped_by_policy_empty": "(hiçbir şey gösterilmeden ağ geçidinin politikası{policy} tarafından durduruldu)", "chat.js.stopped_empty": "(hiçbir yanıt gelmeden durduruldu)", "chat.js.no_content": "(hiçbir içerik gelmedi)", - "download.err.read": "istek okunamadı", - "download.err.no_code": "bu yanıtta indirilecek kod yok" + "download.err.no_code": "bu yanıtta indirilecek kod yok", + "chat.knowledge": "Belgeler", + "chat.knowledge.default": "varsayılan", + "chat.knowledge.none": "yok", + "chat.js.sources": "Kaynaklar:" } diff --git a/internal/web/templates/chat.html b/internal/web/templates/chat.html index fc092f18..e56b8205 100644 --- a/internal/web/templates/chat.html +++ b/internal/web/templates/chat.html @@ -34,6 +34,13 @@ + {{if .Collections}}{{end}} @@ -89,6 +96,7 @@ downloadZip: {{t "chat.js.download_zip"}}, stoppedByPolicy: {{t "chat.js.stopped_by_policy"}}, cutOff: {{t "chat.js.cut_off"}}, + sources: {{t "chat.js.sources"}}, stoppedByPolicyEmpty: {{t "chat.js.stopped_by_policy_empty"}}, stoppedEmpty: {{t "chat.js.stopped_empty"}}, noContent: {{t "chat.js.no_content"}}, @@ -381,6 +389,7 @@ method: "POST", headers: { "Content-Type": "application/json", "X-CSRF-Token": csrf }, body: JSON.stringify({ model: model.value, context: ctxValue(), temperature: +temp.value, + knowledge: (document.getElementById("knowledge") || {}).value || "", messages: (system ? [{ role: "system", content: system }] : []).concat(messages) }), signal: busy.signal, }); @@ -478,6 +487,16 @@ note.textContent = say.cutOff; out.after(note); } + // Which passages of the documents it was given, numbered as the + // reply cites them. Ours, so a sibling placed with textContent. + const src = resp.headers.get("x-llmgw-sources"); + if (src) { + const note = document.createElement("p"); + note.className = "note sources"; + note.textContent = say.sources + " " + src.split(",").map((s, i) => + "[" + (i + 1) + "] " + decodeURIComponent(s.split("#")[0])).join(" "); + out.after(note); + } stats(out, tokens || pieces, t0, tFirst, performance.now(), stopped, resp.headers.get("x-llmgw-hardware")); // Once, at the end. Re-rendering every chunk would try to typeset half // an expression on each token and flicker its way through the reply.