Semantic code search for developers — search codebases by meaning, not exact text.
Instead of relying on grep or Ctrl+F, CodeLens understands the intent behind your query by indexing source code at the function/class level using AST-aware chunking and semantic embeddings.
"Find the function that refreshes authentication tokens."
"Where is request rate limiting implemented?"
"Show me the code responsible for parsing configuration files."
CodeLens is a semantic search engine for source code built around modern information retrieval techniques.
Each function or class is extracted using tree-sitter, converted into a vector embedding using sentence-transformers, stored in a FAISS vector index, and associated metadata is persisted in Supabase (Postgres).
During search, CodeLens performs:
- Semantic retrieval using a bi-encoder
- Cross-encoder reranking
- Metadata lookup to return the corresponding source code
This architecture provides significantly better results than keyword search while remaining fast enough for interactive use.
- Semantic search using natural language queries
- AST-aware chunking with tree-sitter
- Two-stage retrieval
- Bi-encoder retrieval with FAISS
- Cross-encoder reranking
- CLI and REST API
- Index local repositories or public GitHub repositories
- Supabase-backed metadata storage
- Docker support
- Benchmarking utilities
FAISS is responsible solely for vector similarity search.
It performs extremely fast in-memory nearest-neighbor search and easily scales to hundreds of thousands of code chunks while maintaining sub-millisecond retrieval latency.
Supabase (Postgres) stores all relational metadata, including:
- Repository information
- File paths
- Line numbers
- Source code
- Repository ownership
Separating vectors from metadata keeps the architecture simple, scalable, and maintainable.
CodeLens follows the same retrieve-then-rerank architecture used in many production search systems.
- Embed every code chunk once
- Embed the user query
- Retrieve the nearest neighbors using FAISS
- Extremely fast
- Produces a shortlist of candidates
Each candidate is evaluated jointly with the query.
Although slower than the bi-encoder, the cross-encoder significantly improves ranking quality by considering the interaction between the query and the candidate code.
- Python
- FastAPI
- Typer
- tree-sitter
- sentence-transformers
- Cross-Encoder
- FAISS
- Supabase (Postgres)
- Docker
git clone <repository-url>
cd codelens
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install -e .Create a .env file.
cp .env.example .envAdd your connection string.
DATABASE_URL=postgresql://...pytest -v -m "not network"codelens index ./my-projectcodelens index https://github.com/pallets/flaskcodelens search \
"parse a JSON config file" \
--repo-id 1 \
--k 5Start the server.
uvicorn codelens.api:app --reloadcurl -X POST http://localhost:8000/index \
-H "Content-Type: application/json" \
-d '{
"path_or_url":"https://github.com/pallets/flask"
}'curl -X POST http://localhost:8000/search \
-H "Content-Type: application/json" \
-d '{
"query":"render a template",
"repo_id":1,
"k":5
}'Interactive API documentation is available at:
http://localhost:8000/docs
Build the image.
docker compose buildStart the services.
docker compose up -dHealth check.
curl http://localhost:8000/healthDATABASE_URL is automatically read from your .env file.
Repository used:
- Flask
- ~1,637 indexed code chunks
- CPU-only machine
| Metric | Value |
|---|---|
| Chunks Indexed | 1,637 |
| Total Index Time | 313 s |
| Metric | Latency |
|---|---|
| p50 | 291 ms |
| p95 | 584 ms |
| p99 | 683 ms |
| Stage | Latency |
|---|---|
| Query Embedding | 31.1 ms |
| FAISS Search | 0.2 ms |
| Metadata Lookup | 262.9 ms |
| Cross-Encoder | 320.0 ms |
| Total | 614.2 ms |
The semantic retrieval step itself (FAISS) is not the bottleneck. The majority of end-to-end latency comes from:
- Cross-encoder inference
- Network round-trip to Supabase
This is expected in retrieve-then-rerank systems and is the tradeoff for improved ranking quality.
Future optimization opportunities include:
- GPU inference
- Smaller reranking models
- Regional database colocation
- Connection pooling
python scripts/benchmark.py \
--repo https://github.com/pallets/flask \
--queries 30codelens/
├── codelens/
│ ├── ingest.py
│ ├── chunker.py
│ ├── embedder.py
│ ├── vector_index.py
│ ├── db.py
│ ├── reranker.py
│ ├── pipeline.py
│ ├── api.py
│ └── cli.py
│
├── tests/
├── scripts/
│ └── benchmark.py
│
├── docs/
│ └── DESIGN.md
│
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
└── README.md
- IVF-based FAISS indexes for larger repositories
- Incremental re-indexing
- Background indexing jobs
- Additional language grammars (Rust, Java, C++, Go)
- GPU inference for embedding and reranking
- Batch embedding during indexing
- Metadata caching
- Reduced database latency through regional deployment