Beginner LevelAI, LLMs, Embeddings & MCP
What are Vector Embeddings and Cosine Similarity in AI Semantic Search?
❓ Interview Prompt: 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:
•
Captures semantic context rather than literal keyword matches.
•
Enables similarity search across different languages and paraphrased expressions.
Code Implementation Example
typescript
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));
}💡Cosine similarity calculation for vector similarity comparison.
🎯 Interview Key Takeaway
Vector embeddings capture semantic context rather than keyword literal matches, powering modern RAG systems.
#AI#Embeddings#Vector Search#Cosine Similarity#Beginner
Back to all AI, LLMs, Embeddings & MCP Questions→