Views
No views yet
1from sentence_transformers import SentenceTransformer
2
3model = SentenceTransformer("rttl-ai/MOSAIC-embed-biomed", trust_remote_code=True)
4
5sentences = [
6 "search_document: Metformin is a first-line treatment for type 2 diabetes.",
7 "search_query: What medications treat diabetes?"
8]
9
10embeddings = model.encode(sentences)
11print(embeddings.shape)1import torch
2import torch.nn.functional as F
3from transformers import AutoTokenizer, AutoModel
4
5def mean_pooling(model_output, attention_mask):
6 token_embeddings = model_output[0]
7 input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
8 return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
9
10tokenizer = AutoTokenizer.from_pretrained("rttl-ai/MOSAIC-embed-biomed")
11model = AutoModel.from_pretrained("rttl-ai/MOSAIC-embed-biomed", trust_remote_code=True)
12
13sentences = ["search_query: What causes Alzheimer's disease?"]
14
15inputs = tokenizer(sentences, padding=True, truncation=True, return_tensors="pt")
16with torch.no_grad():
17 outputs = model(**inputs)
18
19embeddings = mean_pooling(outputs, inputs["attention_mask"])
20embeddings = F.normalize(embeddings, p=2, dim=1)
21
22print(embeddings.shape)| Task | Prefix | Example |
|---|---|---|
| Document embedding | search_document: | search_document: Aspirin inhibits platelet aggregation. |
| Query embedding | search_query: | search_query: How does aspirin work? |
| Clustering | clustering: | clustering: cardiac arrest treatment protocols |
| Classification | classification: | classification: The patient presents with fever and cough. |
1@inproceedings{mosaic2026,
2 title={MOSAIC: Masked Objective with Selective Adaptation for In-domain Contrastive Learning},
3 author={Pavlova, Vera and ...},
4 booktitle={Findings of the European Chapter of the Association for Computational Linguistics (EACL)},
5 year={2026}
6}