Views
No views yet
1from sentence_transformers import SentenceTransformer
2
3model = SentenceTransformer("codefuse-ai/F2LLM-0.6B", model_kwargs={"torch_dtype": "bfloat16"})
4
5# Some sample query and documents
6query = "What is F2LLM used for?"
7documents = [
8 'We present F2LLM, a family of fully open embedding LLMs that achieve a strong balance between model size, training data, and embedding performance.',
9 'Model checkpoints, training datasets, and training code are released, positioning F2LLM as a strong, reproducible, and budget-friendly baseline for future research in text embedding models.',
10 '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.'
11]
12
13# Encode the query and documents separately, the encode_query method uses the query prompt
14query_embedding = model.encode_query(query)
15document_embeddings = model.encode_document(documents)
16print(query_embedding.shape, document_embeddings.shape)
17# (1024,) (3, 1024)
18
19# Compute cosine similarity between the query and documents
20similarity = model.similarity(query_embedding, document_embeddings)
21print(similarity)
22# tensor([[0.5132, 0.5376, 0.8017]])1from transformers import AutoModel, AutoTokenizer
2import torch
3import torch.nn.functional as F
4
5
6model_path = "codefuse-ai/F2LLM-0.6B"
7tokenizer = AutoTokenizer.from_pretrained(model_path)
8model = AutoModel.from_pretrained(model_path, torch_dtype=torch.bfloat16, device_map={'': 0})
9
10query = "What is F2LLM used for?"
11query_prompt = "Instruct: Given a web search query, retrieve relevant passages that answer the query\nQuery:"
12documents = [
13 'We present F2LLM, a family of fully open embedding LLMs that achieve a strong balance between model size, training data, and embedding performance.',
14 'Model checkpoints, training datasets, and training code are released, positioning F2LLM as a strong, reproducible, and budget-friendly baseline for future research in text embedding models.',
15 '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.'
16]
17
18def encode(sentences):
19 batch_size = len(sentences)
20 tokenized_inputs = tokenizer(sentences, padding=True, return_tensors='pt').to(model.device)
21 last_hidden_state = model(**tokenized_inputs).last_hidden_state
22 eos_positions = tokenized_inputs.attention_mask.sum(dim=1) - 1
23 embeddings = last_hidden_state[torch.arange(batch_size, device=model.device), eos_positions]
24 embeddings = F.normalize(embeddings, p=2, dim=1)
25 return embeddings
26
27# Encode the query and documents
28query_embedding = encode([query_prompt + query])
29document_embeddings = encode(documents)
30print(query_embedding.shape, document_embeddings.shape)
31# torch.Size([1, 1024]) torch.Size([3, 1024])
32
33# Compute cosine similarity between the query and documents
34similarity = query_embedding @ document_embeddings.T
35print(similarity)
36# tensor([[0.5039, 0.5312, 0.7930]], device='cuda:0', dtype=torch.bfloat16,
37# grad_fn=<MmBackward0>)1import mteb
2import logging
3logging.basicConfig(level=logging.INFO)
4
5task_names = ['AmazonCounterfactualClassification', 'ArXivHierarchicalClusteringP2P', 'ArXivHierarchicalClusteringS2S', 'ArguAna', 'AskUbuntuDupQuestions', 'BIOSSES', 'Banking77Classification', 'BiorxivClusteringP2P.v2', 'CQADupstackGamingRetrieval', 'CQADupstackUnixRetrieval', 'ClimateFEVERHardNegatives', 'FEVERHardNegatives', 'FiQA2018', 'HotpotQAHardNegatives', 'ImdbClassification', 'MTOPDomainClassification', 'MassiveIntentClassification', 'MassiveScenarioClassification', 'MedrxivClusteringP2P.v2', 'MedrxivClusteringS2S.v2', 'SCIDOCS', 'SICK-R', 'STS12', 'STS13', 'STS14', 'STS15', 'STS17', 'STS22.v2', 'STSBenchmark', 'SprintDuplicateQuestions', 'StackExchangeClustering.v2', 'StackExchangeClusteringP2P.v2', 'SummEvalSummarization.v2', 'TRECCOVID', 'Touche2020Retrieval.v3', 'ToxicConversationsClassification', 'TweetSentimentExtractionClassification', 'TwentyNewsgroupsClustering.v2', 'TwitterSemEval2015', 'TwitterURLCorpus', 'MindSmallReranking']
6
7tasks = [
8 mteb.get_task(task_name, languages = ["eng"], eval_splits=["test"], exclusive_language_filter=True)
9 for task_name in task_names
10]
11
12
13model = mteb.get_model("codefuse-ai/F2LLM-0.6B", device="cuda:0")
14evaluation = mteb.MTEB(tasks=tasks)
15evaluation.run(model, encode_kwargs={"batch_size": 16})@article{2025F2LLM,
title={F2LLM Technical Report: Matching SOTA Embedding Performance with 6 Million Open-Source Data},
author={Ziyin Zhang and Zihan Liao and Hang Yu and Peng Di and Rui Wang},
journal = {CoRR},
volume = {abs/2510.02294},
year = {2025},
url = {https://doi.org/10.48550/arXiv.2510.02294},
doi = {10.48550/ARXIV.2510.02294},
eprinttype = {arXiv},
eprint = {2510.02294}
}