█

AI/TLDR

मुझे पिलाएं कॉफ़ी
मुझे एक कॉफ़ी पिलाएँ

Sentence Transformers

Python में टेक्स्ट embeddings की गणना करें, उन्हें प्रशिक्षित करें और rerank करें

Embedding Models & Inferenceओपन सोर्स
ताज़ा
v6.0
अपडेट किया गया
18 अग॰ 2026
भाषा
Python
कवरेज
2 stories
$pip install -U sentence-transformers

नया क्या है

v6.018 अग॰ 2026

Sentence Transformers v6.0 adds MultiVectorEncoder, a ColBERT-style late-interaction model type keeping one vector per token; built with LightOn, it loads PyLate and Stanford ColBERT checkpoints.

ताज़ा खबरें

संक्षेप में

Sentence Transformers क्या है?

Sentence Transformers Python में टेक्स्ट embeddings की गणना करें, उन्हें प्रशिक्षित करें और rerank करें. यह Retrieval & Knowledge (RAG) टूल्स की Embedding Models & Inference श्रेणी में आता है; ओपन सोर्स है और Python में लिखा गया है। शुरू करने के लिए: इंस्टॉल करें.

अपडेट किया गया Markdown वर्शन ↗

सारांश

Sentence Transformers एक Python फ्रेमवर्क है जो sentence, reranker (cross-encoder) और sparse encoder मॉडल की मदद से text embeddings निकालता है। आप एक pretrained मॉडल लोड करते हैं, अपने texts पर encode() कॉल करते हैं, और vectors वापस पाते हैं जिनकी तुलना किसी similarity function से कर सकते हैं। यह Hugging Face पर 15,000 से ज़्यादा pretrained मॉडल के साथ काम करता है, जिनमें MTEB leaderboard के कई मॉडल शामिल हैं।

यह उन developers के लिए है जो semantic search, retrieval और text-similarity फ़ीचर बना रहे हैं, साथ ही उन टीमों के लिए भी जो अपने खुद के embedding और reranker मॉडल प्रशिक्षित करना या finetune करना चाहती हैं। यही library उन dense embeddings को भी कवर करती है जिनसे candidate documents खोजे जाते हैं, और उन cross-encoder rerankers को भी जिनसे उन candidates को ज़्यादा सटीक तरीके से score किया जाता है।

Embeddings श्रेणी में यह text को vectors में बदलने और results को rerank करने के लिए standard high-level toolkit है। यह keyword-style sparse representations के लिए sparse encoder मॉडल (जैसे SPLADE) को भी सपोर्ट करता है, इसलिए आप एक ही pipeline में dense, sparse और reranking स्टेज मिला सकते हैं।

यह क्या करता है

  • एक लाइन वाली model.encode() कॉल से dense text embeddings की गणना करें, जो numpy arrays लौटाती है
  • CrossEncoder reranker मॉडल के साथ predict() या rank() के जरिए query-passage pairs को score और rank करें
  • SPLADE जैसे SparseEncoder मॉडल से sparse embeddings बनाएं, जिनमें sparsity stats भी शामिल हैं
  • model.similarity() के जरिए embeddings की तुलना करने के लिए बिल्ट-इन similarity helpers
  • Hugging Face पर 15,000+ pretrained मॉडल तक पहुंच, जो 100+ भाषाओं को कवर करते हैं
  • अपने खुद के embedding, reranker और sparse encoder मॉडल प्रशिक्षित करें या finetune करें

शुरू करें

पैकेज इंस्टॉल करें, फिर एक pretrained मॉडल लोड करें और कुछ text encode करें।

इंस्टॉल करें

PyPI से इंस्टॉल करें। Python 3.10+, PyTorch 1.11.0+ और transformers v4.41.0+ की सिफ़ारिश की जाती है।

bashbash
pip install -U sentence-transformers

Embeddings की गणना करें

Sentence Transformer मॉडल लोड करें और sentences की list को vectors में encode करें।

pythonpython
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

sentences = [
    "The weather is lovely today.",
    "It's so sunny outside!",
    "He drove to the stadium.",
]
embeddings = model.encode(sentences)
print(embeddings.shape)
# => (3, 384)

Similarity की तुलना करें

Embeddings की तुलना के लिए मॉडल के similarity helper का इस्तेमाल करें।

pythonpython
similarities = model.similarity(embeddings, embeddings)
print(similarities)
# tensor([[1.0000, 0.6660, 0.1046],
#         [0.6660, 1.0000, 0.1411],
#         [0.1046, 0.1411, 1.0000]])

Cross Encoder से rerank करें

एक reranker मॉडल लोड करें और मैन्युअल sorting के बिना query के लिए passages को rank करें।

pythonpython
from sentence_transformers import CrossEncoder

model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L6-v2")

query = "How many people live in Berlin?"
passages = [
    "Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.",
    "Berlin has a yearly total of about 135 million day visitors, making it one of the most-visited cities in the European Union.",
    "In 2013 around 600,000 Berliners were registered in one of the more than 2,300 sport and fitness clubs.",
]
ranks = model.rank(query, passages, return_documents=True)

कमांड और कोड प्रोजेक्ट के अपने दस्तावेज़ों से लिए गए हैं — हमेशा जांचें आधिकारिक repo ताज़ा जानकारी के लिए।

कब इस्तेमाल करें

  • Document collection पर semantic search बनाएं: corpus को embed करें और similarity से queries का मिलान करें
  • RAG pipeline में ज़्यादा सटीक top results के लिए cross-encoder से शुरुआती retrieved candidates को rerank करें
  • बड़े text sets में semantic textual similarity मापें या paraphrase mining चलाएं
  • किसी domain-specific dataset के लिए custom embedding या reranker मॉडल प्रशिक्षित करें या finetune करें

How Sentence Transformers compares

Sentence Transformers alongside other open-source embedding models & inference tools AI/TLDR tracks, ranked by GitHub stars.

टूलस्टारयह क्या करता है
Sentence Transformers★ 19.3kPython में टेक्स्ट embeddings की गणना करें, उन्हें प्रशिक्षित करें और rerank करें
OpenCLIP★ 14.3kimage-text embeddings को ट्रेन और चलाने के लिए CLIP का ओपन इम्प्लीमेंटेशन
PyTorch Metric Learning★ 6.3kPyTorch में embedding मॉडल ट्रेनिंग के लिए losses, miners और testers
EmbeddingGemma (Gemma)★ 5.9k308M मल्टीलिंगुअल एम्बेडिंग मॉडल, जो on-device चलने जितना छोटा है
Text Embeddings Inference (TEI)★ 5.2kतेज़ HTTP API के ज़रिए ओपन सोर्स embedding और reranking मॉडल सर्व करें
text2vec★ 5kचीनी या बहुभाषी टेक्स्ट को sentence embeddings में बदलें, ट्रेनिंग कोड के साथ
Infinity (Embeddings)★ 3kएम्बेडिंग, रीरैंकर, CLIP और ColPali मॉडल चलाने के लिए हाई-थ्रूपुट REST API
ColPali★ 2.9kपूरे दस्तावेज़ के पेजों को इमेज के रूप में retrieval के लिए index करें, OCR पाइपलाइन की ज़रूरत के बिना