A production-ready semantic code search system that automatically indexes high-quality Python code snippets from popular open-source repositories. Search through thousands of verified code examples from Django, Flask, FastAPI, Requests, and CPython's standard library - all from your terminal.
- Smart Search: Find Python code snippets using natural language queries
- Quality Verified: Every snippet is validated using heuristic analysis for code quality (7.0+ score)
- Auto-Updated: GitHub Actions runs nightly to keep the database fresh with latest code
- Zero Setup: Pure bash CLI with only
curlandjqdependencies - Curated Sources: 8,000+ snippets from production-grade repositories
- Lightning Fast: Local caching means instant searches after first download
- Context-Rich: See full function code, docstrings, source URLs, and quality scores
# Download the script
curl -o code-snatch https://raw.githubusercontent.com/manusiele/semantic-code-index/main/code-snatch
chmod +x code-snatch
# Move to PATH
sudo mv code-snatch /usr/local/bin/
# Or for local install:
mkdir -p ~/.local/bin
mv code-snatch ~/.local/bin/
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrcUbuntu/Debian:
sudo apt update
sudo apt install curl jqmacOS:
brew install curl jq# First time - download the database
code-snatch --update
# Search for code
code-snatch "async file download"
code-snatch "flask route with error handling"
code-snatch "decorator for timing"
# Check cache info
code-snatch --info
# Get help
code-snatch --helpThe system operates in two distinct phases: automated indexing in the cloud and local searching on your machine.
┌─────────────────────────────────────────────────────────┐
│ GitHub Actions (Runs Daily at 2 AM UTC) │
├─────────────────────────────────────────────────────────┤
│ 1. Clone configured repositories │
│ 2. Extract Python functions with docstrings │
│ 3. Generate semantic embeddings (sentence-transformers)│
│ 4. Apply heuristic quality verification │
│ • Docstring completeness │
│ • Code complexity analysis │
│ • Best practices compliance │
│ 5. Filter snippets (quality score ≥ 7.0) │
│ 6. Commit snippets_db.json (96MB) to repository │
└─────────────────────────────────────────────────────────┘
Indexing Pipeline Components:
indexer_git.py: Clones repos and extracts functions using AST parsingembedder.py: Generates vector embeddings usingall-MiniLM-L6-v2modelverifier_simple.py: Validates code quality with heuristic scoring- Result: ~8,000+ verified, production-ready code snippets
┌─────────────────────────────────────────────────────────┐
│ Bash CLI (code-snatch) │
├─────────────────────────────────────────────────────────┤
│ 1. Download snippets_db.json on first run │
│ 2. Cache locally (~/.semantic-code-cli) │
│ 3. Search using jq text matching │
│ 4. Display formatted results with: │
│ • Function name and source │
│ • Full code with syntax context │
│ • Docstring description │
│ • GitHub source URL │
│ • Quality verification badge │
│ 5. Auto-refresh cache every 24 hours │
└─────────────────────────────────────────────────────────┘
Search Method: Currently uses keyword matching on function names and docstrings. The embeddings are stored for future semantic search capabilities but not yet utilized by the bash CLI (would require Python for vector similarity calculations).
The system is highly configurable through config/repos.json:
{
"repos": [
{
"name": "django/django",
"enabled": true,
"description": "The Web framework for perfectionists with deadlines"
},
{
"name": "psf/requests",
"enabled": true,
"description": "HTTP library for Python"
}
],
"indexing_settings": {
"file_extensions": [".py"],
"max_file_size_kb": 500,
"min_function_lines": 3,
"exclude_patterns": [
"test_", "_test.py", "tests/",
"locale/", "docs/", "examples/"
]
}
}Configuration Options:
repos: List of GitHub repositories to indexname: Repository in formatowner/repoenabled: Toggle indexing for this repodescription: Human-readable description
indexing_settings:file_extensions: File types to process (currently Python only)max_file_size_kb: Skip files larger than thismin_function_lines: Minimum function length to indexexclude_patterns: Paths/files to skip (tests, docs, etc.)
# 1. Install Python dependencies
cd scripts
pip install -r requirements.txt
# 2. Set GitHub token (for higher API rate limits)
export GITHUB_TOKEN="your_github_token"
# 3. Run indexing pipeline
python indexer_git.py # Clones repos and extracts functions
python embedder.py # Generates embeddings
python verifier_simple.py # Validates quality
# 4. Test the CLI
cd ..
./code-snatch "decorator"For more sophisticated quality verification (not used in production due to CI time constraints):
# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
# Start Ollama server
ollama serve &
# Pull the code model
ollama pull qwen2.5-coder:1.5b
# Run LLM verification
python scripts/verifier_ollama.pysemantic-code-index/
├── .github/workflows/
│ └── index-repos.yml # CI/CD: Daily indexing automation
├── config/
│ └── repos.json # Repository and indexing configuration
├── scripts/
│ ├── indexer.py # GitHub API-based indexer (alternative)
│ ├── indexer_git.py # Git clone-based indexer (production)
│ ├── embedder.py # Sentence transformer embeddings
│ ├── verifier_simple.py # Heuristic quality verification (production)
│ ├── verifier_ollama.py # LLM-based verification (optional)
│ ├── verifier_groq.py # Groq API verification (alternative)
│ ├── debug_pipeline.py # Pipeline debugging utility
│ ├── test_indexer.py # Unit tests
│ └── requirements.txt # Python dependencies
├── data/
│ ├── .gitkeep
│ └── snippets_db.json # Generated database (96MB, 8000+ snippets)
└── code-snatch # Bash CLI tool
Each snippet in snippets_db.json contains:
{
"function_name": "user_passes_test",
"code": "def user_passes_test(...):\n ...",
"docstring": "Decorator for views that checks...",
"filepath": "django/contrib/auth/decorators.py",
"source_repo": "django/django",
"source_url": "https://github.com/django/django/blob/main/...",
"type": "function",
"embedding": [0.123, -0.456, ...],
"indexed_date": "2024-02-17T09:40:00",
"heuristic_verified": true,
"quality_score": 7.5,
"verification_method": "heuristic"
}- Fork this repository
- Edit
config/repos.jsonand add your repo:{ "name": "owner/repo-name", "enabled": true, "description": "Brief description" } - Commit and push - GitHub Actions will index it automatically
- Or run locally:
python scripts/indexer_git.py
The heuristic verifier (verifier_simple.py) scores snippets based on:
-
Docstring Quality (40%)
- Length and completeness
- Presence of parameter descriptions
- Return value documentation
-
Code Quality (30%)
- Function length (not too short/long)
- Complexity metrics
- Naming conventions
-
Best Practices (30%)
- Type hints presence
- Error handling patterns
- Documentation standards
Snippets scoring ≥7.0/10 are included in the database.
While the project supports LLM verification via Ollama, production uses heuristic verification because:
- Speed: Processes 8,000+ snippets in minutes vs hours
- Cost: Zero API costs, no compute overhead
- Reliability: Deterministic results, no model dependencies
- CI-Friendly: Runs in GitHub Actions without special setup
LLM verification remains available for local use when deeper quality analysis is needed.
| Repository | Description | Snippets |
|---|---|---|
| django/django | Web framework | ~3,500 |
| psf/requests | HTTP library | ~800 |
| pallets/flask | Micro framework | ~600 |
| fastapi/fastapi | Async framework | ~900 |
| python/cpython | Standard library | ~2,400 |
Total: ~8,200 verified snippets
Current Limitations:
- Keyword-based search only (embeddings not utilized in CLI)
- Python-only (no multi-language support)
- Text matching can miss semantically similar queries
- 96MB database download on first use
Planned Improvements:
- Python CLI with true semantic search using embeddings
- Web interface for easier browsing
- Support for more languages (JavaScript, Go, Rust)
- Incremental database updates
- User-contributed snippet ratings
- Code snippet categories/tags
Contributions are welcome! Here's how you can help:
- Add Repositories: Submit PRs with new repos in
config/repos.json - Improve Verification: Enhance heuristic scoring algorithms
- Build Features: Implement semantic search, web UI, or multi-language support
- Report Issues: Found a bug or have a suggestion? Open an issue
- Documentation: Improve guides and examples
# Fork and clone
git clone https://github.com/manusiele/semantic-code-index.git
cd semantic-code-index
# Create feature branch
git checkout -b feature/your-feature
# Make changes and test
./code-snatch "test query"
# Commit and push
git add .
git commit -m "Add: your feature description"
git push origin feature/your-feature
# Open pull request on GitHubMIT License - see LICENSE file for details
manusiele
manusiele254@gmail.com
GitHub
Star ⭐ this repo if you find it useful!