Loading…
Getting Started With Embeddings
Omar Espejel
Summary
Embeddings represent unstructured information such as text and images as numerical vectors in a shared semantic space. To demonstrate their utility, a simple semantic search engine is built over US Social Security Medicare frequently asked questions. The system generates 384-dimensional vector representations for thirteen FAQ entries by dispatching POST requests to the Hugging Face Inference API using the sentence-transformers/all-MiniLM-L6-v2 model. Incoming user queries are converted into matching vector representations and evaluated against stored dataset vectors using the util.semantic_search function from the Sentence Transformers library. By calculating cosine similarity scores, the system retrieves and ranks the five most semantically relevant questions without requiring custom keyword rules or massive labeled training sets.
Context
Embedding datasets enables search, grouping, and recommendation algorithms across text and images, but generating and managing embedded data has historically been computationally expensive and technically complex for practitioners.
Approach / What changed
Query the Hugging Face Inference API using the sentence-transformers/all-MiniLM-L6-v2 model to encode thirteen Medicare FAQs into 384-dimensional vectors, export the vectors to CSV, and use Sentence Transformers util.semantic_search to find the top matching questions via cosine similarity.
Takeaways
- The sentence-transformers/all-MiniLM-L6-v2 model encodes text inputs into 384-dimensional vector representations that capture semantic meaning.
- The initial call to the Hugging Face feature extraction endpoint can take approximately 20 seconds while the model downloads and installs on the server, after which subsequent inference requests execute much faster.
- Sentence Transformers util.semantic_search relies on cosine similarity by default to compare query embeddings against a dataset corpus, though distance metrics such as the dot product can also be used.
Related reading
Sentence Transformers is joining Hugging Face!
Sentence Transformers is officially transitioning its stewardship from the Ubiquitous Knowledge Processing Lab at TU Darmstadt to Hugging Face. The open-source embedding library was introduced in 2019 by Nils Reimers using a Siamese network architecture to overcome standard BERT limitations on sentence-level semantics. Tom Aarsen of Hugging Face has maintained the project since late 2023 and will continue leading its development. Under Hugging Face, the project retains its Apache 2.0 license and leverages infrastructure such as continuous integration and automated testing. Sentence Transformers currently serves over one million monthly unique users, with more than 16,000 community models publicly hosted on the Hugging Face Hub.
Tom AarsenWelcome EmbeddingGemma, Google's new efficient embedding model