@mondaydotcomorg/atp-runtime
Version:
Runtime SDK injected into sandbox for Agent Tool Protocol
30 lines (29 loc) • 844 B
JavaScript
/**
/**
* Calculate cosine similarity between two vectors
*/
export function cosineSimilarity(vec1, vec2) {
if (vec1.length !== vec2.length) {
throw new Error(`Vectors must have the same length (${vec1.length} vs ${vec2.length})`);
}
let dotProduct = 0;
let norm1 = 0;
let norm2 = 0;
for (let i = 0; i < vec1.length; i++) {
dotProduct += vec1[i] * vec2[i];
norm1 += vec1[i] * vec1[i];
norm2 += vec2[i] * vec2[i];
}
const denominator = Math.sqrt(norm1) * Math.sqrt(norm2);
if (denominator === 0) {
return 0;
}
return dotProduct / denominator;
}
/**
* Generate a unique ID for an embedding
*/
export function generateEmbeddingId(index = 0) {
return `emb_${Date.now()}_${index}_${Math.random().toString(36).slice(2)}`;
}
//# sourceMappingURL=utils.js.map