Transform Text to Embedding with HuggingFace Sentence Transformers

Sentence Transformers allow you to convert text into high-dimensional vectors, which can be compared for semantic similarity. This is useful for search, clustering, or recommendation systems.

You can encode text with just a few lines of code using the `sentence-transformers` library and a pre-trained model like 'all-MiniLM-L6-v2'.


from sentence_transformers import SentenceTransformer

# Load pre-trained model
model = SentenceTransformer('all-MiniLM-L6-v2')

# Input text
sentences = ["This is an example sentence", "Each sentence is converted"]

# Convert to embeddings
embeddings = model.encode(sentences)

# Print the embedding for the first sentence
print(embeddings[0])