How cactus-compute/needle works
Purpose and public entry points
Needle is a Python package for local, constrained tool-calling inference, structured extraction, LoRA fine-tuning, and export of compact .cact model binaries. Primary library APIs are exposed through needle/__init__.py; command-line commands are implemented by needle/cli.py. The optional browser playground is served by needle/playground/server.py.
Agent runtime and tool loop
Needle orchestrates a session: accept user text, select/render relevant tools, request constrained model completion, execute selected Python tools, feed results back, and return structured output plus confidence. Tool declaration/introspection and execution contracts are centered in needle/agent/tools.py, while engine acquisition/loading is separated into needle/agent/fetch.py.
Model execution boundary
The Python package does not implement the production inference engine itself; it fetches and caches a prebuilt engine/weights artifact, then invokes it for offline inference. This creates a hard boundary between Python-facing orchestration and the self-contained quantized binary runtime. Model architecture definitions and inference-side support live in needle/model/architecture.py and needle/model/run.py.
Constrained decoding and extraction
Tool arguments and extraction outputs are constrained to schemas through byte-level grammar-aware decoding, producing JSON-compatible structured results rather than unconstrained text. Tokenization and decoding are distinct stages in needle/model/tokenizer.py and needle/model/decode.py. The same runtime path supports direct completions, schema extraction, and agent tool calls.
Fine-tuning pipeline
Training uses JAX-based LoRA over a frozen base checkpoint. The pipeline consumes JSONL tool-call examples, optionally generates/augments examples via an external OpenAI-compatible endpoint, trains an adapter, then preserves deployment compatibility by merging it into the base before export. Core training logic is in needle/model/finetune.py; CLI-driven data synthesis is part of needle/cli.py.
Build, quantization, and distribution
Export merges a base checkpoint and optional LoRA adapter, quantizes model weights, and emits a portable .cact artifact consumable by the same inference engine. These transformations are separated into needle/model/export.py and needle/model/quantize.py. Artifacts may be downloaded from or uploaded to Hugging Face; engine/base checkpoint retrieval also depends on Hugging Face.
Playground service
The playground is a local server that initializes the model before accepting requests, maintains conversational follow-ups, and can invoke fine-tuning from the UI. Backend coordination is in needle/playground/server.py; the static browser client is needle/playground/index.html and needle/playground/app.js.
Runtimes and external services
Inference targets a small cached native/model engine and runs without network after setup. Fine-tuning requires Python/JAX, with optional CUDA or Apple Metal acceleration. Hugging Face supplies engine, base-model, and published .cact artifacts. Optional synthetic-data generation uses OpenRouter by default, or an environment-configured OpenAI-compatible gateway.
Main entry points: cli.py CLI commands · finetune.py LoRA fine-tuning · fetch.py Engine acquisition · server.py Playground server
Components 19
Public Interfaces
- Needle library API
needle/__init__.py - CLI commands · Runs inference, synthesis, fine-tuning, and export workflows.
needle/cli.py
Inference Runtime
- Agent orchestrator · Runs the constrained completion and tool-result feedback loop.
- Tool contracts · Declares, introspects, renders, and executes Python tools.
needle/agent/tools.py - Engine acquisition · Fetches and caches the native engine and model artifacts.
needle/agent/fetch.py - Cached native engine · Self-contained offline inference boundary.
- Model architecture
needle/model/architecture.py - Inference support
needle/model/run.py - Tokenization
needle/model/tokenizer.py - Grammar-constrained decoding · Constrains tool arguments and extraction output to schemas.
needle/model/decode.py
Training & Export
- LoRA fine-tuning · Trains adapters over a frozen base checkpoint.
needle/model/finetune.py - Model export · Merges the base model and optional adapter into a deployable artifact.
needle/model/export.py - Weight quantization · Produces compact weights for portable artifacts.
needle/model/quantize.py - .cact artifact · Consumed by the same native inference engine.
Local Playground
- Playground server · Preloads the model, maintains follow-ups, and starts UI fine-tuning.
needle/playground/server.py - Browser playground
needle/playground/index.html - Playground UI logic
needle/playground/app.js
Other components
- Hugging Face · Hosts engine, base checkpoints, and published artifacts.
- OpenAI-compatible gateway · OpenRouter is the default optional provider.
Connections 24
- Needle library API → Agent orchestrator: creates sessions
- CLI commands → Agent orchestrator: runs inference
- CLI commands → LoRA fine-tuning: starts training
- CLI commands → Model export: exports models
- CLI commands → OpenAI-compatible gateway: optional data synthesis
- Agent orchestrator → Tool contracts: selects and executes
- Agent orchestrator → Engine acquisition: acquires engine
- Agent orchestrator → Tokenization: encodes requests
- Agent orchestrator → Grammar-constrained decoding: requests structured output
- Engine acquisition → Hugging Face: downloads artifacts
- Engine acquisition → Cached native engine: caches and loads
- Model architecture → Inference support: defines model
- Tokenization → Cached native engine: token input
- Cached native engine → Grammar-constrained decoding: token output
- LoRA fine-tuning → Hugging Face: retrieves base checkpoint
- LoRA fine-tuning → Model export: produces LoRA adapter
- Model export → Weight quantization: quantizes merged weights
- Weight quantization → .cact artifact: emits
- .cact artifact → Cached native engine: loaded for inference
- .cact artifact → Hugging Face: optional publish
- Browser playground → Playground UI logic: loads
- Playground UI logic → Playground server: local requests
- Playground server → Agent orchestrator: serves conversations
- Playground server → LoRA fine-tuning: starts UI training