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-ko-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-ko-8b-preview', padding_side='left')
39model = AutoModel.from_pretrained('sionic-ai/comsat-embed-ko-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-ko-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())| Model | Avg | MIRACL | MrTidy | MLDR | AutoRAG | Ko-StrategyQA | PublicHealthQA | Belebele | SQuADKorV1 | LawIRKo |
|---|---|---|---|---|---|---|---|---|---|---|
| comsat-embed-ko-8b-preview | 0.7930 | 0.6964 | 0.6253 | 0.5183 | 0.8518 | 0.8394 | 0.8871 | 0.9853 | 0.9168 | 0.8164 |
| Qwen/Qwen3-Embedding-8B | 0.7825 | 0.6783 | 0.6187 | 0.5036 | 0.8276 | 0.8363 | 0.8721 | 0.9828 | 0.9063 | 0.8171 |
| Qwen/Qwen3-Embedding-4B | 0.7718 | 0.6803 | 0.6076 | 0.4895 | 0.8431 | 0.8270 | 0.8693 | 0.9479 | 0.9044 | 0.7769 |
| upstage/solar-embedding-1-large | 0.7674 | 0.6703 | 0.5766 | 0.3850 | 0.8833 | 0.8366 | 0.8787 | 0.9684 | 0.9521 | 0.7557 |
| microsoft/harrier-oss-v1-27b | 0.7669 | 0.6653 | 0.5306 | 0.4073 | 0.8176 | 0.8361 | 0.8971 | 0.9538 | 0.9204 | 0.8737 |
| dragonkue/snowflake-arctic-embed-l-v2.0-ko | 0.7636 | 0.6685 | 0.5712 | 0.4150 | 0.9093 | 0.8050 | 0.8337 | 0.9518 | 0.9447 | 0.7735 |
| codefuse-ai/F2LLM-v2-8B | 0.7621 | 0.6311 | 0.6162 | 0.3950 | 0.7678 | 0.8371 | 0.9332 | 0.9509 | 0.8874 | 0.8405 |
| nlpai-lab/KURE-v1 | 0.7603 | 0.6816 | 0.5909 | 0.4521 | 0.8708 | 0.7999 | 0.8193 | 0.9502 | 0.9357 | 0.7426 |
| telepix/PIXIE-Rune-v1.5 | 0.7602 | 0.6393 | 0.5492 | 0.4340 | 0.8927 | 0.8064 | 0.8426 | 0.9617 | 0.9457 | 0.7705 |
| nvidia/llama-nemotron-embed-vl-1b-v2 | 0.7579 | 0.6975 | 0.5998 | 0.3704 | 0.8773 | 0.8084 | 0.8223 | 0.9584 | 0.9360 | 0.7513 |
| dragonkue/BGE-m3-ko | 0.7534 | 0.6833 | 0.6099 | 0.3784 | 0.8738 | 0.7959 | 0.8155 | 0.9503 | 0.9414 | 0.7322 |
| BAAI/bge-m3 | 0.7508 | 0.7015 | 0.6471 | 0.4273 | 0.8301 | 0.7941 | 0.8041 | 0.9316 | 0.9038 | 0.7174 |
| intfloat/multilingual-e5-large | 0.7333 | 0.6649 | 0.6421 | 0.2708 | 0.8134 | 0.8035 | 0.8253 | 0.9450 | 0.9056 | 0.7293 |
| nlpai-lab/KoE5 | 0.7329 | 0.6235 | 0.5841 | 0.2942 | 0.8434 | 0.8001 | 0.8351 | 0.9425 | 0.8980 | 0.7756 |
Avg is the mean over the 9 subsets (higher is better). Reproduction: evaluated with the MTEB retrieval pipeline (NDCG@10, full corpus); the query prompt is applied to queries only (documents get no prefix).