Views
No views yet
feature-extraction: https://huggingface.co/docs/transformers.js/api/pipelines#module_pipelines.FeatureExtractionPipeline






transformers>=4.37.0, or you might encounter the following error:KeyError: 'qwen2'pip install -U sentence-transformers1from sentence_transformers import SentenceTransformer
2import torch
3
4model = SentenceTransformer(
5 "KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5",
6 trust_remote_code=True,
7 model_kwargs={
8 "torch_dtype": torch.bfloat16,
9 "attn_implementation": "flash_attention_2", # Optional
10 },
11)
12model.max_seq_length = 512
13
14sentences = ["This is an example sentence", "Each sentence is converted"]
15embeddings = model.encode(
16 sentences,
17 normalize_embeddings=True,
18 batch_size=256,
19 show_progress_bar=True,
20)
21print(embeddings)
22'''
23[[-0.01043701 -0.02172852 0.0100708 ... -0.02807617 0.00157166
24 -0.03637695]
25 [-0.00424194 0.02966309 0.03686523 ... -0.02587891 0.01953125
26 -0.00125122]]
27'''1from sentence_transformers import SentenceTransformer
2import torch
3
4model = SentenceTransformer(
5 "KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5",
6 trust_remote_code=True,
7 model_kwargs={
8 "torch_dtype": torch.bfloat16,
9 "attn_implementation": "flash_attention_2", # Optional
10 },
11)
12model.max_seq_length = 512
13
14sentences = ["This is an example sentence", "Each sentence is converted"]
15prompt = "Instruct: Classifying the category of french news.\nQuery:"
16embeddings = model.encode(
17 sentences,
18 prompt=prompt,
19 normalize_embeddings=True,
20 batch_size=256,
21 show_progress_bar=True,
22)
23print(embeddings)
24'''
25[[-0.01867676 0.02319336 0.00280762 ... -0.02075195 0.00196838
26 -0.0703125 ]
27 [-0.0067749 0.03491211 0.01434326 ... -0.0043335 0.00509644
28 -0.04174805]]
29'''encode_query and encode_document to automatically add the default prompt for queries ("Instruct: Given a query, retrieve documents that answer the query \n Query: ") and documents (""), respectively.1from sentence_transformers import SentenceTransformer
2import torch
3
4model = SentenceTransformer(
5 "KaLM-Embedding/KaLM-embedding-multilingual-mini-instruct-v2.5",
6 trust_remote_code=True,
7 model_kwargs={
8 "torch_dtype": torch.bfloat16,
9 "attn_implementation": "flash_attention_2", # Optional
10 },
11)
12model.max_seq_length = 512
13
14queries = [
15 "What is the capital of China?",
16 "Explain gravity",
17]
18documents = [
19 "The capital of China is Beijing.",
20 "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun.",
21]
22
23query_embeddings = model.encode_query(queries)
24document_embeddings = model.encode_document(documents)
25
26similarities = model.similarity(query_embeddings, document_embeddings)
27print(similarities)
28'''
29tensor([[0.9034, 0.2563],
30 [0.3153, 0.7396]])
31'''pip install -U vllm==0.8.51import torch
2import vllm
3from vllm import LLM
4def get_detailed_instruct(task_description: str, query: str) -> str:
5 return f'Instruct: {task_description}\nQuery:{query}'
6
7task = 'Given a query, retrieve documents that answer the query'
8queries = [
9 get_detailed_instruct(task, 'What is the capital of China?'),
10 get_detailed_instruct(task, 'Explain gravity')
11]
12documents = [
13 "The capital of China is Beijing.",
14 "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun."
15]
16input_texts = queries + documents
17
18model = LLM(model="{MODEL_NAME_OR_PATH}", task="embed", trust_remote_code=True, dtype="float16")
19
20outputs = model.embed(input_texts)
21embeddings = torch.tensor([o.outputs.embedding for o in outputs])
22scores = (embeddings[:2] @ embeddings[2:].T)
23print(scores.tolist())@misc{zhao2025kalmembeddingv2,
title={KaLM-Embedding-V2: Superior Training Techniques and Data Inspire A Versatile Embedding Model},
author={Xinping Zhao and Xinshuo Hu and Zifei Shan and Shouzheng Huang and Yao Zhou and Xin Zhang and Zetian Sun and Zhenyu Liu and Dongfang Li and Xinyuan Wei and Youcheng Pan and Yang Xiang and Meishan Zhang and Haofen Wang and Jun Yu and Baotian Hu and Min Zhang},
year={2025},
eprint={2506.20923},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2506.20923},
}
@misc{hu2025kalmembedding,
title={KaLM-Embedding: Superior Training Data Brings A Stronger Embedding Model},
author={Xinshuo Hu and Zifei Shan and Xinping Zhao and Zetian Sun and Zhenyu Liu and Dongfang Li and Shaolin Ye and Xinyuan Wei and Qian Chen and Baotian Hu and Haofen Wang and Jun Yu and Min Zhang},
year={2025},
eprint={2501.01028},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2501.01028},
}