Add local AI and Qdrant vector backends

This commit is contained in:
“BeeRad”
2026-05-02 09:57:57 +10:00
parent 00f0afb8f4
commit 782ace9a34
37 changed files with 1440 additions and 321 deletions
+13 -3
View File
@@ -20,7 +20,17 @@ fi
DB_DIR="$(dirname "$DB_PATH")"
DB_NAME="$(basename "$DB_PATH")"
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
VEC_EXTENSION_PATH="${SQLITE_VEC_EXTENSION_PATH:-$ROOT_DIR/vendor/sqlite-extensions/vec0.dylib}"
case "$(uname -s)" in
Darwin*) VEC_EXT="dylib" ;;
MINGW*|MSYS*|CYGWIN*) VEC_EXT="dll" ;;
*) VEC_EXT="so" ;;
esac
VEC_EXTENSION_PATH="${SQLITE_VEC_EXTENSION_PATH:-$ROOT_DIR/vendor/sqlite-extensions/vec0.$VEC_EXT}"
EMBEDDING_DIMENSIONS="${EMBEDDING_DIMENSIONS:-1536}"
if ! [[ "$EMBEDDING_DIMENSIONS" =~ ^[0-9]+$ ]] || [ "$EMBEDDING_DIMENSIONS" -le 0 ]; then
echo "Invalid EMBEDDING_DIMENSIONS: $EMBEDDING_DIMENSIONS" >&2
exit 1
fi
TS=$(date +"%Y%m%d_%H%M%S")
RAW_BACKUP_DIR="$DB_DIR/working/fts_repair_${TS}"
REBUILT_DB="$DB_DIR/${DB_NAME}.rebuilt.${TS}"
@@ -36,12 +46,12 @@ if [ -f "$VEC_EXTENSION_PATH" ]; then
VEC_SQL_BODY="
CREATE VIRTUAL TABLE vec_nodes USING vec0(
node_id INTEGER PRIMARY KEY,
embedding FLOAT[1536]
embedding FLOAT[$EMBEDDING_DIMENSIONS]
);
CREATE VIRTUAL TABLE vec_chunks USING vec0(
chunk_id INTEGER PRIMARY KEY,
embedding FLOAT[1536]
embedding FLOAT[$EMBEDDING_DIMENSIONS]
);
"
+6 -2
View File
@@ -554,17 +554,21 @@ function ensureCoreSchema(db) {
function tryInitVectorTables(db, dbPath) {
const extension = process.platform === 'darwin' ? 'dylib' : process.platform === 'win32' ? 'dll' : 'so';
const extensionPath = process.env.SQLITE_VEC_EXTENSION_PATH || path.join(repoDir, 'vendor', 'sqlite-extensions', `vec0.${extension}`);
const dimensions = Number(process.env.EMBEDDING_DIMENSIONS || '1536');
if (!Number.isInteger(dimensions) || dimensions <= 0) {
throw new Error(`Invalid EMBEDDING_DIMENSIONS="${process.env.EMBEDDING_DIMENSIONS}"`);
}
try {
db.loadExtension(extensionPath);
db.exec(`
CREATE VIRTUAL TABLE IF NOT EXISTS vec_nodes USING vec0(
node_id INTEGER PRIMARY KEY,
embedding FLOAT[1536]
embedding FLOAT[${dimensions}]
);
CREATE VIRTUAL TABLE IF NOT EXISTS vec_chunks USING vec0(
chunk_id INTEGER PRIMARY KEY,
embedding FLOAT[1536]
embedding FLOAT[${dimensions}]
);
`);
log(`Initialized sqlite-vec tables using ${extensionPath}`);
+38
View File
@@ -0,0 +1,38 @@
import { createEmbeddingProvider } from '@/services/embedding/provider';
import { createUtilityLlmProvider } from '@/services/llm/provider';
import { getSQLiteClient } from '@/services/database/sqlite-client';
import { getVectorBackend } from '@/services/vectorBackend/factory';
async function main() {
const sqlite = getSQLiteClient();
const utility = createUtilityLlmProvider();
const embedding = createEmbeddingProvider();
const vectorBackend = await getVectorBackend();
const [utilityHealth, embeddingHealth, vectorHealth] = await Promise.all([
utility.healthCheck(),
embedding.healthCheck(),
vectorBackend.healthCheck(),
]);
const profileStatus = sqlite.getEmbeddingProfileStatus();
console.log(JSON.stringify({
ok: utilityHealth.ok && embeddingHealth.ok && vectorHealth.ok && !profileStatus.rebuild_required,
utility_llm: utilityHealth,
embedding_provider: embeddingHealth,
vector_backend: vectorHealth,
embedding_profile: profileStatus,
next_action: profileStatus.rebuild_required
? 'Run npm run rebuild:embeddings after confirming your embedding provider/model/dimensions.'
: 'Local AI/vector configuration is ready.',
}, null, 2));
if (!utilityHealth.ok || !embeddingHealth.ok || !vectorHealth.ok || profileStatus.rebuild_required) {
process.exitCode = 1;
}
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
+71
View File
@@ -0,0 +1,71 @@
import { getSQLiteClient } from '@/services/database/sqlite-client';
import { NodeEmbedder } from '@/services/typescript/embed-nodes';
import { UniversalEmbedder } from '@/services/typescript/embed-universal';
async function maybeRecreateQdrantCollections() {
if (process.env.VECTOR_BACKEND !== 'qdrant' || process.env.QDRANT_RECREATE_COLLECTIONS !== 'true') {
return;
}
const baseUrl = (process.env.QDRANT_URL || 'http://localhost:6333').replace(/\/+$/, '');
const headers = process.env.QDRANT_API_KEY ? { 'api-key': process.env.QDRANT_API_KEY } : undefined;
const collections = [
process.env.QDRANT_CHUNKS_COLLECTION || 'rah_chunks',
process.env.QDRANT_NODES_COLLECTION || 'rah_nodes',
];
for (const collection of collections) {
const response = await fetch(`${baseUrl}/collections/${encodeURIComponent(collection)}`, {
method: 'DELETE',
headers,
});
if (!response.ok && response.status !== 404) {
const detail = await response.text().catch(() => response.statusText);
throw new Error(`Failed to delete Qdrant collection ${collection}: ${detail}`);
}
console.log(`[rebuild-embeddings] Recreated Qdrant collection on next upsert: ${collection}`);
}
}
async function main() {
const sqlite = getSQLiteClient();
await maybeRecreateQdrantCollections();
const nodeRows = sqlite.query<{ id: number; source?: string | null }>(`
SELECT id, source
FROM nodes
ORDER BY id
`).rows;
console.log(`[rebuild-embeddings] Rebuilding node embeddings for ${nodeRows.length} nodes`);
const nodeEmbedder = new NodeEmbedder();
try {
await nodeEmbedder.embedNodes({ forceReEmbed: true, verbose: true });
} finally {
nodeEmbedder.close();
}
const sourceRows = nodeRows.filter((node) => typeof node.source === 'string' && node.source.trim().length > 0);
console.log(`[rebuild-embeddings] Rebuilding chunk embeddings for ${sourceRows.length} nodes with source text`);
const chunkEmbedder = new UniversalEmbedder();
try {
let processed = 0;
for (const node of sourceRows) {
await chunkEmbedder.processNode({ nodeId: node.id, verbose: false });
processed += 1;
if (processed % 10 === 0 || processed === sourceRows.length) {
console.log(`[rebuild-embeddings] Chunked ${processed}/${sourceRows.length}`);
}
}
} finally {
chunkEmbedder.close();
}
sqlite.markEmbeddingProfileCurrent();
console.log('[rebuild-embeddings] Active embedding/vector profile recorded.');
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
+9 -4
View File
@@ -8,13 +8,18 @@ if (!dbPath) {
}
const db = new Database(dbPath);
const vecPath = path.join(process.cwd(), 'vendor', 'sqlite-extensions', 'vec0.dylib');
const ext = process.platform === 'darwin' ? 'dylib' : process.platform === 'win32' ? 'dll' : 'so';
const vecPath = process.env.SQLITE_VEC_EXTENSION_PATH || path.join(process.cwd(), 'vendor', 'sqlite-extensions', `vec0.${ext}`);
const dimensions = Number(process.env.EMBEDDING_DIMENSIONS || '1536');
if (!Number.isInteger(dimensions) || dimensions <= 0) {
throw new Error(`Invalid EMBEDDING_DIMENSIONS="${process.env.EMBEDDING_DIMENSIONS}"`);
}
db.loadExtension(vecPath);
db.exec(`
CREATE VIRTUAL TABLE IF NOT EXISTS vec_nodes USING vec0(node_id INTEGER PRIMARY KEY, embedding FLOAT[1536]);
CREATE VIRTUAL TABLE IF NOT EXISTS vec_chunks USING vec0(chunk_id INTEGER PRIMARY KEY, embedding FLOAT[1536]);
CREATE VIRTUAL TABLE IF NOT EXISTS vec_nodes USING vec0(node_id INTEGER PRIMARY KEY, embedding FLOAT[${dimensions}]);
CREATE VIRTUAL TABLE IF NOT EXISTS vec_chunks USING vec0(chunk_id INTEGER PRIMARY KEY, embedding FLOAT[${dimensions}]);
`);
console.log('✓ vec tables ensured');
db.close();
db.close();