Menu

Search by Meaning, Not Words: Embeddings Explained in 30 Lines of Python
📰

Search by Meaning, Not Words: Embeddings Explained in 30 Lines of Python

DEV Community: python·Muiduzzaman Lipu·12 days ago
#uSGC9hSy
#devtopython#devto#dev#embed#query
Long article (961 words) — no AI summary yet.
Reading 0:00
15s threshold

A user types "How do I cancel my plan?" Your help page says "To unsubscribe, open Billing and click End subscription."

Same meaning, zero shared words , so keyword search finds nothing. In this post we fix that with embeddings, the idea behind semantic search and RAG. It takes about 30 lines of plain Python (no libraries) and a free model running locally with Ollama.

Video version:

Keyword search compares spelling

def keyword_search ( query ) : words = set ( query . lower ( ) . replace ( "?" , "" ) . split ( ) ) return max ( DOCS , key = lambda d : len ( words & set ( d . lower ( ) . replace ( "." , "" ) . replace ( "," , "" ) . split ( ) ) ) )

Enter fullscreen mode

Exit fullscreen mode

For each of my four test questions below, the correct help article shared no words at all with the query. Keyword search has nothing to go on.

What's an embedding?

An embedding model turns text into a list of numbers (a vector). nomic-embed-text returns 768 of them.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.