rag-whatsapp / src /vectorstore.js
DHEIVER's picture
RAG WhatsApp 100% local no HF Space (Ollama, sem token)
19caa49
Raw
History Blame Contribute Delete
680 Bytes
import { readFileSync, writeFileSync, existsSync } from 'node:fs';
import { config } from './config.js';
import { cosine } from './embeddings.js';
// Vetor store simples em arquivo JSON: { items: [{ id, text, source, vector }] }
export function load() {
if (!existsSync(config.storePath)) return { items: [] };
return JSON.parse(readFileSync(config.storePath, 'utf8'));
}
export function save(store) {
writeFileSync(config.storePath, JSON.stringify(store));
}
export function search(store, queryVector, k = config.topK) {
return store.items
.map((it) => ({ ...it, score: cosine(queryVector, it.vector) }))
.sort((a, b) => b.score - a.score)
.slice(0, k);
}