Re-embed the knowledge base when the embedding model changes
Vectors from two different embedding models are not comparable — Titan v2 has 1024 dimensions where nomic-embed-text has 768 — and _cosine_similarity zips them, so a mismatch produced quietly meaningless scores rather than an error. Nothing forced a re-embed either: sync_local_kb only looked at the content hash, so switching models left every cached vector in place. local_knowledge_docs now records the model each vector came from, and a row is re-embedded when its content hash OR its model differs. Saving a new LOCAL_KB_EMBEDDING_MODEL from the settings panel starts a background pass over every file; the knowledge base panel also has a button for it. While that pass runs, the per-request sync steps aside so no file is embedded twice. The panel now shows what the embedding model is doing: model name, vector size, how many files are embedded with the current model, how many are still on an older one, a progress bar while a pass runs, and the last error. A "接続テスト" button embeds one short string so a broken or unreachable model can be told apart from one that is merely slow to warm up. Also read TAVILY_API_KEY from os.environ at call time. It was imported as a config constant, which is evaluated before load_into_env() runs, so a key saved from the settings panel was stored but never used — not even after a restart. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue'
|
||||
import { fetchKbFile, fetchKbFiles, type KbFileContent, type KbFileInfo } from '../api/client'
|
||||
import {
|
||||
fetchKbEmbeddingStatus,
|
||||
fetchKbFile,
|
||||
fetchKbFiles,
|
||||
reindexKbEmbeddings,
|
||||
testKbEmbedding,
|
||||
type KbEmbeddingProbe,
|
||||
type KbEmbeddingStatus,
|
||||
type KbFileContent,
|
||||
type KbFileInfo,
|
||||
} from '../api/client'
|
||||
|
||||
export interface KbGroup {
|
||||
/** ナレッジベース直下は空文字。 */
|
||||
@@ -18,6 +28,15 @@ export const useKnowledgeBaseStore = defineStore('knowledgeBase', () => {
|
||||
const filter = ref('')
|
||||
/** 折りたたまれているフォルダ名。web_search のようにファイル数が多いフォルダを畳むために使う。 */
|
||||
const collapsed = ref<Set<string>>(new Set())
|
||||
/** 埋め込みモデルの状態と進捗。 */
|
||||
const embedding = ref<KbEmbeddingStatus | null>(null)
|
||||
/** 「接続テスト」の結果。 */
|
||||
const probe = ref<KbEmbeddingProbe | null>(null)
|
||||
const testing = ref(false)
|
||||
|
||||
/** 埋め込み実行中だけ状態をポーリングするタイマー。 */
|
||||
let pollTimer: number | null = null
|
||||
const POLL_INTERVAL_MS = 1500
|
||||
|
||||
const filteredFiles = computed(() => {
|
||||
const q = filter.value.trim().toLowerCase()
|
||||
@@ -66,6 +85,57 @@ export const useKnowledgeBaseStore = defineStore('knowledgeBase', () => {
|
||||
openFile.value = null
|
||||
}
|
||||
|
||||
/** 埋め込みの状態を取り直す。実行中ならポーリングを始め、終わっていれば止める。 */
|
||||
async function loadEmbedding(): Promise<void> {
|
||||
try {
|
||||
embedding.value = await fetchKbEmbeddingStatus()
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : '埋め込み状態の取得に失敗しました'
|
||||
stopPolling()
|
||||
return
|
||||
}
|
||||
if (embedding.value.running && pollTimer === null) {
|
||||
pollTimer = window.setInterval(() => void loadEmbedding(), POLL_INTERVAL_MS)
|
||||
} else if (!embedding.value.running && pollTimer !== null) {
|
||||
stopPolling()
|
||||
// 埋め込みが終わったので、一覧の「埋め込み済み」表示も更新する
|
||||
await load()
|
||||
}
|
||||
}
|
||||
|
||||
function stopPolling(): void {
|
||||
if (pollTimer !== null) {
|
||||
clearInterval(pollTimer)
|
||||
pollTimer = null
|
||||
}
|
||||
}
|
||||
|
||||
/** 埋め込みモデルに実際にリクエストして、応答するか確かめる。 */
|
||||
async function testEmbedding(): Promise<void> {
|
||||
testing.value = true
|
||||
probe.value = null
|
||||
try {
|
||||
probe.value = await testKbEmbedding()
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : '埋め込みテストに失敗しました'
|
||||
} finally {
|
||||
testing.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/** 全ファイルの再埋め込みをバックグラウンドで開始する。 */
|
||||
async function reindex(): Promise<void> {
|
||||
error.value = null
|
||||
try {
|
||||
embedding.value = await reindexKbEmbeddings()
|
||||
if (embedding.value.running && pollTimer === null) {
|
||||
pollTimer = window.setInterval(() => void loadEmbedding(), POLL_INTERVAL_MS)
|
||||
}
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : '再埋め込みの開始に失敗しました'
|
||||
}
|
||||
}
|
||||
|
||||
function toggleGroup(directory: string): void {
|
||||
const next = new Set(collapsed.value)
|
||||
if (next.has(directory)) next.delete(directory)
|
||||
@@ -73,5 +143,25 @@ export const useKnowledgeBaseStore = defineStore('knowledgeBase', () => {
|
||||
collapsed.value = next
|
||||
}
|
||||
|
||||
return { files, openFile, loading, error, filter, collapsed, filteredFiles, groups, load, open, close, toggleGroup }
|
||||
return {
|
||||
files,
|
||||
openFile,
|
||||
loading,
|
||||
error,
|
||||
filter,
|
||||
collapsed,
|
||||
embedding,
|
||||
probe,
|
||||
testing,
|
||||
filteredFiles,
|
||||
groups,
|
||||
load,
|
||||
open,
|
||||
close,
|
||||
toggleGroup,
|
||||
loadEmbedding,
|
||||
stopPolling,
|
||||
testEmbedding,
|
||||
reindex,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user