|
|
Generating Embeddings with Ollama for Semantic Search
Author: Venkata Sudhakar
Ollama supports generating text embeddings locally using models like nomic-embed-text, which is a compact model optimized specifically for producing high-quality embeddings. Embeddings are numerical vector representations of text that capture semantic meaning, enabling similarity comparisons between pieces of text. At ShopMax India, locally generated embeddings are used to power offline product search in the warehouse management system where internet connectivity is unreliable. The ollama.embeddings() function takes a model name and a text prompt and returns a vector of floating point numbers. You can then compute cosine similarity between vectors to find the most semantically related items. This entire pipeline runs on local hardware with no data leaving the machine, which is critical for applications involving confidential inventory and pricing data. The below example shows how to generate embeddings with Ollama and perform semantic search over a product list.
Then use the embeddings in Python as shown below.
It gives the following output,
Query: I want noise cancelling headphones for travel
Best match: ShopMax TurboCharge wireless earbuds 30hr battery
Score: 0.8134
For better performance at scale, pre-compute and cache all product embeddings rather than generating them on every query. The nomic-embed-text model produces 768-dimensional vectors that offer a good balance of quality and speed on CPU. ShopMax India can store these embeddings in a local SQLite database and refresh them nightly as the product catalog changes, enabling fast offline semantic search without any external API dependency.
|
|