BAAI / bge-code-v1

huggingface.co
Total runs: 13.9K
24-hour runs: 0
7-day runs: -1.7K
30-day runs: -42.5K
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

13.9K
Total runs
0
24-hour runs
-1.2K
3-day runs
-1.7K
7-day runs
-42.5K
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: 35.6M
Run Growth: 1.1M
Growth Rate: 3.13%
Updated:July 03 2024
huggingface.co

Total runs: 12.7M
Run Growth: -1.5M
Growth Rate: -11.74%
Updated:February 21 2024
huggingface.co

Total runs: 11.6M
Run Growth: 3.1M
Growth Rate: 26.40%
Updated:February 21 2024
huggingface.co

Total runs: 1.6M
Run Growth: 305.2K
Growth Rate: 19.67%
Updated:December 13 2023
huggingface.co

Total runs: 1.5M
Run Growth: -456.2K
Growth Rate: -30.56%
Updated:October 12 2023
huggingface.co

Total runs: 513.0K
Run Growth: -511.9K
Growth Rate: -99.79%
Updated:April 17 2024
huggingface.co

Total runs: 172.2K
Run Growth: -24.0K
Growth Rate: -13.94%
Updated:October 12 2023
huggingface.co

Total runs: 87.7K
Run Growth: -10.1K
Growth Rate: -11.52%
Updated:October 12 2023
huggingface.co

Total runs: 67.8K
Run Growth: 51.2K
Growth Rate: 75.51%
Updated:July 13 2026
huggingface.co

Total runs: 61.6K
Run Growth: -18.5K
Growth Rate: -29.95%
Updated:January 15 2025
huggingface.co

Total runs: 26.4K
Run Growth: 5.4K
Growth Rate: 20.66%
Updated:October 12 2023
huggingface.co

Total runs: 22.9K
Run Growth: 5.9K
Growth Rate: 25.72%
Updated:November 14 2023
huggingface.co

Total runs: 15.2K
Run Growth: 14.1K
Growth Rate: 92.86%
Updated:January 15 2025
huggingface.co

Total runs: 15.2K
Run Growth: -50.3K
Growth Rate: -331.31%
Updated:April 16 2025
huggingface.co

Total runs: 7.7K
Run Growth: -165.8K
Growth Rate: -2163.59%
Updated:October 12 2023
huggingface.co

Total runs: 2.5K
Run Growth: -708
Growth Rate: -28.55%
Updated:April 10 2026
huggingface.co

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

Total runs: 1.4K
Run Growth: 795
Growth Rate: 55.63%
Updated:November 28 2024
huggingface.co

Total runs: 1.4K
Run Growth: -542
Growth Rate: -38.47%
Updated:February 07 2024
huggingface.co

Total runs: 1.2K
Run Growth: 1.2K
Growth Rate: 100.00%
Updated:August 11 2026
huggingface.co

Total runs: 1.2K
Run Growth: -775
Growth Rate: -63.11%
Updated:December 25 2025
huggingface.co

Total runs: 1.2K
Run Growth: 1.1K
Growth Rate: 89.08%
Updated:June 24 2024
huggingface.co

Total runs: 1.2K
Run Growth: 996
Growth Rate: 85.71%
Updated:June 24 2024
huggingface.co

Total runs: 981
Run Growth: 866
Growth Rate: 88.28%
Updated:December 21 2023
huggingface.co

Total runs: 935
Run Growth: 147
Growth Rate: 15.72%
Updated:March 01 2026
huggingface.co

Total runs: 929
Run Growth: 823
Growth Rate: 88.59%
Updated:August 07 2025
huggingface.co

Total runs: 918
Run Growth: 196
Growth Rate: 21.35%
Updated:May 23 2025
huggingface.co

Total runs: 838
Run Growth: -112
Growth Rate: -13.37%
Updated:April 18 2023
huggingface.co

Total runs: 762
Run Growth: -17.7K
Growth Rate: -2318.90%
Updated:July 13 2026
huggingface.co

Total runs: 678
Run Growth: 400
Growth Rate: 59.00%
Updated:December 25 2025
huggingface.co

Total runs: 675
Run Growth: -595
Growth Rate: -88.15%
Updated:December 31 2022
huggingface.co

Total runs: 657
Run Growth: -306
Growth Rate: -46.58%
Updated:October 23 2024
huggingface.co

Total runs: 641
Run Growth: 619
Growth Rate: 100.00%
Updated:August 11 2026
huggingface.co

Total runs: 585
Run Growth: 465
Growth Rate: 79.49%
Updated:August 07 2025
huggingface.co

Total runs: 550
Run Growth: 195
Growth Rate: 35.45%
Updated:October 27 2023
huggingface.co

Total runs: 549
Run Growth: -266
Growth Rate: -48.45%
Updated:April 19 2024
huggingface.co

Total runs: 501
Run Growth: -17.2K
Growth Rate: -3433.13%
Updated:July 13 2026
huggingface.co

Total runs: 429
Run Growth: 241
Growth Rate: 56.18%
Updated:March 07 2024
huggingface.co

Total runs: 366
Run Growth: 286
Growth Rate: 78.14%
Updated:April 10 2026
huggingface.co

Total runs: 354
Run Growth: 251
Growth Rate: 70.90%
Updated:July 13 2026
huggingface.co

Total runs: 336
Run Growth: 290
Growth Rate: 86.31%
Updated:June 06 2025
huggingface.co

Total runs: 324
Run Growth: 191
Growth Rate: 58.95%
Updated:October 23 2024
huggingface.co

Total runs: 318
Run Growth: 251
Growth Rate: 78.93%
Updated:July 17 2026
huggingface.co

Total runs: 307
Run Growth: 219
Growth Rate: 71.34%
Updated:April 10 2026
huggingface.co

Total runs: 289
Run Growth: -146
Growth Rate: -50.52%
Updated:July 13 2026
huggingface.co

Total runs: 281
Run Growth: -83
Growth Rate: -29.54%
Updated:July 13 2026
huggingface.co

Total runs: 273
Run Growth: -179
Growth Rate: -65.57%
Updated:October 24 2024
huggingface.co

Total runs: 269
Run Growth: 201
Growth Rate: 74.72%
Updated:May 13 2024
huggingface.co

Total runs: 255
Run Growth: 131
Growth Rate: 51.37%
Updated:July 13 2026
huggingface.co

Total runs: 255
Run Growth: 231
Growth Rate: 90.59%
Updated:February 07 2024
huggingface.co

Total runs: 240
Run Growth: 154
Growth Rate: 64.17%
Updated:February 11 2025
huggingface.co

Total runs: 238
Run Growth: 163
Growth Rate: 68.49%
Updated:April 10 2026
huggingface.co

Total runs: 200
Run Growth: 100
Growth Rate: 50.00%
Updated:July 13 2026
huggingface.co

Total runs: 192
Run Growth: 165
Growth Rate: 85.94%
Updated:May 13 2024
huggingface.co

Total runs: 184
Run Growth: -565
Growth Rate: -307.07%
Updated:July 13 2026
huggingface.co

Total runs: 182
Run Growth: 151
Growth Rate: 82.97%
Updated:May 31 2024
huggingface.co

Total runs: 179
Run Growth: 150
Growth Rate: 83.80%
Updated:May 13 2024
huggingface.co

Total runs: 164
Run Growth: 61
Growth Rate: 37.20%
Updated:July 13 2026
huggingface.co

Total runs: 139
Run Growth: -68
Growth Rate: -48.92%
Updated:April 02 2024
huggingface.co

Total runs: 138
Run Growth: 119
Growth Rate: 86.23%
Updated:July 05 2024
huggingface.co

Total runs: 134
Run Growth: 115
Growth Rate: 85.82%
Updated:July 02 2024