--- language: - en - code library_name: sentence-transformers pipeline_tag: sentence-similarity tags: - sentence-transformers - feature-extraction - code-search - code-retrieval - onnx - bert - e5 - lora - rag datasets: - code_search_net license: apache-2.0 base_model: intfloat/e5-base-v2 model-index: - name: e5-base-v2-code-search results: - task: type: Retrieval dataset: type: code_search_net name: CodeSearchNet metrics: - type: ndcg_at_10 value: 0.683 name: NDCG@10 (avg, 6 languages) - task: type: Retrieval dataset: type: cosqa name: CosQA metrics: - type: ndcg_at_10 value: 0.348 name: NDCG@10 --- # e5-base-v2-code-search A fine-tuned code search embedding model based on [intfloat/e5-base-v2](https://huggingface.co/intfloat/e5-base-v2) (110M parameters). Optimized for natural language → code retrieval across 6 programming languages. Built for [cqs](https://github.com/jamie8johnson/cqs), a semantic code search tool for AI agents. ## Key Results | Benchmark | Metric | Score | vs Base E5 | |-----------|--------|-------|------------| | CodeSearchNet (6 lang avg) | NDCG@10 | **0.683** | **+5.6pp** | | CosQA (out-of-distribution) | NDCG@10 | **0.348** | **+1.9pp** | | Hard eval (55 confusable pairs) | Recall@1 | **92.7%** | +1.8pp* | *Hard eval improvement from full pipeline (LoRA + NL enrichment + doc comments), not LoRA alone. ### Per-Language NDCG@10 (CodeSearchNet) | Language | Base E5 | This Model | Delta | |----------|---------|------------|-------| | Go | 0.624 | **0.746** | +0.122 | | Java | 0.571 | **0.621** | +0.050 | | JavaScript | 0.487 | **0.535** | +0.048 | | Ruby | 0.526 | **0.592** | +0.066 | | Python | 0.888 | **0.909** | +0.021 | | PHP | 0.601 | **0.623** | +0.022 | ### CoIR Leaderboard Context Among 110M-class models on the [CoIR benchmark](https://archersama.github.io/coir/) (ACL 2025), E5-base-v2 is already #7 out of 13 entries. This LoRA fine-tune extends the lead over other 110M models (BGE-Base 42.77, GTE-Base 36.75, UniXcoder 37.33) while remaining CPU-runnable in <100ms per query. ## Training - **Method:** LoRA (rank 16) fine-tuning of E5-base-v2 - **Data:** 166,000 CodeSearchNet triplets (query, positive code, negative code) + docstring-as-query pairs - **Training time:** ~30 minutes on A6000 - **Epochs:** 1 - **Loss:** Triplet loss with in-batch negatives ### Why LoRA v5? We trained 10 LoRA variants with different data sizes (10k–186k), ranks (16, 32), epochs (1–3), and data mixes. v5 (166k/1ep) is the best overall: | Config | Samples | Epochs | CSN NDCG@10 | CosQA (transfer) | |--------|---------|--------|-------------|-------------------| | Base E5 | — | — | 0.627 | 0.329 | | v3 (previous) | 50k | 1 | 0.671 | 0.334 | | **v5 (current)** | **166k** | **1** | **0.683** | **0.348** | | v4 | 166k | 3 | 0.695 | 0.305 (over-specialized) | | rank-32 | 186k | 1 | 0.681 | 0.350 | v4 achieves higher CSN but catastrophically regresses on out-of-distribution queries (CosQA drops below base). v5 uses the full dataset at 1 epoch — more data without over-specialization. ## Usage ### With sentence-transformers ```python from sentence_transformers import SentenceTransformer model = SentenceTransformer("jamie8johnson/e5-base-v2-code-search") queries = ["query: find all files matching a glob pattern"] passages = ["passage: def glob_match(pattern, path): ..."] q_emb = model.encode(queries, normalize_embeddings=True) p_emb = model.encode(passages, normalize_embeddings=True) similarity = q_emb @ p_emb.T ``` ### With cqs (semantic code search CLI) ```bash # Uses this model by default cqs "find files matching glob" --json # Override with base E5 CQS_EMBEDDING_MODEL=intfloat/e5-base-v2 cqs "find files matching glob" ``` ### With ONNX Runtime ```python import onnxruntime as ort from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("jamie8johnson/e5-base-v2-code-search") session = ort.InferenceSession("model.onnx") inputs = tokenizer("query: sort array by key", return_tensors="np", padding=True, truncation=True) outputs = session.run(None, dict(inputs)) embeddings = outputs[0][:, 0, :] # CLS token ``` ## Enrichment Pipeline This model achieves its best results when combined with cqs's NL enrichment pipeline, which prepends structured metadata to code before embedding: 1. **Type-aware signatures** (free) — append `fn foo(x: i32) -> Result<()>` to NL description 2. **Call graph context** (free) — append caller/callee function names 3. **LLM discriminating summaries** (optional, ~$0.15/3k functions) — "what makes this function unique" 4. **Doc comment generation** (optional, ~$1.50/3k functions) — structured parameter/return docs The enrichment transforms code into richer text before the embedding model sees it. This is why a 110M model can compete with specialized models 3-20x larger — the intelligence is in the text transformation, not the model size. ## Limitations - Trained primarily on CodeSearchNet (Go, Java, JavaScript, Ruby, Python, PHP). Performance on other languages (Rust, C++, TypeScript) relies on E5's base multilingual capability. - English queries only. E5-base-v2's multilingual capability is untested for code search. - 512 token max sequence length. Long functions are truncated. ## Citation ```bibtex @software{cqs2026, title={cqs: Semantic Code Search with Local Embeddings}, author={Johnson, Jamie}, url={https://github.com/jamie8johnson/cqs}, year={2026} } ``` ## License Apache 2.0 (same as base E5-base-v2).