sqry-nl
Natural language to sqry query translation layer.
Overview
sqry-nl translates natural language queries like "find authentication functions in rust" into executable sqry commands like sqry query "name~=/auth/ AND kind:function" --language rust.
The crate provides a complete translation pipeline:
Input: "find authentication functions"
↓
Preprocess: Unicode normalization, homoglyph detection
↓
Extract: symbols=["authentication"], kind=function
↓
Classify: Intent::SymbolQuery (0.85 confidence)
↓
Assemble: sqry query "name~=/authentication/ AND kind:function"
↓
Validate: whitelist check, safety validation
↓
Output: TranslationResponse::Execute { command, confidence, ... }
Features
- 6-stage translation pipeline: preprocess → extract → classify → assemble → validate → cache
- 4-tier response system: Execute (≥85%) / Confirm (≥65%) / Disambiguate (<65%) / Reject
- Strong safety guarantees: Whitelist-only commands, metachar rejection, path traversal protection
- LRU caching: Context-aware cache keys for repeated queries
- Compact classifier: all-MiniLM-L6-v2 (22M params, 57MB INT8) with 99.75% accuracy
- Feature-gated classifier: Works without ONNX via rule-based fallback
Installation
Add to your Cargo.toml:
[]
= "0.1"
Feature Flags
| Feature | Default | Description |
|---|---|---|
classifier |
Yes | Enable MiniLM-L6-v2 ONNX classifier (requires ONNX Runtime) |
To use without ONNX (rule-based fallback only):
[]
= { = "0.1", = false }
Quick Start
use ;
Supported Intents
| Intent | Example Query | Generated Command |
|---|---|---|
SymbolQuery |
"find login function" | sqry query "login" --kind function |
TextSearch |
"grep for TODO" | sqry search "TODO" |
FindCallers |
"who calls authenticate" | sqry graph direct-callers "authenticate" |
FindCallees |
"what does main call" | sqry graph direct-callees "main" |
TracePath |
"trace from main to db" | sqry graph trace-path "main" "db" |
Visualize |
"draw call graph" | sqry visualize --format mermaid |
IndexStatus |
"index status" | sqry index --status |
Ambiguous |
"help" | (disambiguation options) |
Configuration
use ;
let config = TranslatorConfig ;
Safety
All generated commands are validated against strict safety rules:
| Check | Description |
|---|---|
| Whitelist | Commands must match allowed templates |
| Metacharacters | Rejects ;, ` |
| Environment variables | Rejects $HOME, ${VAR}, etc. |
| Path traversal | Rejects .., absolute paths |
| Write operations | Rejects --force, repair, prune |
| Length limit | Commands capped at 4KB |
Architecture
sqry-nl/
├── src/
│ ├── lib.rs # Public API
│ ├── types.rs # Core types (Intent, TranslationResponse, etc.)
│ ├── translator.rs # Main Translator API
│ ├── preprocess/ # Unicode normalization, homoglyph detection
│ ├── extractor/ # Entity extraction (symbols, languages, etc.)
│ ├── classifier/ # Intent classification (ONNX + fallback)
│ ├── assembler/ # Template-based command generation
│ ├── validator/ # Safety validation
│ ├── cache/ # LRU translation cache
│ └── error.rs # Error types
├── training/ # Python training pipeline
│ ├── generate_data.py # Training data generation
│ ├── train_classifier.py
│ ├── export_onnx.py
│ └── calibrate.py
├── benches/
│ └── eval_harness.rs # Accuracy & latency benchmarks
└── tests/
└── golden_queries.toml # 120+ test queries
API Reference
Types
// Intent classification result
// Translation response with confidence tiers
// Extracted entities from natural language
Translator
Model
The intent classifier uses all-MiniLM-L6-v2 (22M params) fine-tuned for 8 intent classes, exported to ONNX with INT8 dynamic quantization.
| Metric | Value |
|---|---|
| Base model | sentence-transformers/all-MiniLM-L6-v2 |
| Parameters | 22M |
| ONNX INT8 size | 57MB |
| Accuracy | 99.75% |
| P50 latency | 2.1ms |
| P90 latency | 3.0ms |
| Calibrated ECE | 0.0006 |
Benchmarks
Run the evaluation harness:
Target metrics:
- Intent accuracy: ≥95% (with trained model), ≥70% (rule-based fallback)
- Command accuracy: ≥85%
- P95 latency: <100ms (without model), <500ms (with model)
Training
To train the classifier:
# 1. Generate training data (1000+ samples/intent required)
# 2. Generate evaluation data (separate seed)
# 3. Train model (default: all-MiniLM-L6-v2)
# 4. Export to ONNX with INT8 quantization
# 5. Calibrate confidence (temperature scaling)
# 6. Deploy to sqry-nl/models/
See training/README.md for detailed instructions.
License
MIT License - see LICENSE for details.
Related
- sqry - Semantic code search CLI
- sqry-mcp - MCP server with
sqry_asktool - sqry-openai - OpenAI Agents SDK integration