/** * brain-index.json 일괄 압축 — 기존 full-precision 임베딩 벡터를 소수 4자리로 양자화. * * 양자화 저장(quantizeVector)이 들어가기 *전에* 백필된 벡터를 줄이는 1회성 도구. * 이후 신규 벡터는 저장 시점에 양자화되므로 다시 돌릴 일은 드물다. * * node --max-old-space-size=8192 scripts/compact_brain_index.mjs "E:/Wiki/2nd/10_Wiki/Topics/.astra/brain-index.json" */ import fs from 'fs'; const file = process.argv[2]; if (!file || !fs.existsSync(file)) { console.error('사용법: node compact_brain_index.mjs '); process.exit(1); } const before = fs.statSync(file).size; console.log(`읽는 중… (${(before / 1e6).toFixed(0)} MB)`); const idx = JSON.parse(fs.readFileSync(file, 'utf8')); const q = (v) => Math.round(v * 10000) / 10000; let files = 0, chunks = 0; for (const entry of Object.values(idx.entries || {})) { if (Array.isArray(entry?.embedding)) { entry.embedding = entry.embedding.map(q); files++; } for (const ch of entry?.chunks || []) { if (Array.isArray(ch?.embedding)) { ch.embedding = ch.embedding.map(q); chunks++; } } } const tmp = file + '.compact-tmp'; fs.writeFileSync(tmp, JSON.stringify(idx)); fs.renameSync(tmp, file); const after = fs.statSync(file).size; console.log(`완료: 파일벡터 ${files} · 청크벡터 ${chunks} · ${(before / 1e6).toFixed(0)} MB → ${(after / 1e6).toFixed(0)} MB`);