Pre-retrieval query transformation middleware for RAG pipelines. QueryForge takes a raw user query and enriches it into a structured retrieval payload using a locally-run LLM — no external API calls.
Given a query, QueryForge:
- Classifies intent —
EXACT_MATCH,GREETING, orCONCEPTUAL - For conceptual queries, resolves conversational context and applies one of three strategies:
multi_query— expands the query into 3 distinct search variations for dense retrievalhyde— generates a hypothetical ideal document (HyDE) for dense retrievalsub_queries— decomposes a complex/comparative query into distinct sub-queries
- Extracts key terms — extracts key noun phrases (using POS-tagging via NLTK) for sparse/keyword retrieval
- Caches semantically — optionally caches queries using vector embeddings to bypass inference for similar recurring requests
- Returns a structured payload — outputs a unified JSON payload containing both dense and sparse representations ready for retrieval systems
| Model | HuggingFace repo |
|---|---|
| Qwen2.5-3B-Instruct | Qwen/Qwen2.5-3B-Instruct |
| Qwen2.5-3B-Instruct-4bit | mlx-community/Qwen2.5-3B-Instruct-4bit |
| Qwen3-4B-Instruct-2507-4bit | mlx-community/Qwen3-4B-Instruct-2507-4bit |
| Llama-3.2-3B-Instruct | unsloth/Llama-3.2-3B-Instruct |
| Llama-3.2-3B-Instruct-4bit | mlx-community/Llama-3.2-3B-Instruct-4bit |
| Provider | Description |
|---|---|
mlx |
Optimized inference on Apple Silicon via MLX |
huggingface |
Standard HuggingFace transformers pipeline |
llama |
Dedicated MLX engine for Llama models with thread-pinned execution to prevent GPU stream conflicts |
pip install -r requirements.txtCreate a .env file in the project root:
HF_TOKEN=your_huggingface_token_here
HF_TOKENis required only for downloading models. The server itself does not need it at runtime.
python download_model.pySelect a model by number. It will be saved to local_models/.
Open run_server.py and set the provider and model at the top of the file:
PROVIDER = LLMProvider.MLX
MODEL = LLMModel.QWEN_3B_INSTRUCT_4_BITThen start the server:
python run_server.pyServer runs at http://localhost:8000 — interactive docs at http://localhost:8000/docs.
{
"query": "What causes transformer attention to fail on long sequences?",
"strategy": "multi_query",
"history": []
}Strategies: multi_query (default) · hyde · sub_queries
Returns the current provider and model name.
queryforge/
├── run_server.py # FastAPI server — entry point
├── optimizer.py # Core query transformation pipeline
├── llm.py # HuggingFace & MLX engine wrappers
├── config.py # Provider/model enums and constants
├── schemas.py # Pydantic response models
├── prompts.yaml # System prompts for each LLM task
├── semantic_cache.py # Semantic caching decorator using sentence-transformers
└── download_model.py # CLI tool to fetch models from HuggingFace