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:
2026-08-23 22:56:12 +09:00
co-authored by Claude Opus 5
parent 52dfc0c349
commit 97568ba90e
14 changed files with 550 additions and 41 deletions
+43
View File
@@ -431,3 +431,46 @@ export async function fetchKbFile(path: string): Promise<KbFileContent> {
const res = await fetch(`${BASE}/api/kb/file?path=${encodeURIComponent(path)}`)
return parseJson<KbFileContent>(res, 'ナレッジベースファイル取得')
}
/** ローカルナレッジベースの埋め込み状況。GUI の状態表示・進捗バーに使う。 */
export interface KbEmbeddingStatus {
model: string
/** 現在のモデルで埋め込まれたベクトルの次元数(1件も無ければ null)。 */
dimensions: number | null
file_count: number
indexed: number
/** 別のモデルで埋め込まれていて埋め直しが必要な件数。 */
stale: number
running: boolean
total: number
done: number
failed: number
started_at: string | null
finished_at: string | null
last_error: string | null
}
/** 埋め込みモデルに実際に1回リクエストしてみた結果。 */
export interface KbEmbeddingProbe {
ok: boolean
model: string
dimensions: number | null
latency_ms: number
error: string | null
}
export async function fetchKbEmbeddingStatus(): Promise<KbEmbeddingStatus> {
const res = await fetch(`${BASE}/api/kb/embedding`)
return parseJson<KbEmbeddingStatus>(res, '埋め込み状態取得')
}
export async function testKbEmbedding(): Promise<KbEmbeddingProbe> {
const res = await fetch(`${BASE}/api/kb/embedding/test`, { method: 'POST' })
return parseJson<KbEmbeddingProbe>(res, '埋め込みテスト')
}
/** 全ファイルの再埋め込みをバックグラウンドで開始する。戻り値は開始直後の状態。 */
export async function reindexKbEmbeddings(): Promise<KbEmbeddingStatus> {
const res = await fetch(`${BASE}/api/kb/embedding/reindex`, { method: 'POST' })
return parseJson<KbEmbeddingStatus>(res, '再埋め込み開始')
}
+85 -2
View File
@@ -1,11 +1,22 @@
<script setup lang="ts">
import { onMounted } from 'vue'
import { computed, onMounted, onUnmounted } from 'vue'
import MarkdownContent from './MarkdownContent.vue'
import { useKnowledgeBaseStore } from '../stores/knowledgeBase'
const store = useKnowledgeBaseStore()
onMounted(() => store.load())
onMounted(() => {
store.load()
store.loadEmbedding()
})
// パネルを閉じている間もポーリングが回り続けないように止める
onUnmounted(() => store.stopPolling())
const progressPercent = computed(() => {
const e = store.embedding
if (!e || e.total === 0) return 0
return Math.round(((e.done + e.failed) / e.total) * 100)
})
function formatSize(bytes: number): string {
return bytes < 1024 ? `${bytes} B` : `${Math.round(bytes / 1024)} KB`
@@ -41,6 +52,54 @@ function formatDate(iso: string): string {
<!-- 一覧 -->
<template v-else>
<div v-if="store.embedding" class="embedding">
<div class="embedding-head">
<span class="embedding-model" :title="`埋め込みモデル: ${store.embedding.model}`">
🧠 {{ store.embedding.model }}
</span>
<button class="mini-btn" :disabled="store.testing" title="このモデルに実際に1回リクエストして応答を確かめる"
@click="store.testEmbedding()">
{{ store.testing ? 'テスト中…' : '接続テスト' }}
</button>
<button class="mini-btn" :disabled="store.embedding.running" title="全ファイルを現在のモデルで埋め込み直す"
@click="store.reindex()">
埋め直し
</button>
</div>
<div class="embedding-stats">
<span :class="{ warn: store.embedding.indexed < store.embedding.file_count }">
埋め込み済み {{ store.embedding.indexed }} / {{ store.embedding.file_count }}
</span>
<span v-if="store.embedding.stale > 0" class="warn">
・別モデルのまま {{ store.embedding.stale }}
</span>
<span v-if="store.embedding.dimensions">・{{ store.embedding.dimensions }} 次元</span>
</div>
<template v-if="store.embedding.running">
<div class="progress"><div class="progress-bar" :style="{ width: `${progressPercent}%` }" /></div>
<div class="embedding-note">
<template v-if="store.embedding.total === 0">準備中…</template>
<template v-else>
埋め込み中 {{ store.embedding.done + store.embedding.failed }} / {{ store.embedding.total }}
<span v-if="store.embedding.failed > 0" class="ng">(失敗 {{ store.embedding.failed }})</span>
</template>
</div>
</template>
<div v-if="store.probe" class="probe" :class="store.probe.ok ? 'ok' : 'ng'">
<template v-if="store.probe.ok">
✅ 応答あり — {{ store.probe.dimensions }} 次元 / {{ Math.round(store.probe.latency_ms) }} ms
</template>
<template v-else>❌ {{ store.probe.error }}</template>
</div>
<div v-if="store.embedding.last_error" class="probe ng">
直近の埋め込みエラー: {{ store.embedding.last_error }}
</div>
</div>
<div class="search">
<input v-model="store.filter" type="search" placeholder="タイトル・パス・本文の先頭で絞り込み">
<span class="count">{{ store.filteredFiles.length }} / {{ store.files.length }}</span>
@@ -109,6 +168,30 @@ function formatDate(iso: string): string {
.back-btn:hover, .reload-btn:hover:not(:disabled) { color: #fff; }
.error { padding: 8px 12px; color: #f87171; font-size: 12px; }
.embedding {
padding: 8px 12px; border-bottom: 1px solid #1e1e1e; flex-shrink: 0;
display: flex; flex-direction: column; gap: 6px;
}
.embedding-head { display: flex; align-items: center; gap: 6px; }
.embedding-model {
flex: 1; color: #cbd5e1; font-size: 12px;
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.mini-btn {
background: #2a2a2a; color: #aaa; border: 1px solid #444;
border-radius: 5px; padding: 2px 8px; font-size: 11px; cursor: pointer; white-space: nowrap;
}
.mini-btn:hover:not(:disabled) { color: #fff; }
.mini-btn:disabled { opacity: 0.5; cursor: default; }
.embedding-stats { color: #6b7280; font-size: 11px; }
.embedding-stats .warn { color: #fbbf24; }
.embedding-note { color: #6b7280; font-size: 11px; }
.progress { height: 4px; background: #262626; border-radius: 2px; overflow: hidden; }
.progress-bar { height: 100%; background: #2563eb; transition: width 0.3s; }
.probe { font-size: 11px; line-height: 1.5; word-break: break-word; }
.probe.ok { color: #4ade80; }
.probe.ng, .ng { color: #f87171; }
.search {
display: flex; align-items: center; gap: 8px;
padding: 8px 12px; border-bottom: 1px solid #1e1e1e; flex-shrink: 0;
+92 -2
View File
@@ -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,
}
})