auth-yes/archive/.forum/poc-g2/vector_db_poc.ts

106 lines
2.8 KiB
TypeScript

import { Database } from "jsr:@db/sqlite";
import {
assert,
assertEquals,
} from "https://deno.land/std@0.224.0/testing/asserts.ts";
/**
* Proof of Concept: Embedded Vector Database (Gen 2)
*
* Demonstrates the concept of fuzzy semantic retrieval using an actual
* SQLite database. While we are not loading a C extension like `sqlite-vec`
* directly here to keep the PoC universally executable without native build
* dependencies, we simulate it via SQL and User Defined Functions (UDF)
* provided by Deno's `jsr:@db/sqlite`.
*/
// A simple mock for cosine similarity of 1D arrays
function cosineSimilarity(vecA: number[], vecB: number[]): number {
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < vecA.length; i++) {
dotProduct += vecA[i] * vecB[i];
normA += vecA[i] ** 2;
normB += vecB[i] ** 2;
}
if (normA === 0 || normB === 0) return 0;
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
if (import.meta.main) {
console.log("Running Embedded Vector Database PoC (Gen 2) tests...");
try {
const db = new Database(":memory:");
// Create a user-defined function in SQLite to perform vector similarity!
db.function("vec_distance", (aStr: string, bStr: string) => {
const vecA = JSON.parse(aStr) as number[];
const vecB = JSON.parse(bStr) as number[];
return cosineSimilarity(vecA, vecB);
});
db.exec(`
CREATE TABLE documents (
id TEXT PRIMARY KEY,
text TEXT,
vector TEXT
);
`);
const insert = db.prepare(
"INSERT INTO documents (id, text, vector) VALUES (?, ?, ?)",
);
insert.run(
"docs-1",
"How to run the server",
JSON.stringify([0.8, 0.1, 0.1, 0.0]),
);
insert.run(
"docs-2",
"Database connection logic",
JSON.stringify([0.1, 0.9, 0.2, 0.1]),
);
insert.run(
"telemetry-1",
"Server latency spikes",
JSON.stringify([0.2, 0.1, 0.9, 0.3]),
);
insert.finalize();
// Query representing "I have a slow server issue"
const queryVectorStr = JSON.stringify([0.3, 0.0, 0.9, 0.2]);
console.log("Querying Vector DB...");
const results = db.prepare(`
SELECT id, text, vec_distance(vector, ?) as score
FROM documents
ORDER BY score DESC
`).all(queryVectorStr) as { id: string; text: string; score: number }[];
console.log(
"Top result:",
results[0].text,
`(Score: ${results[0].score.toFixed(2)})`,
);
assert(
results[0].score > 0.8,
"The telemetry doc should be the highest match",
);
assertEquals(results[0].id, "telemetry-1");
console.log(
"✅ Embedded Vector DB PoC (Gen 2) successful: Real SQLite fuzzy semantic match via UDF.",
);
db.close();
} catch (err) {
console.error("❌ Embedded Vector DB PoC (Gen 2) failed:", err);
Deno.exit(1);
}
}