| 27 | let embeddingsData: { index: EmbeddingIndex; embeddings: Float32Array } | null = null; |
| 28 | |
| 29 | function loadEmbeddings(): { index: EmbeddingIndex; embeddings: Float32Array } { |
| 30 | if (embeddingsData) return embeddingsData; |
| 31 | |
| 32 | if (!existsSync(INDEX_PATH) || !existsSync(EMBEDDINGS_PATH)) { |
| 33 | throw new Error('Embeddings not found. Run "pnpm embed" first to generate them.'); |
| 34 | } |
| 35 | |
| 36 | console.log('Loading pre-computed embeddings...'); |
| 37 | const index: EmbeddingIndex = JSON.parse(readFileSync(INDEX_PATH, 'utf-8')); |
| 38 | const buffer = readFileSync(EMBEDDINGS_PATH); |
| 39 | const embeddings = new Float32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 4); |
| 40 | |
| 41 | embeddingsData = { index, embeddings }; |
| 42 | console.log(`Loaded ${index.count} embeddings (${(buffer.length / 1024 / 1024).toFixed(1)} MB)`); |
| 43 | |
| 44 | return embeddingsData; |
| 45 | } |
| 46 | |
| 47 | // Cache for query embeddings |
| 48 | const queryCache = new Map<string, number[]>(); |