Views
No views yet
1from sentence_transformers import SentenceTransformer
2model = SentenceTransformer("codefuse-ai/F2LLM-v2-8B", device="cuda:0", model_kwargs={"torch_dtype": "bfloat16"})
3# Some sample query and documents
4query = "What is F2LLM used for?"
5documents = [
6 'We present F2LLM, a family of fully open embedding LLMs that achieve a strong balance between model size, training data, and embedding performance.',
7 'F2LLM is a model for computing text embeddings that can be used for various NLP tasks such as information retrieval, semantic search, and text classification.',
8 'F2LLM 是 CodeFuse 开源的系列嵌入模型。',
9 'F2LLM — это модель вычисления встраивания текста, которую можно использовать для различных задач НЛП, таких как поиск информации, семантический поиск и классификация текста.'
10]
11# Encode the query and documents separately. The encode_query method uses the query prompt
12query_embedding = model.encode_query(query)
13document_embeddings = model.encode_document(documents)
14print(query_embedding.shape, document_embeddings.shape)
15# (4096,) (4, 4096)
16# Compute cosine similarity between the query and documents
17similarity = model.similarity(query_embedding, document_embeddings)
18print(similarity)
19# tensor([[0.5720, 0.8251, 0.6925, 0.8206]])1from transformers import AutoModel, AutoTokenizer
2import torch
3import torch.nn.functional as F
4model_path = "codefuse-ai/F2LLM-v2-8B"
5tokenizer = AutoTokenizer.from_pretrained(model_path)
6model = AutoModel.from_pretrained(model_path, torch_dtype=torch.bfloat16, device_map={'': 0})
7query = "What is F2LLM used for?"
8query_prompt = "Instruct: Given a question, retrieve passages that can help answer the question.\nQuery: "
9documents = [
10 'We present F2LLM, a family of fully open embedding LLMs that achieve a strong balance between model size, training data, and embedding performance.',
11 'F2LLM is a model for computing text embeddings that can be used for various NLP tasks such as information retrieval, semantic search, and text classification.',
12 'F2LLM 是 CodeFuse 开源的系列嵌入模型。',
13 'F2LLM — это модель вычисления встраивания текста, которую можно использовать для различных задач НЛП, таких как поиск информации, семантический поиск и классификация текста.'
14]
15def encode(sentences):
16 batch_size = len(sentences)
17 # the tokenizer will automatically add eos token
18 tokenized_inputs = tokenizer(sentences, padding=True, return_tensors='pt').to(model.device)
19 last_hidden_state = model(**tokenized_inputs).last_hidden_state
20 eos_positions = tokenized_inputs.attention_mask.sum(dim=1) - 1
21 embeddings = last_hidden_state[torch.arange(batch_size, device=model.device), eos_positions]
22 embeddings = F.normalize(embeddings, p=2, dim=1)
23 return embeddings
24# Encode the query and documents
25query_embedding = encode([query_prompt + query])
26document_embeddings = encode(documents)
27print(query_embedding.shape, document_embeddings.shape)
28# torch.Size([1, 4096]) torch.Size([4, 4096])
29# Compute cosine similarity between the query and documents
30similarity = query_embedding @ document_embeddings.T
31print(similarity)
32# tensor([[0.5703, 0.8281, 0.6953, 0.8203]], device='cuda:0',
33# dtype=torch.bfloat16, grad_fn=<MmBackward0>)1Instruct: your_instruction
2Query:intermediate_checkpoints branch.@misc{f2llm-v2,
title={F2LLM-v2: Inclusive, Performant, and Efficient Embeddings for a Multilingual World},
author={Ziyin Zhang and Zihan Liao and Hang Yu and Peng Di and Rui Wang},
year={2026},
eprint={2603.19223},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2603.19223},
}