Add Sentence Transformers usage

#5
by tomaarsen HF Staff - opened

Hello!

Starting with the next Sentence Transformers release (v6.0.0, planned for around the 18th), this checkpoint loads directly as a multi-vector (ColBERT-style late interaction) retriever through the new MultiVectorEncoder. This PR adds a Sentence Transformers usage section to the model card and the multi-vector and sentence-transformers tags. The weights and the existing usage are untouched.

I'd love to feature this model in that release's blog post and documentation, especially once it loads without the revision pin (that is, once this PR is merged).

pip install "sentence-transformers @ git+https://github.com/huggingface/sentence-transformers.git"
from sentence_transformers import MultiVectorEncoder

model = MultiVectorEncoder("ytu-ce-cosmos/turkish-colbert", revision="refs/pr/5")

query = "Birden fazla Nobel Ödülü alan bilim insanı kimdir?"
documents = [
    "Marie Curie, radyoaktivite üzerine yaptığı çalışmalarla bilim dünyasına büyük katkılar sağlamıştır. Polonyum ve radyum elementlerini keşfetmiştir. İki farklı dalda Nobel Ödülü alan ilk kişi olmuştur.",
    "Isaac Newton, fizik ve matematik alanında yaptığı çalışmalarla bilinir. Yerçekimi teorisi ve hareket yasaları, bilim dünyasında çığır açmıştır.",
    "Albert Einstein, izafiyet teorisini geliştirerek modern fiziğin temellerini atmıştır. 1921 yılında Nobel Fizik Ödülü kazanmıştır.",
    "Alexander Fleming, 1928 yılında penisilini keşfederek modern tıpta devrim yaratmıştır. Bu keşfi sayesinde 1945 yılında Nobel Tıp Ödülü kazanmıştır.",
    "Nikola Tesla, alternatif akım sistemini geliştirmiştir. Kablosuz enerji aktarımı üzerine projeleriyle tanınır.",
]

# turkish-colbert is trained on lowercased text with Turkish dotless-i handling
query = query.replace("I", "ı").lower()
documents = [doc.replace("I", "ı").lower() for doc in documents]

query_embeddings = model.encode_query(query)
document_embeddings = model.encode_document(documents)
print(query_embeddings.shape, document_embeddings[0].shape)
# (32, 256) (39, 256)

# MaxSim late-interaction scoring (higher is more relevant)
scores = model.similarity(query_embeddings, document_embeddings)
print(scores)
# tensor([[20.8741, 16.5549, 18.2056, 17.7900, 15.1087]])
  • Tom Aarsen
tomaarsen changed pull request status to open
Yildiz Technical University Computer Engineering Department Cosmos Research Group org

Thank you for your contribution Tom.

cxrbon16 changed pull request status to merged

Gladly! The release and its blogpost featuring your model are now live! https://huggingface.co/blog/multi-vector-encoder

  • Tom Aarsen

Sign up or log in to comment