# Getting Started With Embeddings

huggingface.co · Omar Espejel · Jun 23, 2022

**Type:** Tutorial

## 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.

**Tags:** [Machine Learning](https://yomu.fyi/topic/machine-learning), [Open Source](https://yomu.fyi/topic/open-source), [Python](https://yomu.fyi/topic/python), [Search](https://yomu.fyi/topic/search)

[Read original post](https://huggingface.co/blog/getting-started-with-embeddings)
