refactor(embeddings): replace local cosineSimilarity with @bytelyst/palace re-export
Remove duplicate cosine similarity implementation in favor of the shared @bytelyst/palace primitive. All consumers import from embeddings.ts unchanged.
This commit is contained in:
parent
7d7e445135
commit
ba89cfd643
@ -24,23 +24,9 @@ export async function embedText(text: string): Promise<number[] | null> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute cosine similarity between two embedding vectors.
|
* Re-export cosine similarity from @bytelyst/palace shared primitives.
|
||||||
* Returns a value between -1 and 1 (1 = identical).
|
|
||||||
*/
|
*/
|
||||||
export function cosineSimilarity(a: number[], b: number[]): number {
|
export { cosineSimilarity } from '@bytelyst/palace';
|
||||||
if (a.length !== b.length || a.length === 0) return 0;
|
|
||||||
let dot = 0;
|
|
||||||
let magA = 0;
|
|
||||||
let magB = 0;
|
|
||||||
for (let i = 0; i < a.length; i++) {
|
|
||||||
dot += a[i] * b[i];
|
|
||||||
magA += a[i] * a[i];
|
|
||||||
magB += b[i] * b[i];
|
|
||||||
}
|
|
||||||
const denom = Math.sqrt(magA) * Math.sqrt(magB);
|
|
||||||
if (denom === 0) return 0;
|
|
||||||
return dot / denom;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strip HTML and normalize whitespace for embedding input.
|
* Strip HTML and normalize whitespace for embedding input.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user