const queryEmbedding = [{{4.data.embedding}}];
const rows ={{40.json}}; // Array of row objects from Google Sheets
function cosineSimilarity(a, b) {
let dot = 0.0, normA = 0.0, normB = 0.0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
}
const matches = ;
for (const row of rows) {
try {
// Try to get embedding column (adjust if column name is different)
const rawEmbedding = row.Embedding;
const embedding = rawEmbedding;
const similarity = cosineSimilarity(queryEmbedding, embedding);
matches.push({
Question: row.Question,
Answer: row.Answer,
Category: row.Category,
Similarity: similarity
});
} catch (err) {
result = {“matches”: err};
}
}
matches.sort((a, b) => b.Similarity - a.Similarity);
const topK = matches.slice(0, 3);
const formattedMatches = topK.map(m =>
Q: ${m.Question}\nA: ${m.Answer}
).join(“\n\n”);
result= { matches: topK };
Why is it not able to parse the JSON even if I write const embedding = JSON.parse(rawEmbedding);