Skip to content

    Embeddings & RAG

    AI · Advanced

    Definition

    Embeddings turn text into vectors; RAG retrieves relevant documents by vector similarity and feeds them to an LLM.

    Lets AI answer from your own data with fewer hallucinations.

    Diagram

    docs → vectorsquery → vectorcosine similarityLLM answer

    Flowchart

    Split documentsEmbed chunksStore in vector DBEmbed the questionRetrieve top matchesGenerate answer

    Example: Cosine similarity

    import math
    a, b = [1, 2], [2, 4]
    dot = sum(x*y for x, y in zip(a, b))
    print(round(dot / (math.hypot(*a) * math.hypot(*b)), 2))

    Output: 1.0

    Key points

    • Chunk size affects retrieval quality
    • Cite retrieved sources

    Interview questions

    Why RAG instead of fine-tuning?

    It is cheaper, updates instantly with new data and shows sources.

    More AI topics