Views
No views yet

pip install -U sentence-transformers⚠️ Queries must be encoded with the query prompt; documents are encoded without any prefix. (Skipping the query prompt slightly degrades retrieval quality.)
1from sentence_transformers import SentenceTransformer
2
3model = SentenceTransformer("sionic-ai/comsat-embed-ja-8b-preview")
4
5queries = ["日本の首都はどこですか?"]
6passages = ["日本の首都は東京都です。"]
7
8# Option 1) pass the query prompt explicitly (query only; documents get no prefix)
9q_emb = model.encode(queries, prompt_name="query", normalize_embeddings=True)
10d_emb = model.encode(passages, normalize_embeddings=True)
11
12# Option 2) sentence-transformers 5.x helper API (equivalent result)
13# q_emb = model.encode_query(queries)
14# d_emb = model.encode_document(passages)
15
16scores = q_emb @ d_emb.T # cosine similarity
17print(scores)1# Requires transformers>=4.51.0
2
3import torch
4import torch.nn.functional as F
5
6from torch import Tensor
7from transformers import AutoTokenizer, AutoModel
8
9
10def last_token_pool(last_hidden_states: Tensor,
11 attention_mask: Tensor) -> Tensor:
12 left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
13 if left_padding:
14 return last_hidden_states[:, -1]
15 else:
16 sequence_lengths = attention_mask.sum(dim=1) - 1
17 batch_size = last_hidden_states.shape[0]
18 return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]
19
20
21def get_detailed_instruct(task_description: str, query: str) -> str:
22 return f'Instruct: {task_description}\nQuery:{query}'
23
24# Each query must come with a one-sentence instruction that describes the task
25task = 'Given a web search query, retrieve relevant passages that answer the query'
26
27queries = [
28 get_detailed_instruct(task, '日本の首都はどこですか?'),
29 get_detailed_instruct(task, '光合成はどのように起こりますか?')
30]
31# No need to add instruction for retrieval documents
32documents = [
33 "日本の首都は東京都です。",
34 "光合成は、植物が光エネルギーを利用して二酸化炭素と水からブドウ糖を合成する過程です。"
35]
36input_texts = queries + documents
37
38tokenizer = AutoTokenizer.from_pretrained('sionic-ai/comsat-embed-ja-8b-preview', padding_side='left')
39model = AutoModel.from_pretrained('sionic-ai/comsat-embed-ja-8b-preview')
40
41# We recommend enabling flash_attention_2 for better acceleration and memory saving.
42# model = AutoModel.from_pretrained('sionic-ai/comsat-embed-ja-8b-preview', attn_implementation="flash_attention_2", torch_dtype=torch.bfloat16).cuda()
43
44max_length = 8192
45
46# Tokenize the input texts
47batch_dict = tokenizer(
48 input_texts,
49 padding=True,
50 truncation=True,
51 max_length=max_length,
52 return_tensors="pt",
53)
54batch_dict.to(model.device)
55outputs = model(**batch_dict)
56embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
57
58# normalize embeddings
59embeddings = F.normalize(embeddings, p=2, dim=1)
60scores = (embeddings[:2] @ embeddings[2:].T)
61print(scores.tolist())ja, MrTidy=japanese).| Model | Avg | NLPJ-TitleAbs | NLPJ-TitleIntro | NLPJ-AbsIntro | NLPJ-AbsArticle | Mintaka | JaGovFaqs | Jaqket | MultiLongDoc | JaCWIR | MIRACL | MrTidy |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| comsat-embed-ja-8b-preview | 0.8133 | 0.9779 | 0.9781 | 0.9922 | 0.9973 | 0.6087 | 0.7634 | 0.7809 | 0.5655 | 0.8948 | 0.7479 | 0.6400 |
| codefuse-ai/F2LLM-v2-14B | 0.7965 | 0.9782 | 0.9823 | 0.9938 | 0.9966 | 0.5894 | 0.8244 | 0.7471 | 0.4854 | 0.8142 | 0.6941 | 0.6565 |
| Qwen/Qwen3-Embedding-8B | 0.7924 | 0.9649 | 0.9517 | 0.9900 | 0.9973 | 0.6023 | 0.7313 | 0.6642 | 0.5649 | 0.8590 | 0.7388 | 0.6524 |
| codefuse-ai/F2LLM-v2-8B | 0.7855 | 0.9808 | 0.9882 | 0.9932 | 0.9966 | 0.5438 | 0.8149 | 0.7050 | 0.4824 | 0.8121 | 0.6745 | 0.6485 |
| Qwen/Qwen3-Embedding-4B | 0.7779 | 0.9753 | 0.9589 | 0.9881 | 0.9959 | 0.5201 | 0.7179 | 0.6136 | 0.5659 | 0.8560 | 0.7244 | 0.6406 |
| codefuse-ai/F2LLM-v2-4B | 0.7705 | 0.9853 | 0.9803 | 0.9937 | 0.9966 | 0.4767 | 0.8064 | 0.6528 | 0.4701 | 0.8166 | 0.6527 | 0.6442 |
| Qwen/Qwen3-VL-Embedding-8B | 0.7702 | 0.9764 | 0.9648 | 0.9896 | 0.9973 | 0.4995 | 0.7051 | 0.6830 | 0.4936 | 0.8491 | 0.6934 | 0.6201 |
| sbintuitions/sarashina-embedding-v2-1b | 0.7659 | 0.9804 | 0.9782 | 0.9954 | 0.9858 | 0.4365 | 0.7561 | 0.7371 | 0.4529 | 0.8552 | 0.6552 | 0.5916 |
| cl-nagoya/ruri-v3-130m | 0.7641 | 0.9807 | 0.9643 | 0.9894 | 0.9959 | 0.3283 | 0.7729 | 0.7514 | 0.4565 | 0.8349 | 0.7157 | 0.6149 |
| cl-nagoya/ruri-v3-310m | 0.7630 | 0.9785 | 0.9653 | 0.9908 | 0.9959 | 0.3353 | 0.7726 | 0.7342 | 0.4393 | 0.8405 | 0.7233 | 0.6168 |
Avg is the mean over the 11 JMTEB(v2) retrieval tasks (higher is better). Reproduction: evaluated with the MTEB/JMTEB retrieval pipeline (NDCG@10, full corpus); the query prompt is applied to queries only (documents get no prefix).