1. What are Vector Embeddings and Cosine Similarity in AI Semantic Search?
❓ Context: Explain how text is converted into high-dimensional vector space and how Cosine Similarity calculates semantic closeness.
Vector embeddings are dense floating-point numerical representations of data (text, code, images) in a multi-dimensional space (e.g. 768 or 1536 dimensions) generated by AI models (such as Gemini or OpenAI text-embedding-004).
Cosine Similarity:
Cosine similarity measures the cosine of the angle between two vectors in n-dimensional space. If vectors are normalized, it is equal to the dot product. Values close to 1.0 indicate identical semantic meaning, while 0 indicates orthogonality (unrelated concepts).
Key Features:
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] * vecA[i];
normB += vecB[i] * vecB[i];
}
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}Vector embeddings capture semantic context rather than keyword literal matches, powering modern RAG systems.