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.
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.
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.
1from sentence_transformers import SentenceTransformer, util
23# Task instructions for different retrieval scenarios4task_instructions ={5"general":"Retrieve code or text based on user query",6"originalbug":"Given a bug description, retrieve codes that need to be edited to resolve it.",7"llmsummary":"Given a summary of bug description generated by an LLM, retrieve codes that need to be edited to resolve it."8}910# Example queries and corpus11queries =[12"Function to calculate the sum of two numbers",13"Recursive function to calculate the factorial of a number",14]1516docs =[17"def add(a, b):\n return a + b",18"def factorial(n):\n return 1 if n==0 else n*factorial(n-1)",19]2021# Prepare prompt prefix for corpus22query_prefix =f"Instruct: {task_instructions['general']}\nQuery: "2324# Load model25model = SentenceTransformer('nvidia/NV-EmbedCode-7b-v1', trust_remote_code=True)2627# Encode queries and documents28query_emb = model.encode(queries, prompt=query_prefix, normalize_embeddings=True)29doc_emb = model.encode(docs, normalize_embeddings=True)3031# Compute similarity scores32scores = util.cos_sim(query_emb, doc_emb)*10033print(scores.tolist())34# [[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}
}