Skip to content

Latest commit

Β 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” Semantic Code Search Engine

A Test Assignment for JetBrains Internship

This project implements an embeddings-based code search engine, evaluates its performance on the CoSQA dataset using standard retrieval metrics (Recall@10, MRR@10, nDCG@10), and demonstrates improvement through fine-tuning on code-query pairs.

πŸ“ Project Structure

β”œβ”€β”€ config.py                   # Project configuration: paths, models, and hyperparameters  
β”œβ”€β”€ src/                              
β”‚   β”œβ”€β”€ search_engine.py        # SearchEngine class    
β”‚   β”œβ”€β”€ data_loader.py          # Loads and preprocesses CoSQA dataset    
β”‚   β”œβ”€β”€ evaluation.py           # Metrics and model evaluation    
β”‚   β”œβ”€β”€ train.py                # Training logic     
β”‚   └── bonus_experiments.py    # Bonus analyses (function names vs. bodies)    
β”‚    
β”œβ”€β”€ scripts/                        
β”‚   β”œβ”€β”€ index_and_search_demo.py    # Demo on code snippets    
β”‚   β”œβ”€β”€ evaluate_baseline.py        # Evaluate base model on CoSQ    
β”‚   β”œβ”€β”€ fine_tune_and_evaluate.py   # Fine-tune + compare metrics    
β”‚   └── run_bonus_experiments.py    # Run bonus analyses    
β”‚    
β”œβ”€β”€ report.ipynb                # Complete implementation with results, plots, and analysis    
β”œβ”€β”€ requirements.txt            # Dependencies    
└── README.md                   # This file: setup, structure, and usage instructions  

πŸš€ Quick Start

1. Install dependencies

pip install -r requirements.txt

2. Run the semantic search demo

python scripts/index_and_search_demo.py

3. Evaluate the baseline model on CoSQA

python scripts/evaluate_baseline.py --k 10

4. Fine-tune the model and compare metrics

python scripts/fine_tune_and_evaluate.py --epochs 3

5. Run bonus experiments

python scripts/run_bonus_experiments.py

πŸ“Š Evaluation Metrics

The system is evaluated using standard information retrieval metrics on the CoSQA test set:

Recall@10: Proportion of queries with at least one relevant result in the top 10.
MRR@10 (Mean Reciprocal Rank): Average of the reciprocal rank of the first relevant result (capped at 10).
nDCG@10 (Normalized Discounted Cumulative Gain): Measures ranking quality considering graded relevance.

πŸ”§ Fine-Tuning

I fine-tune the base embedding model on the CoSQA training set to specialize it for code search.

Loss Function: MultipleNegativesRankingLoss β€” chosen because it efficiently leverages in-batch negative sampling: for each natural language query, all non-matching code snippets in the same batch are treated as negatives. This directly optimizes the model to rank the correct code snippet higher than irrelevant ones, which aligns perfectly with semantic code search objectives. The loss is widely used in state-of-the-art retrieval systems and requires no manual negative creation.
Training Progress: Training loss vs. step is logged and plotted in the notebook.
Key Result: Fine-tuning consistently improves all three metrics (see report).

🌟 Bonus Experiments

The notebook also includes additional studies:

1. Function Names vs. Full Bodies:
Using only function names (instead of full bodies) as documents reduces performance, confirming that semantic context in code bodies is critical.

πŸ““ Report Notebook

Open report.ipynb to see:

  • End-to-end demo of indexing and querying
  • Baseline model performace
  • Fine-tuning results
  • Training loss curve
  • Bonus experiment findings
  • Summary

πŸ› οΈ Technologies Used

  • Embedding Model: BAAI/bge-base-en-v1.5 β€” a strong general-purpose encoder for semantic retrieval
  • Embedding Framework: sentence-transformers
  • Vector Index: FAISS β€” efficient similarity search over dense vectors
  • Loss: MultipleNegativesRankingLoss for contrastive fine-tuning
  • Data: CoSQA dataset β€” query–code pairs for code search evaluation

βœ… What’s Implemented

Part Status
Part 1: Search API βœ… Complete
Part 2: Evaluation βœ… All metrics
Part 3: Fine-tuning βœ… With results
Bonus βœ… Bonus 1 explored

πŸ™ Acknowledgments

This project builds on the following open-source resources:

About

πŸ” Semantic search system that retrieves relevant code snippets from natural language queries using embedding-based retrieval.

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages