Knowledge Embeddings Developer Guide - edmozley/freeitsm GitHub Wiki
Knowledge embeddings - developer guide
5.12.0, #449. The user's view: Knowledge § Embeddings.
Article embeddings are what Ask AI, the web chat's answers and the gap analysis find articles with. They used to be OpenAI text-embedding-3-small with the Embeddings tab's own key, hard-wired in five places. Now any provider that can embed, through the shared code the help assistant already used.
📁 Files
| File | What |
|---|---|
includes/knowledge/kb_embeddings.php |
The module: settings, model identity, "current" SQL, embed, store |
includes/ai_embeddings.php |
Shared: aiEmbedBatch() (now returns http), aiEmbeddingMissing() / aiEmbeddingUsable(), aiEmbeddingFatal(), aiEmbeddingModelId(), aiEmbeddingModels() |
includes/ai_settings.php |
The knowledge_openai namespace (embeddings only) |
api/knowledge/embedding_settings.php |
The tab: GET state; POST save, models, test |
api/knowledge/generate_embedding.php, get_embedding_stats.php, get_articles_for_embedding.php |
Generate, its counts, what still needs it |
includes/services/knowledge.php (updateEmbedding()), api/knowledge/ai_chat.php, includes/knowledge/kb_ai.php, includes/knowledge/gap_analysis.php |
The readers and the save-time embed |
knowledge/settings/index.php |
The Embeddings tab |
tests/knowledge-embeddings.php |
34 checks against the stand-in server tests/ai-compatible/mock.php |
The rules
$cfg = kbEmbedConfig($conn); // provider, model, key (decrypted), Azure / server fields
[$sql, $params] = kbEmbedCurrentSql($conn, $cfg, 'a.'); // " AND a.embedding ... AND a.embedding_model = ?"
$r = kbEmbedText($cfg, $question); // ['vector', 'error', 'fatal', 'model']
kbEmbedArticle($conn, $cfg, $id, $title, $html); // embed + store with the model's identity
- Only current vectors count. Every vector stores
knowledge_articles.embedding_model(aiEmbeddingModelId():openai:text-embedding-3-small,compatible:nomic-embed-text). Every reader appendskbEmbedCurrentSql(), so a question is only ever compared with vectors of its own model. Change the model and the old vectors simply stop matching;get_articles_for_embedding.phpoffers them again. - Missing is decided before sending.
aiEmbeddingMissing()names what is not set (provider,model,key,address,endpoint); the stats endpoint returnsusableand areason, and the tab disables Generate with it. - Fatal stops the run.
aiEmbeddingFatal(): never reached the server, 401, 403, 404, 429, or 5xx - the setup's fault, so every next article fails the same way.generate_embedding.phpreturnsfatal, and the page stops and lists the reasons with counts. A 400 or a bad answer is the text's and the run goes on. - The gap cache follows the model.
knowledge_gap_ticketsholds ticket vectors compared with article vectors;kbEmbedModelChanged()empties it when a save changes the model.
🪤 Traps
- TRAP: the namespace is
knowledge_openai, whatever the provider. Its key row isknowledge_openai_api_key- the row every install has had since embeddings shipped - so an upgrade reads the existing key as OpenAI with the default model and nothing is migrated. Renaming it would orphan every saved key. - TRAP: before Database Verification, no
embedding_model.kbEmbedCurrentSql()then counts plain vectors only while the model is still the legacy one (they are the only kind there), and nothing otherwise;kbEmbedStore()refuses to write an unlabelled vector of any other model. The run that adds the column stamps existing vectorsopenai:text-embedding-3-small, once ($kbEmbedModelColWasMissingindb_verify.php). - TRAP: a key never follows the provider to another server.
aiSettingsSave()drops a kept key across the custom-server boundary. Found while building this: on an install that had never saved a provider row, the old provider read as''and the drop was skipped - the OpenAI key would have gone, as a Bearer token, to the new server. An absent row now reads as the namespace's default provider.embedding_settings.php'smodelsaction applies the same rule before it asks a server for its list. - TRAP: the tab's Generate result was never visible. The JS set an inline
display:noneon the result box, which outranked the.success/.errorclasses. Do not reintroduce it. - TRAP (JS):
embLoad()is called throughsetTimeout. It readsEMB_API, aconstdeclared later in the same script; called immediately it hits the const before its line has run and fails silently.
Tests
tests/knowledge-embeddings.php - an upgrade reads as legacy; the server's models listed; the OpenAI key not sent to the server (the control for the trap above); Generate through the real endpoint stores compatible:fake-embed, every request to the stand-in and none to OpenAI; retrieval finds the right article; a model change re-offers the articles, never compares across models (fake-embed and fake-embed-b mean nothing to each other) and empties the gap cache; a 500 and no listener are fatal with their reasons; no key disables Generate; and with the column dropped the readers still answer, then Database Verification re-adds it and stamps once.
See also
AI providers · Help assistant design (the same embedding code) · Issue #449