diff --git a/package.json b/package.json index 11a9ce4..b742559 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "axios": "^1.8.3", "better-sqlite3": "^11.9.1", "clsx": "^2.1.0", - "compute-cosine-similarity": "^1.1.0", "drizzle-orm": "^0.40.1", "framer-motion": "^12.23.24", "html-to-text": "^9.0.5", diff --git a/src/lib/utils/computeSimilarity.ts b/src/lib/utils/computeSimilarity.ts index eda2a59..4cf90c8 100644 --- a/src/lib/utils/computeSimilarity.ts +++ b/src/lib/utils/computeSimilarity.ts @@ -1,7 +1,22 @@ -import cosineSimilarity from 'compute-cosine-similarity'; - const computeSimilarity = (x: number[], y: number[]): number => { - return cosineSimilarity(x, y) as number; + if (x.length !== y.length) + throw new Error('Vectors must be of the same length'); + + let dotProduct = 0; + let normA = 0; + let normB = 0; + + for (let i = 0; i < x.length; i++) { + dotProduct += x[i] * y[i]; + normA += x[i] * x[i]; + normB += y[i] * y[i]; + } + + if (normA === 0 || normB === 0) { + return 0; + } + + return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB)); }; export default computeSimilarity;