BAAI / bge-code-v1

huggingface.co
Total runs: 5.6K
24-hour runs: -264
7-day runs: 973
30-day runs: 1.7K
Model's Last Updated: May 20 2025
sentence-similarity

Introduction of bge-code-v1

Model Details of bge-code-v1

FlagEmbedding

For more details please refer to our Github: FlagEmbedding .

BGE-Code-v1 is an LLM-based code embedding model that supports code retrieval, text retrieval, and multilingual retrieval. It primarily demonstrates the following capabilities:

  • Superior Code Retrieval Performance: The model demonstrates exceptional code retrieval capabilities, supporting natural language queries in both English and Chinese, as well as 20 programming languages.
  • Robust Text Retrieval Capabilities: The model maintains strong text retrieval capabilities comparable to text embedding models of similar scale.
  • Extensive Multilingual Support: BGE-Code-v1 offers comprehensive multilingual retrieval capabilities, excelling in languages such as English, Chinese, Japanese, French, and more.
Usage
Using FlagEmbedding
git clone https://github.com/FlagOpen/FlagEmbedding.git
cd FlagEmbedding
pip install -e .
from FlagEmbedding import FlagLLMModel
queries = [
    "Delete the record with ID 4 from the 'Staff' table.", 
    'Delete all records in the "Livestock" table where age is greater than 5'
]
documents = [
    "DELETE FROM Staff WHERE StaffID = 4;",
    "DELETE FROM Livestock WHERE age > 5;"
]
model = FlagLLMModel('BAAI/bge-code-v1', 
                     query_instruction_format="<instruct>{}\n<query>{}",
                     query_instruction_for_retrieval="Given a question in text, retrieve SQL queries that are appropriate responses to the question.",
                     trust_remote_code=True,
                     use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation
embeddings_1 = model.encode_queries(queries)
embeddings_2 = model.encode_corpus(documents)
similarity = embeddings_1 @ embeddings_2.T
print(similarity)

By default, FlagLLMModel will use all available GPUs when encoding. Please set os.environ["CUDA_VISIBLE_DEVICES"] to select specific GPUs. You also can set os.environ["CUDA_VISIBLE_DEVICES"]="" to make all GPUs unavailable.

Using Sentence Transformers
from sentence_transformers import SentenceTransformer
import torch

# Load the model, optionally in float16 precision for faster inference
model = SentenceTransformer(
    "BAAI/bge-code-v1",
    trust_remote_code=True,
    model_kwargs={"torch_dtype": torch.float16},
)

# Prepare a prompt given an instruction
instruction = 'Given a question in text, retrieve SQL queries that are appropriate responses to the question.'
prompt = f'<instruct>{instruction}\n<query>'
# Prepare queries and documents
queries = [
    "Delete the record with ID 4 from the 'Staff' table.", 
    'Delete all records in the "Livestock" table where age is greater than 5'
]
documents = [
    "DELETE FROM Staff WHERE StaffID = 4;",
    "DELETE FROM Livestock WHERE age > 5;"
]

# Compute the query and document embeddings
query_embeddings = model.encode(queries, prompt=prompt)
document_embeddings = model.encode(documents)

# Compute the cosine similarity between the query and document embeddings
similarities = model.similarity(query_embeddings, document_embeddings)
print(similarities)
Using HuggingFace Transformers
import torch
import torch.nn.functional as F

from torch import Tensor
from transformers import AutoTokenizer, AutoModel


def last_token_pool(last_hidden_states: Tensor,
                 attention_mask: Tensor) -> Tensor:
    left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
    if left_padding:
        return last_hidden_states[:, -1]
    else:
        sequence_lengths = attention_mask.sum(dim=1) - 1
        batch_size = last_hidden_states.shape[0]
        return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]


def get_detailed_instruct(task_description: str, query: str) -> str:
    return f'<instruct>{task_description}\n<query>{query}'


instruction = 'Given a question in text, retrieve SQL queries that are appropriate responses to the question.'
queries = [
    "Delete the record with ID 4 from the 'Staff' table.", 
    'Delete all records in the "Livestock" table where age is greater than 5'
]
documents = [
    "DELETE FROM Staff WHERE StaffID = 4;",
    "DELETE FROM Livestock WHERE age > 5;"
]
input_texts = queries + documents

tokenizer = AutoTokenizer.from_pretrained('BAAI/bge-code-v1', trust_remote_code=True)
model = AutoModel.from_pretrained('BAAI/bge-code-v1', trust_remote_code=True)
model.eval()

max_length = 4096
# Tokenize the input texts
batch_dict = tokenizer(input_texts, max_length=max_length, padding=True, truncation=True, return_tensors='pt', pad_to_multiple_of=8)

with torch.no_grad():
    outputs = model(**batch_dict)
    embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
    
# normalize embeddings
embeddings = F.normalize(embeddings, p=2, dim=1)
scores = (embeddings[:2] @ embeddings[2:].T) * 100
print(scores.tolist())
Evaluation

BGE-Code-v1 achieves state-of-the-art performance on both the CoIR and CodeRAG benchmarks.

  • CoIR
CodeXEmbed-2B CodeXEmbed-7B Voyage-Code-002 Voyage-Code-003 BGE-Code-v1
Apps 76.86 85.38 26.52 93.62 98.08
CosQA 40.47 42.47 29.79 34.45 46.72
Text2SQL 78.42 78.94 69.26 62.87 64.35
CSN 87.87 89.67 81.79 89.35 89.53
CSN-CCR 97.66 97.95 73.45 90.05 98.30
CodeTrans-Contest 90.30 94.45 72.77 94.96 94.38
CodeTrans-DL 38.57 40.46 27.48 38.57 46.13
StackOverFlow-QA 94.47 96.33 67.68 97.17 95.35
CodeFeedBack-ST 86.36 87.53 65.35 90.67 90.56
CodeFeedBack-MT 65.51 68.83 28.74 93.58 94.38
AVG 75.65 78.20 56.26 78.53 81.77
  • CodedRAG
HummanEval MBPP DS-1000 ODEX RepoEval SWE-bench-Lite AVG
SFR 100.0 99.0 19.3 37.1 83.8 62.7 67.0
Jina-v2-code 100.0 97.7 26.2 19.9 90.5 58.3 65.4
CodeXEmbed-2B 100.0 97.4 25.4 23.9 88.7 52.4 64.6
Voyage-Code-002 100.0 99.0 33.1 26.6 94.3 29.1 63.7
BGE-Code-v1 100.0 99.2 40.9 36.1 93.1 67.4 72.8
Instructions for Evaluation
{
    "Apps": "Given a code contest problem description, retrieve relevant code that can help solve the problem.",
    "CosQA": "Given a web search query, retrieve relevant code that can help answer the query.",
    "Text2SQL": "Given a question in text, retrieve SQL queries that are appropriate responses to the question.",
    "CSN": "Given a piece of code, retrieve the document string that summarizes the code.",
    "CSN-CCR": "Given a piece of code segment, retrieve the code segment that is the latter part of the code.",
    "CodeTrans-DL": "Given a piece of code, retrieve code that is semantically equivalent to the input code.",
    "CodeTrans-Contest": "Given a piece of Python code, retrieve C++ code that is semantically equivalent to the input code.",
    "StackOverFlow-QA": "Given a question that consists of a mix of text and code snippets, retrieve relevant answers that also consist of a mix of text and code snippets, and can help answer the question.",
    "CodeFeedBack-ST": "Given a question that consists of a mix of text and code snippets, retrieve relevant answers that also consist of a mix of text and code snippets, and can help answer the question.",
    "CodeFeedBack-MT": "Given a multi-turn conversation history that consists of a mix of text and code snippets, retrieve relevant answers that also consist of a mix of text and code snippets, and can help answer the question.",
    "HummanEval": "Given a question that consists of a mix of text and code snippets, retrieve relevant answers that also consist of a mix of text and code snippets, and can help answer the question.",
    "MBPP": "Given a textual explanation of code functionality, retrieve the corresponding code implementation.",
    "DS-1000": "Given a question that consists of a mix of text and code snippets, retrieve relevant answers that also consist of a mix of text and code snippets, and can help answer the question.",
    "ODEX": "Given a question, retrieve relevant answers that also consist of a mix of text and code snippets, and can help answer the question.",
    "RepoEval": "Given a piece of code segment, retrieve the code segment that is the latter part of the code.",
    "SWE-bench-Lite": "Given a code snippet containing a bug and a natural language description of the bug or error, retrieve code snippets that demonstrate solutions or fixes for similar bugs or errors (the desired documents)."
}
Citation

If you find this repository useful, please consider giving a star :star: and citation

@misc{bge_code,
    title={Towards A Generalist Code Embedding Model Based On Massive Data Synthesis}, 
    author={Chaofan Li and Jianlyu Chen and Yingxia Shao and Defu Lian and Zheng Liu},
    year={2025},
    eprint={2505.12697},
    archivePrefix={arXiv},
    primaryClass={cs.IR},
    url={https://arxiv.org/abs/2505.12697}, 
}

Runs of BAAI bge-code-v1 on huggingface.co

5.6K
Total runs
-264
24-hour runs
7
3-day runs
973
7-day runs
1.7K
30-day runs

More Information About bge-code-v1 huggingface.co Model

More bge-code-v1 license Visit here:

https://choosealicense.com/licenses/apache-2.0

bge-code-v1 huggingface.co

bge-code-v1 huggingface.co is an AI model on huggingface.co that provides bge-code-v1's model effect (), which can be used instantly with this BAAI bge-code-v1 model. huggingface.co supports a free trial of the bge-code-v1 model, and also provides paid use of the bge-code-v1. Support call bge-code-v1 model through api, including Node.js, Python, http.

bge-code-v1 huggingface.co Url

https://huggingface.co/BAAI/bge-code-v1

BAAI bge-code-v1 online free

bge-code-v1 huggingface.co is an online trial and call api platform, which integrates bge-code-v1's modeling effects, including api services, and provides a free online trial of bge-code-v1, you can try bge-code-v1 online for free by clicking the link below.

BAAI bge-code-v1 online free url in huggingface.co:

https://huggingface.co/BAAI/bge-code-v1

bge-code-v1 install

bge-code-v1 is an open source model from GitHub that offers a free installation service, and any user can find bge-code-v1 on GitHub to install. At the same time, huggingface.co provides the effect of bge-code-v1 install, users can directly use bge-code-v1 installed effect in huggingface.co for debugging and trial. It also supports api for free installation.

bge-code-v1 install url in huggingface.co:

https://huggingface.co/BAAI/bge-code-v1

Url of bge-code-v1

bge-code-v1 huggingface.co Url

Provider of bge-code-v1 huggingface.co

BAAI
ORGANIZATIONS

Other API from BAAI

huggingface.co

Total runs: 63.3M
Run Growth: -4.5M
Growth Rate: -7.13%
Updated:February 22 2024
huggingface.co

Total runs: 35.4M
Run Growth: -2.1M
Growth Rate: -5.93%
Updated:July 03 2024
huggingface.co

Total runs: 10.4M
Run Growth: -1.4M
Growth Rate: -13.84%
Updated:February 21 2024
huggingface.co

Total runs: 9.8M
Run Growth: -3.9M
Growth Rate: -39.54%
Updated:February 21 2024
huggingface.co

Total runs: 1.8M
Run Growth: 226.2K
Growth Rate: 12.29%
Updated:April 17 2024
huggingface.co

Total runs: 747.5K
Run Growth: -430.0K
Growth Rate: -57.52%
Updated:April 02 2024
huggingface.co

Total runs: 654.1K
Run Growth: -422.0K
Growth Rate: -64.51%
Updated:October 12 2023
huggingface.co

Total runs: 202.5K
Run Growth: -528.7K
Growth Rate: -261.08%
Updated:December 13 2023
huggingface.co

Total runs: 64.3K
Run Growth: 49.9K
Growth Rate: 77.61%
Updated:October 12 2023
huggingface.co

Total runs: 59.8K
Run Growth: -22.4K
Growth Rate: -37.38%
Updated:October 12 2023
huggingface.co

Total runs: 59.3K
Run Growth: -16.2K
Growth Rate: -27.35%
Updated:July 13 2026
huggingface.co

Total runs: 51.6K
Run Growth: 2.1K
Growth Rate: 4.04%
Updated:October 12 2023
huggingface.co

Total runs: 41.1K
Run Growth: -610
Growth Rate: -1.48%
Updated:January 15 2025
huggingface.co

Total runs: 40.4K
Run Growth: 1.9K
Growth Rate: 4.66%
Updated:November 14 2023
huggingface.co

Total runs: 15.8K
Run Growth: -769
Growth Rate: -4.88%
Updated:April 16 2025
huggingface.co

Total runs: 10.4K
Run Growth: 10.4K
Growth Rate: 100.00%
Updated:September 16 2026
huggingface.co

Total runs: 7.8K
Run Growth: -10.4K
Growth Rate: -133.29%
Updated:October 12 2023
huggingface.co

Total runs: 3.7K
Run Growth: 2.2K
Growth Rate: 59.61%
Updated:April 19 2024
huggingface.co

Total runs: 2.4K
Run Growth: 1.1K
Growth Rate: 45.59%
Updated:April 10 2026
huggingface.co

Total runs: 2.3K
Run Growth: 145
Growth Rate: 6.26%
Updated:September 11 2026
huggingface.co

Total runs: 1.8K
Run Growth: 982
Growth Rate: 53.49%
Updated:November 28 2024
huggingface.co

Total runs: 1.8K
Run Growth: -13.8K
Growth Rate: -781.52%
Updated:January 15 2025
huggingface.co

Total runs: 1.6K
Run Growth: 113
Growth Rate: 6.93%
Updated:April 10 2026
huggingface.co

Total runs: 1.5K
Run Growth: -86
Growth Rate: -5.86%
Updated:December 31 2022
huggingface.co

Total runs: 1.3K
Run Growth: 916
Growth Rate: 72.70%
Updated:September 11 2026
huggingface.co

Total runs: 767
Run Growth: -20
Growth Rate: -2.61%
Updated:May 23 2025
huggingface.co

Total runs: 689
Run Growth: -849
Growth Rate: -121.29%
Updated:February 07 2024
huggingface.co

Total runs: 665
Run Growth: -105
Growth Rate: -15.79%
Updated:October 24 2024
huggingface.co

Total runs: 651
Run Growth: -475
Growth Rate: -72.96%
Updated:October 23 2024
huggingface.co

Total runs: 510
Run Growth: -202
Growth Rate: -39.61%
Updated:July 13 2026
huggingface.co

Total runs: 491
Run Growth: -399
Growth Rate: -81.26%
Updated:April 18 2023
huggingface.co

Total runs: 464
Run Growth: 312
Growth Rate: 67.24%
Updated:April 02 2024
huggingface.co

Total runs: 431
Run Growth: -100
Growth Rate: -23.20%
Updated:August 11 2026
huggingface.co

Total runs: 419
Run Growth: 86
Growth Rate: 20.53%
Updated:July 13 2026
huggingface.co

Total runs: 418
Run Growth: -22
Growth Rate: -5.26%
Updated:April 10 2026
huggingface.co

Total runs: 404
Run Growth: 3
Growth Rate: 0.74%
Updated:December 25 2025
huggingface.co

Total runs: 393
Run Growth: -157
Growth Rate: -39.95%
Updated:July 13 2026
huggingface.co

Total runs: 371
Run Growth: -329
Growth Rate: -88.68%
Updated:August 11 2026
huggingface.co

Total runs: 328
Run Growth: 87
Growth Rate: 26.52%
Updated:July 13 2026
huggingface.co

Total runs: 303
Run Growth: -121
Growth Rate: -39.93%
Updated:July 13 2026
huggingface.co

Total runs: 296
Run Growth: 63
Growth Rate: 21.28%
Updated:August 18 2026
huggingface.co

Total runs: 292
Run Growth: 17
Growth Rate: 5.82%
Updated:July 13 2026
huggingface.co

Total runs: 282
Run Growth: -368
Growth Rate: -130.50%
Updated:December 25 2025
huggingface.co

Total runs: 275
Run Growth: -191
Growth Rate: -69.45%
Updated:October 27 2023
huggingface.co

Total runs: 266
Run Growth: 266
Growth Rate: 100.00%
Updated:September 05 2026
huggingface.co

Total runs: 262
Run Growth: 42
Growth Rate: 16.03%
Updated:July 13 2026
huggingface.co

Total runs: 261
Run Growth: -155
Growth Rate: -56.78%
Updated:March 07 2024
huggingface.co

Total runs: 242
Run Growth: 39
Growth Rate: 16.12%
Updated:July 17 2026
huggingface.co

Total runs: 232
Run Growth: -11
Growth Rate: -4.74%
Updated:June 06 2025
huggingface.co

Total runs: 226
Run Growth: 135
Growth Rate: 59.73%
Updated:November 28 2023
huggingface.co

Total runs: 224
Run Growth: 25
Growth Rate: 11.16%
Updated:July 13 2026
huggingface.co

Total runs: 213
Run Growth: 102
Growth Rate: 47.89%
Updated:July 13 2026
huggingface.co

Total runs: 212
Run Growth: -2.0K
Growth Rate: -949.53%
Updated:April 10 2026
huggingface.co

Total runs: 209
Run Growth: 93
Growth Rate: 44.50%
Updated:July 13 2026
huggingface.co

Total runs: 202
Run Growth: -193
Growth Rate: -95.54%
Updated:April 10 2026
huggingface.co

Total runs: 194
Run Growth: 45
Growth Rate: 23.20%
Updated:June 24 2024
huggingface.co

Total runs: 191
Run Growth: 93
Growth Rate: 48.69%
Updated:March 13 2026
huggingface.co

Total runs: 190
Run Growth: 41
Growth Rate: 21.58%
Updated:June 24 2024
huggingface.co

Total runs: 189
Run Growth: -12
Growth Rate: -6.35%
Updated:May 13 2024
huggingface.co

Total runs: 187
Run Growth: 15
Growth Rate: 8.02%
Updated:May 13 2024
huggingface.co

Total runs: 184
Run Growth: 11
Growth Rate: 5.98%
Updated:May 13 2024
huggingface.co

Total runs: 182
Run Growth: 80
Growth Rate: 43.96%
Updated:December 21 2023