← Back to list

Learning Series: A Beginner’s Guide to AI Embeddings and Local Models

This is a concept learning series of new Ai paradigm. lets go!!

Madhu Nair · 2026-08-02 15:45 · 0 claps · 4.7 min read
#gwen3 #llm-applications #embedded #nomic-embed-text #learning
Open on Medium ↗
Wiki topics: LLM · Large Language Models RAG · RAG & Retrieval GEN · Genomics & Sequencing EDU · Education & Learning

Learning Series: A Beginner’s Guide to AI Embeddings and Local Models

This is a concept learning series of new Ai paradigm. lets go!!

Instead of just looking at the spelling of a word, an embedding captures the word’s actual context and intent. It is the bridge that allows an AI to stop treating text like random letters and start understanding it mathematically

In simple words, an embedding is a way of translating a word or sentence into a list of numbers that a computer can understand

Think of it like giving a word a specific set of GPS coordinates on a giant, multi-dimensional map of meaning

Before we dive into the code, let’s break down the exact lifecycle of how text is transformed into mathematical meaning.

+========================================================================+
|                        EMBEDDING DATA FLOW                             |
+========================================================================+
|                                                                        |
|  [START] Raw Text                                                      |
|    |                                                                   |
|    +---> "i got a fine"                                                |
|                                                                        |
|  [STEP 1] Tokenization                                                 |
|    |                                                                   |
|    +---> ["i", "got", "a", "fine"]                                     |
|                                                                        |
|  [STEP 2] Assign Token IDs                                             |
|    |                                                                   |
|    +---> [1, 3, 4, 5] (Numerical mapping from vocabulary)              |
|                                                                        |
|  [STEP 3] Transformer Model & Self-Attention                           |
|    |                                                                   |
|    +---> Calculates context weights. "fine" is mathematically          |
|          linked to "got a" (indicating a penalty, not an emotion).     |
|                                                                        |
|  [STEP 4] Generate Dense Vectors                                       |
|    |                                                                   |
|    +---> [0.89, 0.21, -0.45, ...] (High-dimensional semantic array)    |
|                                                                        |
|  [STEP 5] Calculate Cosine Similarity                                  |
|    |                                                                   |
|    +---> Compares angle against other vectors to measure meaning.      |
|                                                                        |
|  [END] Semantic Output Score                                           |
+========================================================================+

The Journey of a Word

The transformation from raw text to AI comprehension happens in a matter of milliseconds, but the process under the hood is fascinating:

1. Tokenization

  • The Concept: Breaking raw text into smaller chunks (words or subwords).
  • **Example:**“I caught a bass” becomes the token list: ["I", "caught", "a", "bass"].
  • “I play the bass” becomes: ["I", "play", "the", "bass"].
  • (For complex words, it might use subwords: “unbelievable” could become ["un", "believ", "able"]).`

2. Token ID Assignment

  • The Concept: Converting text strings into raw numerical identifiers based on a predefined dictionary.
  • **Example: **The system looks up the tokens in its vocabulary.
  • *["I", "caught", "a", "bass"] translates to an array of numbers like [104, 2940, 10, 4892].*
  • Notice that the word “bass” is simply assigned ID 4892 in both sentences at this stage. The model doesn't know the difference between the fish and the instrument yet; it just knows the letters b-a-s-s correspond to that specific ID number.

3. Transformer Processing

  • The Concept: Reading the entire sequence of tokens simultaneously rather than sequentially.
  • Example: Instead of processing 104 ("I"), waiting, and then processing 2940 ("caught"), the Transformer ingests the entire block [104, 2940, 10, 4892] at the exact same moment. This bidirectional reading allows the system to look ahead at the word "bass" at the exact same time it is looking back at the word "caught."

4. Self-Attention Mechanism

  • The Concept: Determining context by weighing how much each word relates to the words around it.
  • **Example:**In the first sentence, the attention mechanism calculates that the word “bass” has a very strong mathematical relationship to the word “caught.” It applies heavy “attention weights” linking the two, signaling an aquatic or hunting context.
  • In the second sentence, it calculates a heavy weight between “bass” and “play,” shifting the context entirely toward music.

5. Dense Vector Generation

  • The Concept: Converting the contextualized token into a final, high-dimensional mathematical coordinate (a vector).
  • Example: Because of the attention weights applied in the previous step, the final mathematical representation of “bass” diverges entirely:
  • The “fishing bass” becomes a vector of numbers pointing in one direction: [0.12, -0.85, 0.44, ...]
  • The “musical bass” becomes a completely different vector of numbers: [-0.99, 0.21, 0.05, ...]
  • Even though they started as the exact same string of text, their final numerical arrays look nothing alike.

6. Cosine Similarity

  • The Concept: Measuring the angle between vectors to determine if they mean the same thing.
  • **Example:*If we take the dense vector for our “fishing bass” and compare it to the dense vector for the word “trout,” the math will show they are pointing in almost the exact same direction (a very narrow angle), resulting in a high similarity score of 0.92*.
  • If we compare the “fishing bass” vector to the word “guitar,” the math will show them pointing in entirely different directions (a very wide angle), resulting in a low similarity score of 0.15.

Getting Started with Local Models

You don’t need a massive cloud server to play with this technology. You can run embeddings and chat models locally on your own machine using Ollama.

Step 1: Install Ollama

Ollama is a lightweight tool that lets you run large language models locally and is the best way to learn llm and understand how it works.

  • Go to ollama.com and download the installer for macOS, Windows, or Linux.
  • Run the installer. Once finished, open your terminal or command prompt.

Step 2: Pull the Models

We need two models for our project: an embedding model to convert text to vectors, and a chat model to generate responses. Run these commands in your terminal:

# Pull the embedding model (excellent for vector math)
ollama pull nomic-embed-text
# Pull the chat model (you can swap this for llama3 or qwen)
ollama pull gwen3.5:4b

Step 3: Install the Python Libraries

You’ll need the official Ollama library, plus numpy and scikit-learn for the math:

pip install ollama numpy scikit-learn

Building the Logic: Routing by Similarity

Now, let’s put it all together. We are going to build a script that embeds a user’s prompt, checks if it relates to a specific financial “route,” and if the cosine similarity is high enough, passes it to the chat model.

Create a file called app.py and drop in this code:

import ollama
import numpy as np
from sklearn.metrics.pairwise import cosine_similaritydef main():
    prompt = "i got a fine"


# 1. Embed the user's prompt

# Note: ollama.embed() is the modern API call for the python library

    prompt_response = ollama.embed(model="nomic-embed-text", input=prompt)
    prompt_emb = np.array(prompt_response["embeddings"]).reshape(1, -1)
    print(f"Generating embeddings for prompt...")

# 2. Embed the target route's description

    route_desc = "economical,money,payment,bill,fine,tax,fee,cost,price,charge,expense,budget,spending"
    route_response = ollama.embed(model="nomic-embed-text", input=route_desc)
    route_emb = np.array(route_response["embeddings"]).reshape(1, -1)

# 3. Calculate Cosine Similarity

    similarity = cosine_similarity(prompt_emb, route_emb)[0][0]
    print(f"Similarity Score: {similarity:.4f}\n")

# 4. Route to the local model if the score clears the threshold

    if similarity > 0.55:
        print(f"Threshold met! Routing to gwen3.5:4b...\n")


# Stream the response back locally
        stream = ollama.chat(
            model="gwen3.5:4b", 
            messages=[{"role": "user", "content": prompt}],
            stream=True
        )
        for chunk in stream:
            print(chunk["message"]["content"], end="", flush=True)
        print("\n")
    else:
        print("Similarity too low. Fallback triggered.")
    if __name__ == "__main__":
        main()

By calculating similarity before asking the LLM to generate text, you save massive amounts of compute power. You’re using the math of the embeddings to make a lightning-fast routing decision, and only waking up the heavy-duty generative model when you’re sure it’s necessary.


메타데이터
post_id
a3f47a0d94be
slug
learning-series-a-beginners-guide-to-ai-embeddings-and-local-models-a3f47a0d94be
url
https://medium.com/@madhusnair340/learning-series-a-beginners-guide-to-ai-embeddings-and-local-models-a3f47a0d94be
canonical_url
https://medium.com/@madhusnair340/learning-series-a-beginners-guide-to-ai-embeddings-and-local-models-a3f47a0d94be
author_url
https://medium.com/@madhusnair340
status
ok
fetched_at
2026-08-06 02:49:13