Introduction

The NV-EmbedCode model is a 7B Mistral-based embedding model optimized for code retrieval, supporting text, code, and hybrid queries.

Code retrieval is a critical task in many domains including coding assistance, code explanation, summarization, and documentation search. NV-EmbedCode transforms the input code or textual data into dense vector representations, known as embeddings, enabling effective retrieval and search.

For technical details, refer to our paper: Nemotron-CORTEXA: Enhancing LLM Agents for Software Engineering Tasks via Improved Localization and Solution Diversity

Intended use

The NV-EmbedCode model is most suitable for users who want to build a code retrieval system over a large text or code corpus, leveraging the latest dense retrieval technologies.

License/Terms of Use

The use of this model is governed by the NVIDIA AI Foundation Models Community License Agreement and the Apache License 2.0.

Model Details

Evaluation Results:

We evaluated NV-EmbedCode model using the CoIR benchmark and a curated set based on SWE-bench. CoIR consists of 10 code datasets across four retrieval tasks: (1) Text-to-Code Retrieval, (2) Code-to-Code Retrieval, (3) Code-to-Text Retrieval, and (4) Hybrid Code Retrieval. The default evaluation metric for CoIR is average NDCG@10 across all datasets. SWE-bench originally consists of real-world software engineering problems from GitHub issues and their corresponding pull requests. We adapted it into a retrieval task, where the goal is to identify the files that need to be edited to resolve an issue. These files are identified using the pull request that solved the issue. For SWE-bench Lite, we use Recall@1 to measure whether the top retrieved file is the correct one for resolving the issue, as each instance typically involves editing just one file. For more detailed evaluation results on SWE-bench, please refer to our paper.

Retrieval Method CoIR Main Score (NDCG@10) SWE-bench Lite (Recall@1)
NV-EmbedCode 72.45% 70.33%
NV-EmbedQA-Mistral-7B-v2 60.08% 61.33%
SFR-Embedding-Code-2B_R 67.41% 47.00%
SFR-Mistral-2_R 61.85% 60.33%
BM25 - 42.33%

Usage

Direct Usage (Sentence Transformers)

First install the following libraries:

pip install transformers==4.37.2 sentence_transformers

Then you can load this model and run inference.

from sentence_transformers import SentenceTransformer, util

# Task instructions for different retrieval scenarios
task_instructions = {
    "general": "Retrieve code or text based on user query",
    "originalbug": "Given a bug description, retrieve codes that need to be edited to resolve it.",
    "llmsummary": "Given a summary of bug description generated by an LLM, retrieve codes that need to be edited to resolve it."
}

# Example queries and corpus
queries = [
    "Function to calculate the sum of two numbers",
    "Recursive function to calculate the factorial of a number",
]

docs = [
    "def add(a, b):\n    return a + b",
    "def factorial(n):\n    return 1 if n==0 else n*factorial(n-1)",
]

# Prepare prompt prefix for corpus
query_prefix = f"Instruct: {task_instructions['general']}\nQuery: "

# Load model
model = SentenceTransformer('nvidia/NV-EmbedCode-7b-v1', trust_remote_code=True)

# Encode queries and documents
query_emb = model.encode(queries, prompt=query_prefix, normalize_embeddings=True)
doc_emb = model.encode(docs, normalize_embeddings=True)

# Compute similarity scores
scores = util.cos_sim(query_emb, doc_emb) * 100
print(scores.tolist())
# [[68.55826568603516, 24.0609130859375], [28.60508918762207, 76.94281005859375]]

Framework Versions

  • Python: 3.11.10
  • Sentence Transformers: 3.0.0
  • Transformers: 4.37.2
  • PyTorch: 2.2.0+cu121
  • Accelerate: 0.34.2
  • Datasets: 3.0.1
  • Tokenizers: 0.15.2

Citation

If you find this model useful in your research, please consider citing:

@inproceedings{nemotroncortexa,
  title={Nemotron-{CORTEXA}: Enhancing {LLM} Agents for Software Engineering Tasks via Improved Localization and Solution Diversity},
  author={Atefeh Sohrabizadeh and Jialin Song and Mingjie Liu and Rajarshi Roy and Chankyu Lee and Jonathan Raiman and Bryan Catanzaro},
  booktitle={Forty-second International Conference on Machine Learning},
  year={2025},
  url={https://openreview.net/forum?id=k6p8UKRdH7}
}
Downloads last month
62
Safetensors
Model size
7.11B params
Tensor type
F32
ยท
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support