Embeddings and Vector Similarity Fundamentals
To build advanced GenAI applications (like custom chatbots that can answer questions about your private company data), we must move beyond traditional keyword search. We need Semantic Search.
Semantic Search understands the meaning and context of words. If a user searches for "How do I fix my broken screen?", a semantic search will successfully retrieve a document titled "Display Repair Guidelines", even though they share zero exact keywords.
What is a Vector Embedding?
Machine learning models cannot read English words; they only understand numbers.
An Embedding Model takes a piece of text (a word, a sentence, or a whole document) and translates its semantic meaning into a long list of numbers called a Vector.
For example, the text "Apple" might become a vector like:
[0.45, -0.12, 0.89, 0.01, ... ]
Because these numbers represent meaning:
- The vector for "Apple" and "Banana" will be mathematically close to each other.
- The vector for "Apple" and "Carburetor" will be mathematically far apart.
The Snowflake VECTOR Data Type
To support Semantic Search, Snowflake introduced a native VECTOR data type. A VECTOR is a fixed-length array of numeric values (usually FLOAT).
When you create a table to store your text documents, you can add a VECTOR column to store their embeddings.
-- Create a table for our company knowledge base
CREATE OR REPLACE TABLE KNOWLEDGE_BASE (
DOC_ID INT,
CATEGORY VARCHAR,
TEXT_CONTENT VARCHAR,
TEXT_EMBEDDING VECTOR(FLOAT, 768) -- Store the embedding vector here
);
Note: The 768 represents the number of dimensions in the vector. Different embedding models produce different sized vectors. The Snowflake snowflake-arctic-embed-m model produces 768 dimensions.
Generating Embeddings with CORTEX.EMBED_TEXT
To fill our TEXT_EMBEDDING column, we don't need to build our own embedding model. We can use the native SNOWFLAKE.CORTEX.EMBED_TEXT() function.
Let's insert a document and generate its embedding on the fly!
INSERT INTO KNOWLEDGE_BASE (DOC_ID, CATEGORY, TEXT_CONTENT, TEXT_EMBEDDING)
SELECT
1,
'HR Policy',
'Employees are granted 20 days of paid time off (PTO) per calendar year.',
SNOWFLAKE.CORTEX.EMBED_TEXT(
'snowflake-arctic-embed-m', -- The embedding model to use
'Employees are granted 20 days of paid time off (PTO) per calendar year.'
);
INSERT INTO KNOWLEDGE_BASE (DOC_ID, CATEGORY, TEXT_CONTENT, TEXT_EMBEDDING)
SELECT
2,
'IT Policy',
'To request a replacement laptop, submit a ticket to the IT Helpdesk via Jira.',
SNOWFLAKE.CORTEX.EMBED_TEXT(
'snowflake-arctic-embed-m',
'To request a replacement laptop, submit a ticket to the IT Helpdesk via Jira.'
);
Your KNOWLEDGE_BASE table now contains the raw text and its mathematical representation.
Performing a Semantic Search
Now, imagine a user asks a question: "How do I get a new computer?"
Notice that the words "new" and "computer" do not appear in our IT Policy document (which uses the words "replacement" and "laptop"). A standard SQL WHERE TEXT_CONTENT LIKE '%new computer%' would fail to find the document.
Instead, we perform a Semantic Search using Cosine Similarity:
- We convert the user's question into a Vector.
- We compare the question's Vector to every Vector in our database.
- We calculate the "distance" between them using
VECTOR_COSINE_SIMILARITY(). A score closer to1.0means highly similar.
-- The user's question
SET user_question = 'How do I get a new computer?';
-- Perform the semantic search
SELECT
DOC_ID,
TEXT_CONTENT,
-- Calculate similarity between the question embedding and document embeddings
VECTOR_COSINE_SIMILARITY(
TEXT_EMBEDDING,
SNOWFLAKE.CORTEX.EMBED_TEXT('snowflake-arctic-embed-m', $user_question)
) AS SIMILARITY_SCORE
FROM KNOWLEDGE_BASE
ORDER BY SIMILARITY_SCORE DESC
LIMIT 1; -- Get the top 1 most relevant document
Expected Outcome:
| DOC_ID | TEXT_CONTENT | SIMILARITY_SCORE |
|---|---|---|
| 2 | To request a replacement laptop, submit a ticket to the IT Helpdesk via Jira. | 0.842 |
By using Cortex and the Vector data type, Snowflake successfully understood that "new computer" means the same thing as "replacement laptop" and returned the correct IT policy document!
In the next module, we will combine this Semantic Search capability with an LLM to build a complete RAG Architecture.