Feature Extraction
sentence-transformers
ONNX
Safetensors
GGUF
English
code
roberta
python
javascript
go
java
ruby
php
code-search
code-embeddings
matryoshka
unixcoder
mteb
Eval Results (legacy)
text-embeddings-inference
Instructions to use divakantpandey/code-embed-v2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use divakantpandey/code-embed-v2 with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("divakantpandey/code-embed-v2") sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use divakantpandey/code-embed-v2 with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible embeddings server: llama serve -hf divakantpandey/code-embed-v2:Q8_0 --embeddings
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible embeddings server: llama serve -hf divakantpandey/code-embed-v2:Q8_0 --embeddings
Build from source code
git clone https://github.com/ggml-org/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-app # Start a local OpenAI-compatible embeddings server: ./build/bin/llama serve -hf divakantpandey/code-embed-v2:Q8_0 --embeddings
- Llama new
- LM Studio
- Jan
- Ollama
How to use divakantpandey/code-embed-v2 with Ollama:
ollama run hf.co/divakantpandey/code-embed-v2:Q8_0
- Unsloth Desktop
- Docker Model Runner
How to use divakantpandey/code-embed-v2 with Docker Model Runner:
docker model run hf.co/divakantpandey/code-embed-v2:Q8_0
- Lemonade
How to use divakantpandey/code-embed-v2 with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull divakantpandey/code-embed-v2:Q8_0
Run and chat with the model
lemonade run user.code-embed-v2-Q8_0
List all available models
lemonade list
- Atomic Chat
Set max length to 1024 and correct model card
Browse files- Modelfile +1 -1
- README.md +292 -454
- code_embed_config.json +3 -3
- sentence_bert_config.json +2 -2
- tokenizer_config.json +3 -3
Modelfile
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# Ollama Modelfile for Code-Embed v2
|
| 2 |
FROM ./outputs/gguf/code-embed-v2-q8_0.gguf
|
| 3 |
|
| 4 |
-
PARAMETER num_ctx
|
| 5 |
|
| 6 |
|
|
|
|
| 1 |
# Ollama Modelfile for Code-Embed v2
|
| 2 |
FROM ./outputs/gguf/code-embed-v2-q8_0.gguf
|
| 3 |
|
| 4 |
+
PARAMETER num_ctx 1024
|
| 5 |
|
| 6 |
|
README.md
CHANGED
|
@@ -11,7 +11,6 @@ tags:
|
|
| 11 |
- php
|
| 12 |
- code-search
|
| 13 |
- code-embeddings
|
| 14 |
-
- multilingual
|
| 15 |
- sentence-transformers
|
| 16 |
- matryoshka
|
| 17 |
- unixcoder
|
|
@@ -22,364 +21,343 @@ tags:
|
|
| 22 |
license: cc-by-nc-4.0
|
| 23 |
pipeline_tag: feature-extraction
|
| 24 |
library_name: sentence-transformers
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
-
|
| 28 |
-
- map
|
| 29 |
model-index:
|
| 30 |
- name: code-embed-v2
|
| 31 |
results:
|
| 32 |
- task:
|
| 33 |
-
type:
|
| 34 |
-
name:
|
| 35 |
dataset:
|
| 36 |
-
name:
|
| 37 |
-
type: mteb/
|
|
|
|
| 38 |
metrics:
|
| 39 |
- type: main_score
|
| 40 |
-
value: 0.
|
| 41 |
-
name:
|
| 42 |
-
- type: reranking_mean
|
| 43 |
-
value: 0.4894
|
| 44 |
-
name: Reranking Mean
|
| 45 |
-
- type: retrieval_mean
|
| 46 |
-
value: 0.3831
|
| 47 |
-
name: Retrieval Mean
|
| 48 |
- task:
|
| 49 |
-
type:
|
| 50 |
-
name:
|
| 51 |
dataset:
|
| 52 |
-
name:
|
| 53 |
-
type:
|
| 54 |
split: test
|
| 55 |
metrics:
|
| 56 |
-
- type:
|
| 57 |
-
value: 0.
|
| 58 |
-
name:
|
| 59 |
- task:
|
| 60 |
type: retrieval
|
| 61 |
-
name:
|
| 62 |
dataset:
|
| 63 |
-
name:
|
| 64 |
-
type: mteb/
|
|
|
|
| 65 |
metrics:
|
| 66 |
-
- type:
|
| 67 |
-
value: 0.
|
| 68 |
-
name:
|
| 69 |
- task:
|
| 70 |
type: retrieval
|
| 71 |
-
name:
|
| 72 |
dataset:
|
| 73 |
-
name:
|
| 74 |
-
type: mteb/
|
|
|
|
| 75 |
metrics:
|
| 76 |
-
- type:
|
| 77 |
-
value: 0.
|
| 78 |
-
name:
|
| 79 |
- task:
|
| 80 |
-
type:
|
| 81 |
-
name:
|
| 82 |
dataset:
|
| 83 |
-
name:
|
| 84 |
-
type:
|
| 85 |
split: test
|
| 86 |
metrics:
|
| 87 |
-
- type:
|
| 88 |
-
value: 0.
|
| 89 |
-
name:
|
| 90 |
- task:
|
| 91 |
-
type:
|
| 92 |
-
name:
|
| 93 |
dataset:
|
| 94 |
-
name:
|
| 95 |
-
type:
|
| 96 |
-
split:
|
| 97 |
metrics:
|
| 98 |
-
- type:
|
| 99 |
-
value: 0.
|
| 100 |
-
name:
|
| 101 |
- task:
|
| 102 |
type: retrieval
|
| 103 |
-
name:
|
| 104 |
dataset:
|
| 105 |
name: CodeFeedbackMT
|
| 106 |
-
type:
|
| 107 |
split: test
|
| 108 |
metrics:
|
| 109 |
-
- type:
|
| 110 |
value: 0.6164
|
| 111 |
-
name:
|
| 112 |
- task:
|
| 113 |
type: retrieval
|
| 114 |
-
name:
|
| 115 |
dataset:
|
| 116 |
-
name:
|
| 117 |
-
type:
|
| 118 |
split: test
|
| 119 |
metrics:
|
| 120 |
-
- type:
|
| 121 |
-
value: 0.
|
| 122 |
-
name:
|
| 123 |
- task:
|
| 124 |
type: retrieval
|
| 125 |
-
name:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
dataset:
|
| 127 |
name: CodeSearchNetRetrieval
|
| 128 |
-
type:
|
| 129 |
split: test
|
| 130 |
metrics:
|
| 131 |
-
- type:
|
| 132 |
value: 0.8574
|
| 133 |
-
name:
|
| 134 |
- task:
|
| 135 |
type: retrieval
|
| 136 |
-
name:
|
| 137 |
dataset:
|
| 138 |
-
name:
|
| 139 |
-
type:
|
| 140 |
split: test
|
| 141 |
metrics:
|
| 142 |
-
- type:
|
| 143 |
-
value: 0.
|
| 144 |
-
name:
|
| 145 |
- task:
|
| 146 |
type: retrieval
|
| 147 |
-
name:
|
| 148 |
dataset:
|
| 149 |
-
name:
|
| 150 |
-
type:
|
| 151 |
split: test
|
| 152 |
metrics:
|
| 153 |
-
- type:
|
| 154 |
-
value: 0.
|
| 155 |
-
name:
|
| 156 |
- task:
|
| 157 |
-
type:
|
| 158 |
-
name:
|
| 159 |
dataset:
|
| 160 |
-
name:
|
| 161 |
-
type:
|
| 162 |
split: test
|
| 163 |
metrics:
|
| 164 |
-
- type:
|
| 165 |
-
value: 0.
|
| 166 |
-
name:
|
| 167 |
- task:
|
| 168 |
type: retrieval
|
| 169 |
-
name:
|
| 170 |
dataset:
|
| 171 |
-
name:
|
| 172 |
-
type:
|
| 173 |
split: test
|
| 174 |
metrics:
|
| 175 |
-
- type:
|
| 176 |
-
value: 0.
|
| 177 |
-
name:
|
| 178 |
- task:
|
| 179 |
-
type:
|
| 180 |
-
name:
|
| 181 |
dataset:
|
| 182 |
-
name:
|
| 183 |
-
type:
|
| 184 |
split: test
|
| 185 |
metrics:
|
| 186 |
-
- type:
|
| 187 |
-
value: 0.
|
| 188 |
-
name:
|
| 189 |
- task:
|
| 190 |
type: retrieval
|
| 191 |
-
name:
|
| 192 |
dataset:
|
| 193 |
name: CorebC2TRetrieval
|
| 194 |
-
type:
|
| 195 |
split: test
|
| 196 |
metrics:
|
| 197 |
-
- type:
|
| 198 |
value: 0.6073
|
| 199 |
-
name:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
- task:
|
| 201 |
type: retrieval
|
| 202 |
-
name:
|
| 203 |
dataset:
|
| 204 |
-
name:
|
| 205 |
-
type:
|
| 206 |
split: test
|
| 207 |
metrics:
|
| 208 |
-
- type:
|
| 209 |
-
value: 0.
|
| 210 |
-
name:
|
| 211 |
- task:
|
| 212 |
type: retrieval
|
| 213 |
-
name:
|
| 214 |
dataset:
|
| 215 |
name: CosQA
|
| 216 |
-
type:
|
| 217 |
split: test
|
| 218 |
metrics:
|
| 219 |
-
- type:
|
| 220 |
-
value: 0.
|
| 221 |
-
name:
|
| 222 |
- task:
|
| 223 |
type: retrieval
|
| 224 |
-
name:
|
| 225 |
dataset:
|
| 226 |
-
name:
|
| 227 |
-
type:
|
| 228 |
split: test
|
| 229 |
metrics:
|
| 230 |
-
- type:
|
| 231 |
-
value: 0.
|
| 232 |
-
name:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
---
|
| 234 |
|
| 235 |
-
#
|
| 236 |
|
| 237 |
-
|
| 238 |
-
>
|
| 239 |
-
> On the official **Massive Text Embedding Benchmark (MTEB)**, CodeEmbed v2 dominates the 150M compact model category ($\le$ 150M parameters): **Rank #3 on CoREB(v1)**, **Rank #8 on CoIR**, and **Rank #9 on MTEB(Code, v1)**, alongside **Rank #3 Worldwide** across all model sizes on Code-to-Code Reranking (`CorebC2CReranking`). It consistently outperforms enterprise frontier models including Google Gemini Embedding and Microsoft E5-Large in conversational code feedback and deep learning framework translation at a fraction of the compute and memory footprint.
|
| 240 |
|
| 241 |
[](https://huggingface.co/divakantpandey/code-embed-v2)
|
| 242 |
-
[-Rank%20%238-gold)](https://mteb-leaderboard.hf.space/benchmark/CoIR?minSize=2&maxSize=150)
|
| 246 |
-
[%20(≤150M)-Rank%20%239-gold)](https://mteb-leaderboard.hf.space/benchmark/MTEB(Code%2C%20v1)?minSize=2&maxSize=150)
|
| 247 |
-
[%20Overall-Rank%20%2319-orange)](https://mteb-leaderboard.hf.space/models/divakantpandey/code-embed-v2)
|
| 248 |
-
[](https://mteb-leaderboard.hf.space/models/divakantpandey/code-embed-v2)
|
| 249 |
-
[%20Overall-Rank%20%2369-orange)](https://mteb-leaderboard.hf.space/models/divakantpandey/code-embed-v2)
|
| 250 |
-
[](https://mteb-leaderboard.hf.space/models/divakantpandey/code-embed-v2)
|
| 251 |
-
[]()
|
| 252 |
-
[-brightgreen)](https://github.com/embeddings-benchmark/results/pull/803)
|
| 253 |
-
[](https://sbert.net)
|
| 254 |
[](https://creativecommons.org/licenses/by-nc/4.0/)
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
-
|
| 287 |
-
|
| 288 |
-
##
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
- Evaluated by MTEB with an Openness Score of 4/5: Open Weights (✅), Open Training Code (✅), Open Training Data (✅), and Complete Model Card (✅).
|
| 335 |
-
|
| 336 |
-
---
|
| 337 |
-
|
| 338 |
-
### 📊 Official MTEB Scorecard & Global Ranks *(Snapshot: October 24, 2026 — Sorted by Rank Ascending)*
|
| 339 |
-
|
| 340 |
-
| Benchmark / Task | Category | Metric | Rank (Class / Global) | Total Models | Score | Comparison vs Baselines |
|
| 341 |
-
| :--- | :--- | :---: | :---: | :---: | :---: | :--- |
|
| 342 |
-
| **`CorebC2CReranking`** | Code-to-Code Reranking | MAP | **#3 Global** | 25 | **`0.4205`** | **Top 3 Worldwide**; beats Naver SPLADE 600M (`0.4198`) |
|
| 343 |
-
| **`CoREB(v1)` (Compact Tier)** | Full Benchmark Suite | MeanTask | **#3 (≤150M)** | 8 | **`0.4362`** | **Top 3 Worldwide** in $\le$150M category; [View Filter](https://mteb-leaderboard.hf.space/benchmark/CoREB(v1)?minSize=33&maxSize=150) |
|
| 344 |
-
| **`CoIR` (Compact Tier)** | Full Benchmark Suite | MeanTask | **#8 (≤150M)** | 17 | **`0.5343`** | **Top 8 Worldwide** in $\le$150M category; [View Filter](https://mteb-leaderboard.hf.space/benchmark/CoIR?minSize=2&maxSize=150) |
|
| 345 |
-
| **`MTEB(Code, v1)` (Compact Tier)** | Full Benchmark Suite | MeanTask | **#9 (≤150M)** | 17 | **`0.5475`** | **Top 9 Worldwide** in $\le$150M category; [View Filter](https://mteb-leaderboard.hf.space/benchmark/MTEB(Code%2C%20v1)?minSize=2&maxSize=150) |
|
| 346 |
-
| **`CoREB(v1)` Macro-Average** | Full Benchmark Suite | MeanTask | **#19 Global** | 25 | **`0.4362`** | **Top 20 Worldwide** (Reranking: `0.4894`, Retrieval: `0.3831`) |
|
| 347 |
-
| **`CorebC2TReranking`** | Code-to-Text Reranking | MAP | **#20 Global** | 25 | **`0.7937`** | ~80% pairwise alignment accuracy |
|
| 348 |
-
| **`CorebT2CReranking`** | Text-to-Code Reranking | MAP | **#21 Global** | 25 | **`0.2539`** | Near 7B LLM ceiling (`0.2882`) |
|
| 349 |
-
| **`CodeTransOceanDL`** | DL Framework Translation | NDCG@10 | **#21 Global** | 246 | **`0.3533`** | **Beats Gemini-Embedding-001** (`0.3147`) & **E5-Large** (`0.3128`) |
|
| 350 |
-
| **`CodeFeedbackMT`** | Multi-Turn Code Assistant | NDCG@10 | **#45 Global** | 246 | **`0.6164`** | **Beats Gemini-Embedding-001** (`0.5628`) & **E5-Large** (`0.4278`) |
|
| 351 |
-
| **`COIRCodeSearchNetRetrieval`** | Full 300k Corpus Code Search | NDCG@10 | **#45 Global** | 264 | **`0.7488`** | High-scale multilingual code retrieval |
|
| 352 |
-
| **`CodeFeedbackST`** | Single-Turn Code Feedback | NDCG@10 | **#52 Global** | 246 | **`0.6680`** | Robust assistant instruction alignment |
|
| 353 |
-
| **`CoIR` Macro-Average** | Full Benchmark Suite | MeanTask | **#60 Global** | 264 | **`0.5343`** | 100% Zero-Shot generalizability across all models |
|
| 354 |
-
| **`CodeSearchNetRetrieval`** | Multilingual Code Search | NDCG@10 | **#66 Global** | 265 | **`0.8574`** | **Beats multilingual-e5-large** (`0.8412`) |
|
| 355 |
-
| **`MTEB(Code, v1)` Macro-Average** | Full Benchmark Suite | MeanTask | **#69 Global** | 265 | **`0.5475`** | Top quartile of all models across all sizes |
|
| 356 |
-
| **`CodeTransOceanContest`** | Competitive Code Translation | NDCG@10 | **#82 Global** | 246 | **`0.6814`** | Cross-language algorithmic problem matching |
|
| 357 |
-
| **`StackOverflowQA`** | Developer Q&A Matching | NDCG@10 | **#92 Global** | 246 | **`0.7131`** | High-precision question-to-code resolution |
|
| 358 |
-
| **`CodeSearchNetCCRetrieval`** | Code Clone Retrieval | NDCG@10 | — | 246 | **`0.5661`** | Multilingual semantic clone search |
|
| 359 |
-
| **`SyntheticText2SQL`** | Natural Language to SQL | NDCG@10 | — | 246 | **`0.4938`** | Schema-aware SQL query retrieval |
|
| 360 |
-
| **`CosQA`** | Web Query Code QA | NDCG@10 | — | 246 | **`0.3000`** | Real-world search engine query matching |
|
| 361 |
-
| **`AppsRetrieval`** | Complex Problem Retrieval | NDCG@10 | — | 246 | **`0.2017`** | Algorithmic specification retrieval |
|
| 362 |
-
|
| 363 |
-
*Note: All scores above were evaluated with `mteb==2.26.16` and official reference implementation under revision `e3b5ded5d2448ee53043f95c414635e2d8e915e1`.*
|
| 364 |
-
|
| 365 |
-
*Note: All scores above were evaluated with `mteb==2.26.16` and official reference implementation under revision `e3b5ded5d2448ee53043f95c414635e2d8e915e1`.*
|
| 366 |
-
|
| 367 |
-
---
|
| 368 |
-
|
| 369 |
-
## 💻 Production Usage & Complex Examples
|
| 370 |
-
|
| 371 |
-
### Example 1: Basic Semantic Code Search with `sentence-transformers`
|
| 372 |
|
| 373 |
```python
|
| 374 |
from sentence_transformers import SentenceTransformer
|
| 375 |
|
| 376 |
-
# Load model directly from Hugging Face Hub
|
| 377 |
model = SentenceTransformer("divakantpandey/code-embed-v2")
|
| 378 |
|
| 379 |
queries = [
|
| 380 |
"binary search implementation with edge handling",
|
| 381 |
"http json get request with retry logic",
|
| 382 |
-
"worker pool concurrency pattern using channels"
|
| 383 |
]
|
| 384 |
|
| 385 |
code_candidates = [
|
|
@@ -419,36 +397,25 @@ code_candidates = [
|
|
| 419 |
}
|
| 420 |
wg.Wait()
|
| 421 |
close(results)
|
| 422 |
-
}"""
|
| 423 |
]
|
| 424 |
|
| 425 |
-
# Compute dense vectors (1536-dimensional native)
|
| 426 |
query_vecs = model.encode(queries, normalize_embeddings=True)
|
| 427 |
code_vecs = model.encode(code_candidates, normalize_embeddings=True)
|
| 428 |
-
|
| 429 |
-
# Pairwise cosine similarity
|
| 430 |
-
similarity_matrix = query_vecs @ code_vecs.T
|
| 431 |
|
| 432 |
for i, q in enumerate(queries):
|
| 433 |
-
|
| 434 |
-
print(f"
|
| 435 |
-
print(f"Top Match (Score: {similarity_matrix[i][best_match_idx]:.4f}):")
|
| 436 |
-
print(code_candidates[best_match_idx][:100] + "...\n")
|
| 437 |
```
|
| 438 |
|
| 439 |
-
--
|
| 440 |
-
|
| 441 |
-
### Example 2: Cross-Language Clone & Duplicate Detection
|
| 442 |
-
|
| 443 |
-
Search across repositories to find functionally identical functions implemented in different programming languages:
|
| 444 |
|
| 445 |
```python
|
| 446 |
from sentence_transformers import SentenceTransformer
|
| 447 |
-
import numpy as np
|
| 448 |
|
| 449 |
model = SentenceTransformer("divakantpandey/code-embed-v2")
|
| 450 |
|
| 451 |
-
# Reference Python implementation
|
| 452 |
reference_python = """
|
| 453 |
def quicksort(arr):
|
| 454 |
if len(arr) <= 1:
|
|
@@ -460,9 +427,7 @@ def quicksort(arr):
|
|
| 460 |
return quicksort(left) + middle + quicksort(right)
|
| 461 |
"""
|
| 462 |
|
| 463 |
-
# Candidate implementations in different languages
|
| 464 |
candidates = [
|
| 465 |
-
# Candidate A: Quicksort in Go
|
| 466 |
"""func QuickSort(arr []int) []int {
|
| 467 |
if len(arr) <= 1 { return arr }
|
| 468 |
pivot := arr[len(arr)/2]
|
|
@@ -472,7 +437,6 @@ candidates = [
|
|
| 472 |
}
|
| 473 |
return append(append(QuickSort(left), middle...), QuickSort(right)...)
|
| 474 |
}""",
|
| 475 |
-
# Candidate B: Bubble sort in Go (different algorithm)
|
| 476 |
"""func BubbleSort(arr []int) []int {
|
| 477 |
n := len(arr)
|
| 478 |
for i := 0; i < n-1; i++ {
|
|
@@ -481,26 +445,23 @@ candidates = [
|
|
| 481 |
}
|
| 482 |
}
|
| 483 |
return arr
|
| 484 |
-
}"""
|
| 485 |
]
|
| 486 |
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
scores = cand_embs @ ref_emb
|
| 491 |
-
print("Candidate A (Go Quicksort) Clone Similarity:", scores[0]) # High similarity (>0.85)
|
| 492 |
-
print("Candidate B (Go Bubble Sort) Clone Similarity:", scores[1]) # Significantly lower
|
| 493 |
```
|
| 494 |
|
| 495 |
-
---
|
| 496 |
|
| 497 |
-
###
|
| 498 |
|
| 499 |
-
|
| 500 |
|
| 501 |
```python
|
| 502 |
-
from sentence_transformers import SentenceTransformer
|
| 503 |
import numpy as np
|
|
|
|
| 504 |
|
| 505 |
model = SentenceTransformer("divakantpandey/code-embed-v2")
|
| 506 |
|
|
@@ -509,239 +470,116 @@ corpus = [
|
|
| 509 |
"import jwt, datetime\ndef make_token(uid): return jwt.encode({'sub': uid, 'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=1)}, 'secret')",
|
| 510 |
"import hashlib\ndef hash_password(pwd): return hashlib.sha256(pwd.encode()).hexdigest()",
|
| 511 |
"def connect_db(): return psycopg2.connect('dbname=app user=postgres')",
|
| 512 |
-
"import jwt\ndef decode_token(tok): return jwt.decode(tok, 'secret', algorithms=['HS256'])"
|
| 513 |
-
] * 250 # 1,000 document simulated corpus
|
| 514 |
-
|
| 515 |
-
# --- STAGE 1: Coarse Filtering (128 Dimensions -> 12x storage & speed savings) ---
|
| 516 |
-
coarse_query = model.encode(query, truncate_dim=128, normalize_embeddings=True)
|
| 517 |
-
coarse_corpus = model.encode(corpus, truncate_dim=128, normalize_embeddings=True)
|
| 518 |
-
|
| 519 |
-
# Fast candidate selection (top 10)
|
| 520 |
-
coarse_scores = coarse_corpus @ coarse_query
|
| 521 |
-
top_10_indices = np.argsort(coarse_scores)[-10:][::-1]
|
| 522 |
-
|
| 523 |
-
# --- STAGE 2: Fine Reranking (1536 Dimensions) ---
|
| 524 |
-
top_candidates = [corpus[idx] for idx in top_10_indices]
|
| 525 |
-
fine_query = model.encode(query, truncate_dim=1536, normalize_embeddings=True)
|
| 526 |
-
fine_candidates = model.encode(top_candidates, truncate_dim=1536, normalize_embeddings=True)
|
| 527 |
-
|
| 528 |
-
fine_scores = fine_candidates @ fine_query
|
| 529 |
-
final_best_idx = top_10_indices[np.argmax(fine_scores)]
|
| 530 |
-
|
| 531 |
-
print(f"Top Matched Snippet (Final score: {fine_scores.max():.4f}):")
|
| 532 |
-
print(corpus[final_best_idx])
|
| 533 |
-
```
|
| 534 |
-
|
| 535 |
-
---
|
| 536 |
-
|
| 537 |
-
### Example 4: Debugging & Crash Traceback Matching
|
| 538 |
-
|
| 539 |
-
Retrieve the buggy function in your codebase directly from a runtime exception traceback:
|
| 540 |
-
|
| 541 |
-
```python
|
| 542 |
-
from sentence_transformers import SentenceTransformer
|
| 543 |
-
|
| 544 |
-
model = SentenceTransformer("divakantpandey/code-embed-v2")
|
| 545 |
-
|
| 546 |
-
traceback_query = """
|
| 547 |
-
Traceback (most recent call last):
|
| 548 |
-
File "payment_service.py", line 42, in process_transaction
|
| 549 |
-
charge_amount = invoice.total * (1 - discount_rate)
|
| 550 |
-
TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'
|
| 551 |
-
"""
|
| 552 |
-
|
| 553 |
-
repo_functions = [
|
| 554 |
-
"""def calculate_shipping(weight, distance):
|
| 555 |
-
base_rate = 5.0
|
| 556 |
-
return base_rate + (weight * 0.5) + (distance * 0.1)""",
|
| 557 |
-
|
| 558 |
-
"""def process_transaction(user_id, invoice, discount_rate=None):
|
| 559 |
-
if discount_rate is None:
|
| 560 |
-
discount_rate = 0.0
|
| 561 |
-
charge_amount = invoice.total * (1 - discount_rate)
|
| 562 |
-
return payment_gateway.charge(user_id, charge_amount)""",
|
| 563 |
-
|
| 564 |
-
"""def validate_email(email_address):
|
| 565 |
-
import re
|
| 566 |
-
regex = r'^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w+$'
|
| 567 |
-
return bool(re.search(regex, email_address))"""
|
| 568 |
]
|
| 569 |
|
| 570 |
-
|
| 571 |
-
|
|
|
|
|
|
|
| 572 |
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
|
|
|
| 576 |
```
|
| 577 |
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
### Example 5: Qdrant Vector Database Integration
|
| 581 |
|
| 582 |
```python
|
| 583 |
from qdrant_client import QdrantClient
|
| 584 |
from qdrant_client.models import Distance, VectorParams, PointStruct
|
| 585 |
from sentence_transformers import SentenceTransformer
|
| 586 |
|
| 587 |
-
# Initialize client & model
|
| 588 |
qdrant = QdrantClient(":memory:")
|
| 589 |
model = SentenceTransformer("divakantpandey/code-embed-v2")
|
| 590 |
|
| 591 |
-
collection_name = "codebase_symbols"
|
| 592 |
qdrant.create_collection(
|
| 593 |
-
collection_name=
|
| 594 |
vectors_config=VectorParams(size=1536, distance=Distance.COSINE),
|
| 595 |
)
|
| 596 |
|
| 597 |
-
# Ingest code files with metadata
|
| 598 |
files = [
|
| 599 |
-
{"id": 1, "path": "auth/session.go", "
|
| 600 |
-
{"id": 2, "path": "models/user.py", "
|
| 601 |
]
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
qdrant.upsert(collection_name=collection_name, points=points)
|
| 609 |
-
|
| 610 |
-
# Search with metadata filtering
|
| 611 |
-
query_emb = model.encode("validate active user session", normalize_embeddings=True).tolist()
|
| 612 |
-
search_results = qdrant.query_points(
|
| 613 |
-
collection_name=collection_name,
|
| 614 |
-
query=query_emb,
|
| 615 |
-
limit=1
|
| 616 |
)
|
| 617 |
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 621 |
```
|
| 622 |
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
### Example 6: LangChain Code Repository Q&A (RAG Pipeline)
|
| 626 |
|
| 627 |
```python
|
| 628 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 629 |
from langchain_community.vectorstores import Chroma
|
| 630 |
from langchain_core.documents import Document
|
| 631 |
|
| 632 |
-
# 1. Initialize embeddings with CodeEmbed v2
|
| 633 |
embeddings = HuggingFaceEmbeddings(
|
| 634 |
model_name="divakantpandey/code-embed-v2",
|
| 635 |
-
encode_kwargs={"normalize_embeddings": True}
|
| 636 |
)
|
| 637 |
|
| 638 |
-
# 2. Build local document store
|
| 639 |
documents = [
|
| 640 |
Document(page_content="func RateLimiter(ip string) bool { return limiter.Allow(ip) }", metadata={"file": "security/ratelimit.go"}),
|
| 641 |
-
Document(page_content="def send_metric(name, val): statsd.increment(name, val)", metadata={"file": "telemetry/metrics.py"})
|
| 642 |
]
|
| 643 |
-
|
| 644 |
vector_db = Chroma.from_documents(documents, embeddings)
|
| 645 |
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
matched_docs = retriever.invoke("how do we prevent DDOS attacks on our API endpoints?")
|
| 649 |
-
|
| 650 |
-
for doc in matched_docs:
|
| 651 |
-
print(f"Source: {doc.metadata['file']}")
|
| 652 |
-
print(f"Content: {doc.page_content}")
|
| 653 |
```
|
| 654 |
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
### Example 7: Local Serving with Ollama
|
| 658 |
-
|
| 659 |
-
Run `code-embed-v2` locally with Ollama (native embed API or OpenAI-compatible endpoint).
|
| 660 |
|
| 661 |
> [!NOTE]
|
| 662 |
-
>
|
| 663 |
|
| 664 |
```bash
|
| 665 |
-
# 1. Pull & run directly from Hugging Face
|
| 666 |
ollama pull hf.co/divakantpandey/code-embed-v2:code-embed-v2-q8_0.gguf
|
| 667 |
|
| 668 |
-
# Or if pulled from the Ollama registry:
|
| 669 |
-
# ollama pull divakantpandey/code-embed-v2
|
| 670 |
-
|
| 671 |
-
# 2. Generate embeddings via cURL:
|
| 672 |
curl http://localhost:11434/api/embed -d '{
|
| 673 |
-
"model": "divakantpandey/code-embed-v2",
|
| 674 |
"input": "def binary_search(arr, target):"
|
| 675 |
}'
|
| 676 |
-
|
| 677 |
-
# 3. Query via OpenAI-compatible endpoint (/v1/embeddings):
|
| 678 |
-
curl http://localhost:11434/v1/embeddings \
|
| 679 |
-
-H "Content-Type: application/json" \
|
| 680 |
-
-d '{
|
| 681 |
-
"model": "divakantpandey/code-embed-v2",
|
| 682 |
-
"input": "def binary_search(arr, target):"
|
| 683 |
-
}'
|
| 684 |
```
|
| 685 |
|
| 686 |
-
---
|
| 687 |
-
|
| 688 |
-
### Example 8: High-Performance C++ Inference with `llama.cpp`
|
| 689 |
-
|
| 690 |
-
Use the Q8_0 GGUF quantized weights for standalone C++ embedding generation or an OpenAI-compatible microservice:
|
| 691 |
-
|
| 692 |
```bash
|
| 693 |
-
# Download GGUF weights (135MB)
|
| 694 |
wget https://huggingface.co/divakantpandey/code-embed-v2/resolve/main/code-embed-v2-q8_0.gguf
|
| 695 |
-
|
| 696 |
-
# Start an OpenAI-compatible embedding server:
|
| 697 |
-
llama-server \
|
| 698 |
-
-m code-embed-v2-q8_0.gguf \
|
| 699 |
-
--embedding \
|
| 700 |
-
-c 8192 \
|
| 701 |
-
--port 8080
|
| 702 |
-
|
| 703 |
-
# Query with standard OpenAI Python SDK:
|
| 704 |
-
from openai import OpenAI
|
| 705 |
-
|
| 706 |
-
client = OpenAI(base_url="http://localhost:8080/v1", api_key="not-needed")
|
| 707 |
-
response = client.embeddings.create(
|
| 708 |
-
model="code-embed-v2",
|
| 709 |
-
input="def find_max(nums):"
|
| 710 |
-
)
|
| 711 |
-
print("Vector length:", len(response.data[0].embedding))
|
| 712 |
```
|
| 713 |
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
### Example 9: Production Serving with Text Embeddings Inference (TEI)
|
| 717 |
-
|
| 718 |
-
For enterprise throughput with dynamic batching and token truncation:
|
| 719 |
|
| 720 |
```bash
|
| 721 |
-
# Deploy with Hugging Face TEI (Docker)
|
| 722 |
docker run --gpus all -p 8080:80 \
|
| 723 |
ghcr.io/huggingface/text-embeddings-inference:1.5 \
|
| 724 |
--model-id divakantpandey/code-embed-v2
|
| 725 |
```
|
| 726 |
|
| 727 |
-
--
|
| 728 |
|
| 729 |
-
##
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
howpublished = {\url{https://huggingface.co/divakantpandey/code-embed-v2}}
|
| 742 |
-
}
|
| 743 |
-
```
|
| 744 |
-
- **Commercial Inquiries:** For commercial deployments, SaaS integration, or enterprise licenses, contact:
|
| 745 |
-
- **Maintainer:** Divakant Pandey
|
| 746 |
-
- **Email:** `divakant01@gmail.com`
|
| 747 |
-
- **GitHub:** [@divakantpandey](https://github.com/divakantpandey)
|
|
|
|
| 11 |
- php
|
| 12 |
- code-search
|
| 13 |
- code-embeddings
|
|
|
|
| 14 |
- sentence-transformers
|
| 15 |
- matryoshka
|
| 16 |
- unixcoder
|
|
|
|
| 21 |
license: cc-by-nc-4.0
|
| 22 |
pipeline_tag: feature-extraction
|
| 23 |
library_name: sentence-transformers
|
| 24 |
+
base_model: microsoft/unixcoder-base
|
| 25 |
+
datasets:
|
| 26 |
+
- code-search-net/code_search_net
|
|
|
|
| 27 |
model-index:
|
| 28 |
- name: code-embed-v2
|
| 29 |
results:
|
| 30 |
- task:
|
| 31 |
+
type: retrieval
|
| 32 |
+
name: MTEB(Code, v1)
|
| 33 |
dataset:
|
| 34 |
+
name: MTEB(Code, v1) mean over 12 tasks
|
| 35 |
+
type: mteb/benchmark
|
| 36 |
+
split: test
|
| 37 |
metrics:
|
| 38 |
- type: main_score
|
| 39 |
+
value: 0.5475
|
| 40 |
+
name: mean main_score
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
- task:
|
| 42 |
+
type: retrieval
|
| 43 |
+
name: CoIR
|
| 44 |
dataset:
|
| 45 |
+
name: CoIR mean over 10 tasks
|
| 46 |
+
type: mteb/benchmark
|
| 47 |
split: test
|
| 48 |
metrics:
|
| 49 |
+
- type: main_score
|
| 50 |
+
value: 0.5343
|
| 51 |
+
name: mean main_score
|
| 52 |
- task:
|
| 53 |
type: retrieval
|
| 54 |
+
name: CoREB(v1)
|
| 55 |
dataset:
|
| 56 |
+
name: CoREB(v1) mean over 6 tasks
|
| 57 |
+
type: mteb/benchmark
|
| 58 |
+
split: test
|
| 59 |
metrics:
|
| 60 |
+
- type: main_score
|
| 61 |
+
value: 0.4362
|
| 62 |
+
name: mean main_score
|
| 63 |
- task:
|
| 64 |
type: retrieval
|
| 65 |
+
name: AppsRetrieval
|
| 66 |
dataset:
|
| 67 |
+
name: AppsRetrieval
|
| 68 |
+
type: mteb/AppsRetrieval
|
| 69 |
+
split: test
|
| 70 |
metrics:
|
| 71 |
+
- type: main_score
|
| 72 |
+
value: 0.2017
|
| 73 |
+
name: main_score (ndcg_at_10 or map)
|
| 74 |
- task:
|
| 75 |
+
type: retrieval
|
| 76 |
+
name: COIRCodeSearchNetRetrieval
|
| 77 |
dataset:
|
| 78 |
+
name: COIRCodeSearchNetRetrieval
|
| 79 |
+
type: mteb/COIRCodeSearchNetRetrieval
|
| 80 |
split: test
|
| 81 |
metrics:
|
| 82 |
+
- type: main_score
|
| 83 |
+
value: 0.7488
|
| 84 |
+
name: main_score (ndcg_at_10 or map)
|
| 85 |
- task:
|
| 86 |
+
type: retrieval
|
| 87 |
+
name: CodeEditSearchRetrieval
|
| 88 |
dataset:
|
| 89 |
+
name: CodeEditSearchRetrieval
|
| 90 |
+
type: mteb/CodeEditSearchRetrieval
|
| 91 |
+
split: train
|
| 92 |
metrics:
|
| 93 |
+
- type: main_score
|
| 94 |
+
value: 0.3703
|
| 95 |
+
name: main_score (ndcg_at_10 or map)
|
| 96 |
- task:
|
| 97 |
type: retrieval
|
| 98 |
+
name: CodeFeedbackMT
|
| 99 |
dataset:
|
| 100 |
name: CodeFeedbackMT
|
| 101 |
+
type: mteb/CodeFeedbackMT
|
| 102 |
split: test
|
| 103 |
metrics:
|
| 104 |
+
- type: main_score
|
| 105 |
value: 0.6164
|
| 106 |
+
name: main_score (ndcg_at_10 or map)
|
| 107 |
- task:
|
| 108 |
type: retrieval
|
| 109 |
+
name: CodeFeedbackST
|
| 110 |
dataset:
|
| 111 |
+
name: CodeFeedbackST
|
| 112 |
+
type: mteb/CodeFeedbackST
|
| 113 |
split: test
|
| 114 |
metrics:
|
| 115 |
+
- type: main_score
|
| 116 |
+
value: 0.6680
|
| 117 |
+
name: main_score (ndcg_at_10 or map)
|
| 118 |
- task:
|
| 119 |
type: retrieval
|
| 120 |
+
name: CodeSearchNetCCRetrieval
|
| 121 |
+
dataset:
|
| 122 |
+
name: CodeSearchNetCCRetrieval
|
| 123 |
+
type: mteb/CodeSearchNetCCRetrieval
|
| 124 |
+
split: test
|
| 125 |
+
metrics:
|
| 126 |
+
- type: main_score
|
| 127 |
+
value: 0.5664
|
| 128 |
+
name: main_score (ndcg_at_10 or map)
|
| 129 |
+
- task:
|
| 130 |
+
type: retrieval
|
| 131 |
+
name: CodeSearchNetRetrieval
|
| 132 |
dataset:
|
| 133 |
name: CodeSearchNetRetrieval
|
| 134 |
+
type: mteb/CodeSearchNetRetrieval
|
| 135 |
split: test
|
| 136 |
metrics:
|
| 137 |
+
- type: main_score
|
| 138 |
value: 0.8574
|
| 139 |
+
name: main_score (ndcg_at_10 or map)
|
| 140 |
- task:
|
| 141 |
type: retrieval
|
| 142 |
+
name: CodeTransOceanContest
|
| 143 |
dataset:
|
| 144 |
+
name: CodeTransOceanContest
|
| 145 |
+
type: mteb/CodeTransOceanContest
|
| 146 |
split: test
|
| 147 |
metrics:
|
| 148 |
+
- type: main_score
|
| 149 |
+
value: 0.6814
|
| 150 |
+
name: main_score (ndcg_at_10 or map)
|
| 151 |
- task:
|
| 152 |
type: retrieval
|
| 153 |
+
name: CodeTransOceanDL
|
| 154 |
dataset:
|
| 155 |
+
name: CodeTransOceanDL
|
| 156 |
+
type: mteb/CodeTransOceanDL
|
| 157 |
split: test
|
| 158 |
metrics:
|
| 159 |
+
- type: main_score
|
| 160 |
+
value: 0.3533
|
| 161 |
+
name: main_score (ndcg_at_10 or map)
|
| 162 |
- task:
|
| 163 |
+
type: reranking
|
| 164 |
+
name: CorebC2CReranking
|
| 165 |
dataset:
|
| 166 |
+
name: CorebC2CReranking
|
| 167 |
+
type: mteb/CorebC2CReranking
|
| 168 |
split: test
|
| 169 |
metrics:
|
| 170 |
+
- type: main_score
|
| 171 |
+
value: 0.4205
|
| 172 |
+
name: main_score (ndcg_at_10 or map)
|
| 173 |
- task:
|
| 174 |
type: retrieval
|
| 175 |
+
name: CorebC2CRetrieval
|
| 176 |
dataset:
|
| 177 |
+
name: CorebC2CRetrieval
|
| 178 |
+
type: mteb/CorebC2CRetrieval
|
| 179 |
split: test
|
| 180 |
metrics:
|
| 181 |
+
- type: main_score
|
| 182 |
+
value: 0.2750
|
| 183 |
+
name: main_score (ndcg_at_10 or map)
|
| 184 |
- task:
|
| 185 |
+
type: reranking
|
| 186 |
+
name: CorebC2TReranking
|
| 187 |
dataset:
|
| 188 |
+
name: CorebC2TReranking
|
| 189 |
+
type: mteb/CorebC2TReranking
|
| 190 |
split: test
|
| 191 |
metrics:
|
| 192 |
+
- type: main_score
|
| 193 |
+
value: 0.7937
|
| 194 |
+
name: main_score (ndcg_at_10 or map)
|
| 195 |
- task:
|
| 196 |
type: retrieval
|
| 197 |
+
name: CorebC2TRetrieval
|
| 198 |
dataset:
|
| 199 |
name: CorebC2TRetrieval
|
| 200 |
+
type: mteb/CorebC2TRetrieval
|
| 201 |
split: test
|
| 202 |
metrics:
|
| 203 |
+
- type: main_score
|
| 204 |
value: 0.6073
|
| 205 |
+
name: main_score (ndcg_at_10 or map)
|
| 206 |
+
- task:
|
| 207 |
+
type: reranking
|
| 208 |
+
name: CorebT2CReranking
|
| 209 |
+
dataset:
|
| 210 |
+
name: CorebT2CReranking
|
| 211 |
+
type: mteb/CorebT2CReranking
|
| 212 |
+
split: test
|
| 213 |
+
metrics:
|
| 214 |
+
- type: main_score
|
| 215 |
+
value: 0.2539
|
| 216 |
+
name: main_score (ndcg_at_10 or map)
|
| 217 |
- task:
|
| 218 |
type: retrieval
|
| 219 |
+
name: CorebT2CRetrieval
|
| 220 |
dataset:
|
| 221 |
+
name: CorebT2CRetrieval
|
| 222 |
+
type: mteb/CorebT2CRetrieval
|
| 223 |
split: test
|
| 224 |
metrics:
|
| 225 |
+
- type: main_score
|
| 226 |
+
value: 0.2671
|
| 227 |
+
name: main_score (ndcg_at_10 or map)
|
| 228 |
- task:
|
| 229 |
type: retrieval
|
| 230 |
+
name: CosQA
|
| 231 |
dataset:
|
| 232 |
name: CosQA
|
| 233 |
+
type: mteb/CosQA
|
| 234 |
split: test
|
| 235 |
metrics:
|
| 236 |
+
- type: main_score
|
| 237 |
+
value: 0.2997
|
| 238 |
+
name: main_score (ndcg_at_10 or map)
|
| 239 |
- task:
|
| 240 |
type: retrieval
|
| 241 |
+
name: StackOverflowQA
|
| 242 |
dataset:
|
| 243 |
+
name: StackOverflowQA
|
| 244 |
+
type: mteb/StackOverflowQA
|
| 245 |
split: test
|
| 246 |
metrics:
|
| 247 |
+
- type: main_score
|
| 248 |
+
value: 0.7131
|
| 249 |
+
name: main_score (ndcg_at_10 or map)
|
| 250 |
+
- task:
|
| 251 |
+
type: retrieval
|
| 252 |
+
name: SyntheticText2SQL
|
| 253 |
+
dataset:
|
| 254 |
+
name: SyntheticText2SQL
|
| 255 |
+
type: mteb/SyntheticText2SQL
|
| 256 |
+
split: test
|
| 257 |
+
metrics:
|
| 258 |
+
- type: main_score
|
| 259 |
+
value: 0.4938
|
| 260 |
+
name: main_score (ndcg_at_10 or map)
|
| 261 |
---
|
| 262 |
|
| 263 |
+
# code-embed-v2
|
| 264 |
|
| 265 |
+
`divakantpandey/code-embed-v2` is a 129.5M-parameter code embedding model fine-tuned from [microsoft/unixcoder-base](https://huggingface.co/microsoft/unixcoder-base) on the CodeSearchNet train split (Python, JavaScript, Go, Java, Ruby, PHP). It maps natural-language queries and functions to 1536-dimensional unit vectors for code search.
|
|
|
|
|
|
|
| 266 |
|
| 267 |
[](https://huggingface.co/divakantpandey/code-embed-v2)
|
| 268 |
+
[](https://mteb-leaderboard.hf.space/models/divakantpandey/code-embed-v2)
|
| 269 |
+
[](https://github.com/embeddings-benchmark/results/pull/803)
|
| 270 |
+
[]()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
[](https://creativecommons.org/licenses/by-nc/4.0/)
|
| 272 |
|
| 273 |
+
> [!WARNING]
|
| 274 |
+
> **Corrections (2026-11-17).** Earlier versions of this card and the accompanying preprint draft contained claims that were wrong or not backed by a measurement:
|
| 275 |
+
>
|
| 276 |
+
> - **Context length is 1,024 tokens, not 8,192.** The model has 1,026 absolute position embeddings and was trained and evaluated at 1,024 tokens. The previous config advertised 8,192, which made any input over 1,024 tokens fail with `IndexError: index 1026 is out of bounds`. The config now truncates at 1,024.
|
| 277 |
+
> - **Not fully zero-shot.** The model was trained on CodeSearchNet, which is the source of `CodeSearchNetRetrieval`, `COIRCodeSearchNetRetrieval` and `CodeSearchNetCCRetrieval`. Scores on those three tasks are in-distribution.
|
| 278 |
+
> - **Matryoshka truncation quality and CPU latency were never measured.** The tables that reported them have been removed.
|
| 279 |
+
> - **Training used in-batch negatives only**, not BM25 hard negatives. Pooling is mean pooling, not CLS. The Matryoshka dimensions were 64, 128, 256, 384, 768 and 1536.
|
| 280 |
+
> - **Supported languages are the six CodeSearchNet languages.** TypeScript and SQL were never in the training data.
|
| 281 |
+
> - **Parameter count is 129.5M** (90.0M excluding the token embedding table), not 131M total / 92M active.
|
| 282 |
+
|
| 283 |
+
## Model details
|
| 284 |
+
|
| 285 |
+
| | |
|
| 286 |
+
|---|---|
|
| 287 |
+
| Base model | `microsoft/unixcoder-base` (RoBERTa architecture, 12 layers, hidden size 768) |
|
| 288 |
+
| Pooling | Mean over tokens |
|
| 289 |
+
| Head | Linear 768 → 1536, GELU, Linear 1536 → 1536, L2 normalisation |
|
| 290 |
+
| Output dimension | 1536 (trained with a Matryoshka loss at 64, 128, 256, 384, 768, 1536) |
|
| 291 |
+
| Maximum input | 1,024 tokens; longer inputs are truncated |
|
| 292 |
+
| Parameters | 129,471,744 total; 89,984,256 excluding the token embedding table |
|
| 293 |
+
| Training data | CodeSearchNet train split, 6 languages, 1,838,250 docstring–function pairs after filtering and deduplication |
|
| 294 |
+
| Formats | safetensors (Sentence Transformers), ONNX (`model_optimized.onnx`, 135 MB), GGUF Q8_0 (138 MB, see the note under Ollama) |
|
| 295 |
+
| License | CC-BY-NC-4.0 |
|
| 296 |
+
|
| 297 |
+
## Training
|
| 298 |
+
|
| 299 |
+
Three stages, each one epoch, effective batch 128 (32 × 4 gradient accumulation), bf16 autocast, maximum length 1,024 tokens. Config: [`configs/v2_multilingual_config.yaml`](https://github.com/divakantpandey/code-embed/blob/main/configs/v2_multilingual_config.yaml).
|
| 300 |
+
|
| 301 |
+
1. **SimCSE** on functions: two dropout views (p = 0.1) of the same function are positives, temperature 0.05, lr 3e-5.
|
| 302 |
+
2. **Multiple Negatives Ranking Loss** on docstring → function pairs with in-batch negatives only, scale 25, lr 2.7e-5.
|
| 303 |
+
3. **Matryoshka loss** wrapping the same ranking loss at dimensions 64, 128, 256, 384, 768, 1536 with weights 0.10, 0.10, 0.15, 0.15, 0.25, 0.25, lr 1e-5.
|
| 304 |
+
|
| 305 |
+
## Evaluation
|
| 306 |
+
|
| 307 |
+
Official MTEB results, `mteb` 2.26, model revision `d7759ca2`, merged in [embeddings-benchmark/results#803](https://github.com/embeddings-benchmark/results/pull/803). Evaluation used 1,024 tokens.
|
| 308 |
+
|
| 309 |
+
| Benchmark | Tasks | Mean main score |
|
| 310 |
+
|---|---|---|
|
| 311 |
+
| MTEB(Code, v1) | 12 | 0.5475 |
|
| 312 |
+
| CoIR | 10 | 0.5343 |
|
| 313 |
+
| CoREB(v1) | 6 | 0.4362 |
|
| 314 |
+
|
| 315 |
+
| Task | Metric | Score | Note |
|
| 316 |
+
|---|---|---|---|
|
| 317 |
+
| AppsRetrieval | nDCG@10 | 0.2017 | |
|
| 318 |
+
| CodeEditSearchRetrieval | nDCG@10 | 0.3703 | |
|
| 319 |
+
| CodeFeedbackMT | nDCG@10 | 0.6164 | |
|
| 320 |
+
| CodeFeedbackST | nDCG@10 | 0.6680 | |
|
| 321 |
+
| CodeSearchNetCCRetrieval | nDCG@10 | 0.5664 | in-distribution |
|
| 322 |
+
| CodeSearchNetRetrieval | nDCG@10 | 0.8574 | in-distribution |
|
| 323 |
+
| COIRCodeSearchNetRetrieval | nDCG@10 | 0.7488 | in-distribution |
|
| 324 |
+
| CodeTransOceanContest | nDCG@10 | 0.6814 | |
|
| 325 |
+
| CodeTransOceanDL | nDCG@10 | 0.3533 | |
|
| 326 |
+
| CosQA | nDCG@10 | 0.2997 | |
|
| 327 |
+
| StackOverflowQA | nDCG@10 | 0.7131 | |
|
| 328 |
+
| SyntheticText2SQL | nDCG@10 | 0.4938 | |
|
| 329 |
+
| CorebC2CReranking | MAP | 0.4205 | |
|
| 330 |
+
| CorebC2CRetrieval | nDCG@10 | 0.2750 | |
|
| 331 |
+
| CorebC2TReranking | MAP | 0.7937 | |
|
| 332 |
+
| CorebC2TRetrieval | nDCG@10 | 0.6073 | |
|
| 333 |
+
| CorebT2CReranking | MAP | 0.2539 | |
|
| 334 |
+
| CorebT2CRetrieval | nDCG@10 | 0.2671 | |
|
| 335 |
+
|
| 336 |
+
**Leaderboard position.** On 2026-10-24, filtered to models with at most 150M parameters, the MTEB leaderboard listed this model 3rd on CoREB(v1), 8th on CoIR and 9th on MTEB(Code, v1), and 3rd of 25 models overall on CorebC2CReranking. Ranks change as models are added; check the [live leaderboard](https://mteb-leaderboard.hf.space/models/divakantpandey/code-embed-v2).
|
| 337 |
+
|
| 338 |
+
**Zero-shot status.** The model's current mteb metadata declares only `CodeSearchNetRetrieval` as training data, which gives 91% zero-shot on MTEB(Code, v1). `COIRCodeSearchNetRetrieval` and `CodeSearchNetCCRetrieval` are also built from CodeSearchNet. Declaring all three gives 75% zero-shot on MTEB(Code, v1), 80% on CoIR and 100% on CoREB(v1).
|
| 339 |
+
|
| 340 |
+
## Limitations
|
| 341 |
+
|
| 342 |
+
- **No comparison against the base model.** UniXcoder-base was not evaluated with the same harness, so the gain from this fine-tune is not quantified.
|
| 343 |
+
- **Truncated dimensions are untested.** The model was trained with a Matryoshka loss, but retrieval quality at truncated dimensions has not been measured. Validate on your own data before relying on it.
|
| 344 |
+
- **Latency is not benchmarked.**
|
| 345 |
+
- **The GGUF file is not the same embedding.** It outputs the 768-dimensional backbone representation without the projection head, and it has not been evaluated. Do not mix GGUF vectors with Sentence Transformers vectors in one index.
|
| 346 |
+
- **Function-level training only.** Training pairs are single functions with their docstrings, at most 1,024 tokens. Whole files, other languages, and non-docstring queries are out of distribution.
|
| 347 |
+
|
| 348 |
+
## Usage
|
| 349 |
+
|
| 350 |
+
### Semantic code search with `sentence-transformers`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
```python
|
| 353 |
from sentence_transformers import SentenceTransformer
|
| 354 |
|
|
|
|
| 355 |
model = SentenceTransformer("divakantpandey/code-embed-v2")
|
| 356 |
|
| 357 |
queries = [
|
| 358 |
"binary search implementation with edge handling",
|
| 359 |
"http json get request with retry logic",
|
| 360 |
+
"worker pool concurrency pattern using channels",
|
| 361 |
]
|
| 362 |
|
| 363 |
code_candidates = [
|
|
|
|
| 397 |
}
|
| 398 |
wg.Wait()
|
| 399 |
close(results)
|
| 400 |
+
}""",
|
| 401 |
]
|
| 402 |
|
|
|
|
| 403 |
query_vecs = model.encode(queries, normalize_embeddings=True)
|
| 404 |
code_vecs = model.encode(code_candidates, normalize_embeddings=True)
|
| 405 |
+
similarity = query_vecs @ code_vecs.T
|
|
|
|
|
|
|
| 406 |
|
| 407 |
for i, q in enumerate(queries):
|
| 408 |
+
best = similarity[i].argmax()
|
| 409 |
+
print(f"{q!r} -> candidate {best} (score {similarity[i][best]:.4f})")
|
|
|
|
|
|
|
| 410 |
```
|
| 411 |
|
| 412 |
+
### Code-to-code similarity
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
```python
|
| 415 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 416 |
|
| 417 |
model = SentenceTransformer("divakantpandey/code-embed-v2")
|
| 418 |
|
|
|
|
| 419 |
reference_python = """
|
| 420 |
def quicksort(arr):
|
| 421 |
if len(arr) <= 1:
|
|
|
|
| 427 |
return quicksort(left) + middle + quicksort(right)
|
| 428 |
"""
|
| 429 |
|
|
|
|
| 430 |
candidates = [
|
|
|
|
| 431 |
"""func QuickSort(arr []int) []int {
|
| 432 |
if len(arr) <= 1 { return arr }
|
| 433 |
pivot := arr[len(arr)/2]
|
|
|
|
| 437 |
}
|
| 438 |
return append(append(QuickSort(left), middle...), QuickSort(right)...)
|
| 439 |
}""",
|
|
|
|
| 440 |
"""func BubbleSort(arr []int) []int {
|
| 441 |
n := len(arr)
|
| 442 |
for i := 0; i < n-1; i++ {
|
|
|
|
| 445 |
}
|
| 446 |
}
|
| 447 |
return arr
|
| 448 |
+
}""",
|
| 449 |
]
|
| 450 |
|
| 451 |
+
ref = model.encode(reference_python, normalize_embeddings=True)
|
| 452 |
+
cands = model.encode(candidates, normalize_embeddings=True)
|
| 453 |
+
print(cands @ ref)
|
|
|
|
|
|
|
|
|
|
| 454 |
```
|
| 455 |
|
| 456 |
+
The model was not trained on cross-language pairs. Code-to-code scores across languages come from the shared docstring-to-code objective; check them on your own data.
|
| 457 |
|
| 458 |
+
### Truncated embeddings
|
| 459 |
|
| 460 |
+
The model was trained with a Matryoshka loss at 64, 128, 256, 384, 768 and 1536 dimensions, so leading prefixes of the vector can be used as smaller embeddings. Quality at each size has not been measured.
|
| 461 |
|
| 462 |
```python
|
|
|
|
| 463 |
import numpy as np
|
| 464 |
+
from sentence_transformers import SentenceTransformer
|
| 465 |
|
| 466 |
model = SentenceTransformer("divakantpandey/code-embed-v2")
|
| 467 |
|
|
|
|
| 470 |
"import jwt, datetime\ndef make_token(uid): return jwt.encode({'sub': uid, 'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=1)}, 'secret')",
|
| 471 |
"import hashlib\ndef hash_password(pwd): return hashlib.sha256(pwd.encode()).hexdigest()",
|
| 472 |
"def connect_db(): return psycopg2.connect('dbname=app user=postgres')",
|
| 473 |
+
"import jwt\ndef decode_token(tok): return jwt.decode(tok, 'secret', algorithms=['HS256'])",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
]
|
| 475 |
|
| 476 |
+
# Stage 1: shortlist with 256-d prefixes.
|
| 477 |
+
q256 = model.encode(query, truncate_dim=256, normalize_embeddings=True)
|
| 478 |
+
c256 = model.encode(corpus, truncate_dim=256, normalize_embeddings=True)
|
| 479 |
+
shortlist = np.argsort(c256 @ q256)[::-1][:2]
|
| 480 |
|
| 481 |
+
# Stage 2: rerank the shortlist with full 1536-d vectors.
|
| 482 |
+
q = model.encode(query, normalize_embeddings=True)
|
| 483 |
+
c = model.encode([corpus[i] for i in shortlist], normalize_embeddings=True)
|
| 484 |
+
print(corpus[shortlist[np.argmax(c @ q)]])
|
| 485 |
```
|
| 486 |
|
| 487 |
+
### Qdrant
|
|
|
|
|
|
|
| 488 |
|
| 489 |
```python
|
| 490 |
from qdrant_client import QdrantClient
|
| 491 |
from qdrant_client.models import Distance, VectorParams, PointStruct
|
| 492 |
from sentence_transformers import SentenceTransformer
|
| 493 |
|
|
|
|
| 494 |
qdrant = QdrantClient(":memory:")
|
| 495 |
model = SentenceTransformer("divakantpandey/code-embed-v2")
|
| 496 |
|
|
|
|
| 497 |
qdrant.create_collection(
|
| 498 |
+
collection_name="codebase_symbols",
|
| 499 |
vectors_config=VectorParams(size=1536, distance=Distance.COSINE),
|
| 500 |
)
|
| 501 |
|
|
|
|
| 502 |
files = [
|
| 503 |
+
{"id": 1, "path": "auth/session.go", "code": "func VerifySession(token string) bool { return redis.Exists(token) }"},
|
| 504 |
+
{"id": 2, "path": "models/user.py", "code": "class User(BaseModel): id: int; name: str; active: bool"},
|
| 505 |
]
|
| 506 |
+
qdrant.upsert(
|
| 507 |
+
collection_name="codebase_symbols",
|
| 508 |
+
points=[
|
| 509 |
+
PointStruct(id=f["id"], vector=model.encode(f["code"], normalize_embeddings=True).tolist(), payload=f)
|
| 510 |
+
for f in files
|
| 511 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 512 |
)
|
| 513 |
|
| 514 |
+
hits = qdrant.query_points(
|
| 515 |
+
collection_name="codebase_symbols",
|
| 516 |
+
query=model.encode("validate active user session", normalize_embeddings=True).tolist(),
|
| 517 |
+
limit=1,
|
| 518 |
+
)
|
| 519 |
+
for hit in hits.points:
|
| 520 |
+
print(hit.payload["path"], round(hit.score, 4))
|
| 521 |
```
|
| 522 |
|
| 523 |
+
### LangChain
|
|
|
|
|
|
|
| 524 |
|
| 525 |
```python
|
| 526 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 527 |
from langchain_community.vectorstores import Chroma
|
| 528 |
from langchain_core.documents import Document
|
| 529 |
|
|
|
|
| 530 |
embeddings = HuggingFaceEmbeddings(
|
| 531 |
model_name="divakantpandey/code-embed-v2",
|
| 532 |
+
encode_kwargs={"normalize_embeddings": True},
|
| 533 |
)
|
| 534 |
|
|
|
|
| 535 |
documents = [
|
| 536 |
Document(page_content="func RateLimiter(ip string) bool { return limiter.Allow(ip) }", metadata={"file": "security/ratelimit.go"}),
|
| 537 |
+
Document(page_content="def send_metric(name, val): statsd.increment(name, val)", metadata={"file": "telemetry/metrics.py"}),
|
| 538 |
]
|
|
|
|
| 539 |
vector_db = Chroma.from_documents(documents, embeddings)
|
| 540 |
|
| 541 |
+
for doc in vector_db.as_retriever(search_kwargs={"k": 1}).invoke("rate limit requests per client IP"):
|
| 542 |
+
print(doc.metadata["file"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
```
|
| 544 |
|
| 545 |
+
### Ollama and llama.cpp (GGUF)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
|
| 547 |
> [!NOTE]
|
| 548 |
+
> The GGUF file contains the transformer only. It returns the 768-dimensional pooled backbone output, without the 1536-d projection head, so its vectors are not the same as the Sentence Transformers output and have not been evaluated. Keep the context at 1,024 tokens.
|
| 549 |
|
| 550 |
```bash
|
|
|
|
| 551 |
ollama pull hf.co/divakantpandey/code-embed-v2:code-embed-v2-q8_0.gguf
|
| 552 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
curl http://localhost:11434/api/embed -d '{
|
| 554 |
+
"model": "hf.co/divakantpandey/code-embed-v2:code-embed-v2-q8_0.gguf",
|
| 555 |
"input": "def binary_search(arr, target):"
|
| 556 |
}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 557 |
```
|
| 558 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
```bash
|
|
|
|
| 560 |
wget https://huggingface.co/divakantpandey/code-embed-v2/resolve/main/code-embed-v2-q8_0.gguf
|
| 561 |
+
llama-server -m code-embed-v2-q8_0.gguf --embedding -c 1024 --port 8080
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
```
|
| 563 |
|
| 564 |
+
### Text Embeddings Inference
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
|
| 566 |
```bash
|
|
|
|
| 567 |
docker run --gpus all -p 8080:80 \
|
| 568 |
ghcr.io/huggingface/text-embeddings-inference:1.5 \
|
| 569 |
--model-id divakantpandey/code-embed-v2
|
| 570 |
```
|
| 571 |
|
| 572 |
+
Check that the returned vectors are 1536-dimensional. A 768-dimensional result means the projection head was not applied.
|
| 573 |
|
| 574 |
+
## License and citation
|
| 575 |
+
|
| 576 |
+
Code and weights are released under [CC-BY-NC-4.0](https://creativecommons.org/licenses/by-nc/4.0/). For commercial use, contact Divakant Pandey (`divakant01@gmail.com`, [@divakantpandey](https://github.com/divakantpandey)).
|
| 577 |
+
|
| 578 |
+
```bibtex
|
| 579 |
+
@misc{pandey2026codeembedv2,
|
| 580 |
+
author = {Divakant Pandey},
|
| 581 |
+
title = {code-embed-v2: A UniXcoder-Based Code Embedding Model Fine-Tuned on CodeSearchNet},
|
| 582 |
+
year = {2026},
|
| 583 |
+
howpublished = {\url{https://huggingface.co/divakantpandey/code-embed-v2}}
|
| 584 |
+
}
|
| 585 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
code_embed_config.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
"backbone_hidden_size": 768,
|
| 4 |
"embedding_dim": 1536,
|
| 5 |
"projection_hidden_dim": 1536,
|
| 6 |
-
"pooling": "
|
| 7 |
-
"max_seq_length":
|
| 8 |
"dropout": 0.1
|
| 9 |
-
}
|
|
|
|
| 3 |
"backbone_hidden_size": 768,
|
| 4 |
"embedding_dim": 1536,
|
| 5 |
"projection_hidden_dim": 1536,
|
| 6 |
+
"pooling": "mean",
|
| 7 |
+
"max_seq_length": 1024,
|
| 8 |
"dropout": 0.1
|
| 9 |
+
}
|
sentence_bert_config.json
CHANGED
|
@@ -7,5 +7,5 @@
|
|
| 7 |
}
|
| 8 |
},
|
| 9 |
"module_output_name": "token_embeddings",
|
| 10 |
-
"max_seq_length":
|
| 11 |
-
}
|
|
|
|
| 7 |
}
|
| 8 |
},
|
| 9 |
"module_output_name": "token_embeddings",
|
| 10 |
+
"max_seq_length": 1024
|
| 11 |
+
}
|
tokenizer_config.json
CHANGED
|
@@ -8,8 +8,8 @@
|
|
| 8 |
"is_local": true,
|
| 9 |
"local_files_only": false,
|
| 10 |
"mask_token": "<mask>",
|
| 11 |
-
"max_length":
|
| 12 |
-
"model_max_length":
|
| 13 |
"pad_to_multiple_of": null,
|
| 14 |
"pad_token": "<pad>",
|
| 15 |
"pad_token_type_id": 0,
|
|
@@ -21,4 +21,4 @@
|
|
| 21 |
"truncation_side": "right",
|
| 22 |
"truncation_strategy": "longest_first",
|
| 23 |
"unk_token": "<unk>"
|
| 24 |
-
}
|
|
|
|
| 8 |
"is_local": true,
|
| 9 |
"local_files_only": false,
|
| 10 |
"mask_token": "<mask>",
|
| 11 |
+
"max_length": 1024,
|
| 12 |
+
"model_max_length": 1024,
|
| 13 |
"pad_to_multiple_of": null,
|
| 14 |
"pad_token": "<pad>",
|
| 15 |
"pad_token_type_id": 0,
|
|
|
|
| 21 |
"truncation_side": "right",
|
| 22 |
"truncation_strategy": "longest_first",
|
| 23 |
"unk_token": "<unk>"
|
| 24 |
+
}
|