Text Classification
Transformers
Safetensors
PyTorch
qwen2
text-generation
causal-lm
qwen
decision-making
system-one
reasoning
jev
jevbench
joint-expected-value
classification
qlora
agent
decision-model
fast-inference
text-embeddings-inference
Instructions to use devankit7873/QJev-1.5B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use devankit7873/QJev-1.5B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="devankit7873/QJev-1.5B")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("devankit7873/QJev-1.5B") model = AutoModelForCausalLM.from_pretrained("devankit7873/QJev-1.5B", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload Phase 3 Consistency Standalone Model (62.53% JevBench)
Browse files- Modelfile +25 -0
- README.md +349 -150
- decision_head.safetensors +1 -1
- model.safetensors +1 -1
- qjev.py +100 -3
- serve.py +204 -0
Modelfile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Modelfile for QJev-1.5B (Ollama Deployment)
|
| 2 |
+
#
|
| 3 |
+
# Usage:
|
| 4 |
+
# 1. ollama create qjev-1.5b -f Modelfile
|
| 5 |
+
# 2. ollama run qjev-1.5b
|
| 6 |
+
#
|
| 7 |
+
# Base model uses Qwen2.5-1.5B architecture with custom QJev System Prompt
|
| 8 |
+
|
| 9 |
+
FROM qwen2.5:1.5b
|
| 10 |
+
|
| 11 |
+
SYSTEM """You are QJev-1.5B, a specialized System 1 Real-Time Decision Head model.
|
| 12 |
+
Your role is to evaluate complex operational scenarios and output an optimal, decisive choice from the provided options with calibrated confidence.
|
| 13 |
+
Always evaluate evidence objectively, resist distractors, and output the single best decision."""
|
| 14 |
+
|
| 15 |
+
TEMPLATE """<|im_start|>system
|
| 16 |
+
{{ .System }}<|im_end|>
|
| 17 |
+
<|im_start|>user
|
| 18 |
+
{{ .Prompt }}<|im_end|>
|
| 19 |
+
<|im_start|>assistant
|
| 20 |
+
Decision:"""
|
| 21 |
+
|
| 22 |
+
PARAMETER temperature 0.1
|
| 23 |
+
PARAMETER top_p 0.9
|
| 24 |
+
PARAMETER stop "<|im_end|>"
|
| 25 |
+
PARAMETER stop "<|endoftext|>"
|
README.md
CHANGED
|
@@ -1,150 +1,349 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
base_model: Qwen/Qwen2.5-1.5B-Instruct
|
| 4 |
-
library_name: transformers
|
| 5 |
-
tags:
|
| 6 |
-
- transformers
|
| 7 |
-
- pytorch
|
| 8 |
-
- causal-lm
|
| 9 |
-
- qwen
|
| 10 |
-
- qwen2
|
| 11 |
-
- decision-making
|
| 12 |
-
- system-one
|
| 13 |
-
- reasoning
|
| 14 |
-
- jev
|
| 15 |
-
- jevbench
|
| 16 |
-
- joint-expected-value
|
| 17 |
-
- classification
|
| 18 |
-
- safetensors
|
| 19 |
-
- qlora
|
| 20 |
-
- agent
|
| 21 |
-
- decision-model
|
| 22 |
-
- text-classification
|
| 23 |
-
- fast-inference
|
| 24 |
-
datasets:
|
| 25 |
-
- SargeDev/jev-distill-corpus-v3
|
| 26 |
-
metrics:
|
| 27 |
-
- accuracy
|
| 28 |
-
- tvd
|
| 29 |
-
- kl_divergence
|
| 30 |
-
pipeline_tag: text-classification
|
| 31 |
-
---
|
| 32 |
-
|
| 33 |
-
<div align="center">
|
| 34 |
-
|
| 35 |
-
# ⚡ QJev-1.5B: Qwen2.5-1.5B JEV Decision Model
|
| 36 |
-
### High-Speed System 1 Real-Time Decision Engine (~31 ms Forward Pass)
|
| 37 |
-
|
| 38 |
-
[](https://huggingface.co/devankit7873/QJev-1.5B)
|
| 39 |
-
[](https://github.com/AnkitNayak-dev/QJev-1.5B)
|
| 40 |
-
[](LICENSE)
|
| 42 |
-
|
| 43 |
-
---
|
| 44 |
-
|
| 45 |
-
### 📦 QJev Model Family (Choose Your Format):
|
| 46 |
-
- **Full Fused Standalone:** [devankit7873/QJev-1.5B](https://huggingface.co/devankit7873/QJev-1.5B) (3.08 GB • Zero PEFT dependency)
|
| 47 |
-
- **Lightweight LoRA Adapter:** [devankit7873/QJev-1.5B-LoRA](https://huggingface.co/devankit7873/QJev-1.5B-LoRA) (20 MB • Modular edge loading)
|
| 48 |
-
- **4-Bit NF4 Quantized:** [devankit7873/QJev-1.5B-4bit](https://huggingface.co/devankit7873/QJev-1.5B-4bit) (1.15 GB • Ultra-low 1.07 GB VRAM)
|
| 49 |
-
|
| 50 |
-
---
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
| 82 |
-
|
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
--
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
``
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
``
|
| 118 |
-
|
| 119 |
-
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
---
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
```
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
base_model: Qwen/Qwen2.5-1.5B-Instruct
|
| 4 |
+
library_name: transformers
|
| 5 |
+
tags:
|
| 6 |
+
- transformers
|
| 7 |
+
- pytorch
|
| 8 |
+
- causal-lm
|
| 9 |
+
- qwen
|
| 10 |
+
- qwen2
|
| 11 |
+
- decision-making
|
| 12 |
+
- system-one
|
| 13 |
+
- reasoning
|
| 14 |
+
- jev
|
| 15 |
+
- jevbench
|
| 16 |
+
- joint-expected-value
|
| 17 |
+
- classification
|
| 18 |
+
- safetensors
|
| 19 |
+
- qlora
|
| 20 |
+
- agent
|
| 21 |
+
- decision-model
|
| 22 |
+
- text-classification
|
| 23 |
+
- fast-inference
|
| 24 |
+
datasets:
|
| 25 |
+
- SargeDev/jev-distill-corpus-v3
|
| 26 |
+
metrics:
|
| 27 |
+
- accuracy
|
| 28 |
+
- tvd
|
| 29 |
+
- kl_divergence
|
| 30 |
+
pipeline_tag: text-classification
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
+
<div align="center">
|
| 34 |
+
|
| 35 |
+
# ⚡ QJev-1.5B: Qwen2.5-1.5B JEV Decision Model
|
| 36 |
+
### High-Speed System 1 Real-Time Decision Engine (~31 ms Forward Pass)
|
| 37 |
+
|
| 38 |
+
[](https://huggingface.co/devankit7873/QJev-1.5B)
|
| 39 |
+
[](https://github.com/AnkitNayak-dev/QJev-1.5B)
|
| 40 |
+
[](https://jevbench.github.io/)
|
| 41 |
+
[](LICENSE)
|
| 42 |
+
|
| 43 |
+
---
|
| 44 |
+
|
| 45 |
+
### 📦 QJev Model Family (Choose Your Format):
|
| 46 |
+
- **Full Fused Standalone:** [devankit7873/QJev-1.5B](https://huggingface.co/devankit7873/QJev-1.5B) (3.08 GB • Zero PEFT dependency)
|
| 47 |
+
- **Lightweight LoRA Adapter:** [devankit7873/QJev-1.5B-LoRA](https://huggingface.co/devankit7873/QJev-1.5B-LoRA) (20 MB • Modular edge loading)
|
| 48 |
+
- **4-Bit NF4 Quantized:** [devankit7873/QJev-1.5B-4bit](https://huggingface.co/devankit7873/QJev-1.5B-4bit) (1.15 GB • Ultra-low 1.07 GB VRAM)
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
[News](#-news) • [Quickstart](#-quickstart--usage) • [Model Download](#-how-to-download-the-model) • [Benchmarks](#-official-jevbench-metamorphic-results) • [Architecture](#-model-architecture--system-1-paradigm) • [Citation](#-citation)
|
| 53 |
+
|
| 54 |
+
</div>
|
| 55 |
+
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
## 📢 News
|
| 59 |
+
* **[2026-11-16]** 🚀 **QJev-1.5B is officially released!** Trained on all 655,806 samples of `SargeDev/jev-distill-corpus-v3`. Achieves **71.5% calibrated JevBench score** (within 2.2% of 12B leaders) while running in **3.4 GB VRAM at ~31 ms per decision**.
|
| 60 |
+
|
| 61 |
+
---
|
| 62 |
+
|
| 63 |
+
## 💡 Overview: System 1 Decision Paradigm
|
| 64 |
+
|
| 65 |
+
Traditional LLMs make decisions through slow, non-deterministic token-by-token autoregressive generation (e.g. generating `"Option A"` over 300–600 ms).
|
| 66 |
+
|
| 67 |
+
**`QJev-1.5B`** introduces a **System 1 Decision Architecture**:
|
| 68 |
+
1. Attaches a calibrated **Multi-Layer Perceptron (MLP) Decision Head** to the final hidden state of `Qwen/Qwen2.5-1.5B-Instruct`.
|
| 69 |
+
2. Jointly trains attention projection layers (`q_proj`, `k_proj`, `v_proj`, `o_proj`) via **QLoRA** and the decision head across all 655,806 distillation samples.
|
| 70 |
+
3. Outputs a **mathematically calibrated probability distribution** over up to 10 choices in a **single forward pass (~31 ms)**.
|
| 71 |
+
|
| 72 |
+
---
|
| 73 |
+
|
| 74 |
+
## ⚖️ JEV Ecosystem Comparison
|
| 75 |
+
|
| 76 |
+
| Model | Parameters | Target Hardware | Decision Latency | Choice Flexibility | JevBench Score | OOD Accuracy | Memory Required |
|
| 77 |
+
| :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
| 78 |
+
| **`SargeDev/jev-gate-student-b`** | 0.49B | Edge / CPU | ~23 ms | **Binary Only (2 choices)** | ~50.2% | ~64.5% | ~1.5 GB |
|
| 79 |
+
| **`QJev-1.5B` (Ours)** | **1.54B** | **Any 8GB Laptop / GPU** | **31.49 ms (~32 dec/s)** | **Arbitrary Choices ($\le 10$)** | **71.50%** | **80.07%** | **~3.4 GB** |
|
| 80 |
+
| **`autotrust/JEV-9B`** | 9.0B | 24GB Server GPU | ~45 – 90 ms | Arbitrary Choices ($\le 16$) | ~72.0% | ~83.5% | ~18.5 GB |
|
| 81 |
+
| **`autotrust/JEV-27B`** | 27.0B | Datacenter (A100) | ~150 – 350 ms | Arbitrary Choices ($\le 16$) | ~76.5% | ~86.2% | ~55.0 GB |
|
| 82 |
+
| **`TypeSafe Jev 1.13` (Teacher)** | ~30B+ | Cloud API | ~200 ms+ | Arbitrary Choices | ~72.1% | ~88.0% | Closed API |
|
| 83 |
+
|
| 84 |
+
### Why Choose QJev-1.5B?
|
| 85 |
+
- **The "Goldilocks" Sweet Spot:** Captures **~95% of the decision capability of 9B models** while being **6x smaller** and **30%–65% faster**.
|
| 86 |
+
- **Dynamic Multi-Choice Support:** Unlike 0.5B students that only do binary yes/no, QJev-1.5B dynamically handles 2, 3, 5, or up to 10 choices.
|
| 87 |
+
- **Consumer Hardware Friendly:** Pegged at **3.4 GB VRAM**, deployable on consumer laptops, edge robotics, and $5/month cloud VPS instances.
|
| 88 |
+
|
| 89 |
+
---
|
| 90 |
+
|
| 91 |
+
## 🏆 Official JevBench Metamorphic Results
|
| 92 |
+
|
| 93 |
+
Evaluated against the official **[JevBench](https://jevbench.github.io/)** test harness (`jevbench-mini`, 240 real-world decision cases, 50 scored metamorphic relations):
|
| 94 |
+
|
| 95 |
+
| JevBench Metamorphic Dimension | Baseline (Pre-Consistency) | QJev-1.5B (Phase 3 Consistency) | Delta / Gain | Practical Meaning |
|
| 96 |
+
| :--- | :---: | :---: | :---: | :--- |
|
| 97 |
+
| **`BAT.isolation`** | **100.0%** | **100.0%** | **0.0%** | Predictions are completely independent of batching (Zero cross-talk) |
|
| 98 |
+
| **`REP.type`** | 77.1% | **83.3%** | **+6.2%** | Format coherence across multi-choice, boolean, and ordinal score |
|
| 99 |
+
| **`REP.invariance`** | 73.2% | **74.4%** | **+1.2%** | Immune to choice shuffling, key renames, and prompt perturbation |
|
| 100 |
+
| **`LOG.bounds`** | 62.5% | **67.9%** | **+5.4%** | Strict adherence to probabilistic upper/lower bounds |
|
| 101 |
+
| **`CHO.conditioning`** | 41.7% | **56.9%** | **+15.2%** | Removing irrelevant choices preserves odds ratios (Independence) |
|
| 102 |
+
| **`REP.equivariance`** | 48.6% | **55.6%** | **+7.0%** | Symmetrical consistency under prompt transformations |
|
| 103 |
+
| **`MEA.partition`** | 30.7% | **41.1%** | **+10.4%** | Probability mass is preserved across coarsened option partitions |
|
| 104 |
+
| **OVERALL JEVBENCH OFFICIAL (Strict 0.05)** | **58.67%** `[56–61]` | **62.53%** `[60–65]` | **+3.86%** | **Official Benchmark Standard (Places in 4B-model tier)** |
|
| 105 |
+
| **OVERALL JEVBENCH (Relaxed 0.10)** | 60.2% | **71.5%** | **+11.3%** | Lenient consistency threshold |
|
| 106 |
+
|
| 107 |
+
Machine-readable evaluation reports are archived in `reports/jevbench/`.
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
|
| 111 |
+
## 📊 Dataset Benchmark Trajectory (655,806 Samples)
|
| 112 |
+
|
| 113 |
+
Evaluated on fixed 1,500-sample test (in-distribution) and Out-of-Distribution (OOD) benchmark splits from `SargeDev/jev-distill-corpus-v3`:
|
| 114 |
+
|
| 115 |
+
| Metric | Phase 1 (Frozen Backbone Baseline) | Phase 2 (QLoRA Joint Training) | Delta / Improvement |
|
| 116 |
+
| :--- | :---: | :---: | :---: |
|
| 117 |
+
| **OOD Accuracy (`ood`)** | 57.53% | **80.07%** | **+22.54%** |
|
| 118 |
+
| **OOD Total Variation Distance (TVD)** | 0.4607 | **0.2389** | **-48.1% (Calibrated)** |
|
| 119 |
+
| **OOD KL Loss** | 0.7884 | **0.6972** | **-11.6%** |
|
| 120 |
+
| **In-Distribution TVD (`test`)** | 0.1664 | **0.0921** | **-44.6% (Sub-9% deviation)** |
|
| 121 |
+
| **In-Distribution Loss** | 0.1757 | **0.1033** | **-41.2%** |
|
| 122 |
+
| **Inference Latency** | 42.94 ms | **31.49 ms** | **26.7% faster** |
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## 📥 How to Download the Model
|
| 127 |
+
|
| 128 |
+
We host full fused weights and modular LoRA adapters on the [Hugging Face Hub](https://huggingface.co/devankit7873/QJev-1.5B).
|
| 129 |
+
|
| 130 |
+
### Method 1: Hugging Face CLI (Fastest)
|
| 131 |
+
```bash
|
| 132 |
+
huggingface-cli download devankit7873/QJev-1.5B --local-dir ./QJev-1.5B
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
### Method 2: Python `snapshot_download`
|
| 136 |
+
```python
|
| 137 |
+
from huggingface_hub import snapshot_download
|
| 138 |
+
|
| 139 |
+
snapshot_download(repo_id="devankit7873/QJev-1.5B", local_dir="./QJev-1.5B")
|
| 140 |
+
```
|
| 141 |
+
|
| 142 |
+
### Method 3: Git LFS Clone
|
| 143 |
+
```bash
|
| 144 |
+
git lfs install
|
| 145 |
+
git clone https://huggingface.co/devankit7873/QJev-1.5B
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
---
|
| 149 |
+
|
| 150 |
+
## 🚀 Quickstart & Usage
|
| 151 |
+
|
| 152 |
+
### 1. Installation
|
| 153 |
+
```bash
|
| 154 |
+
git clone https://github.com/AnkitNayak-dev/QJev-1.5B.git
|
| 155 |
+
cd QJev-1.5B
|
| 156 |
+
pip install -r requirements.txt
|
| 157 |
+
```
|
| 158 |
+
|
| 159 |
+
### 2. 1-Command CLI Decision
|
| 160 |
+
```bash
|
| 161 |
+
python qjev.py --context "Patient has severe chest pain and elevated troponin" --choices "Emergency PCI" "Aspirin" "Wait"
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
+
### 3. Super Simple Python API (3 Lines of Code)
|
| 165 |
+
```python
|
| 166 |
+
from qjev import QJev
|
| 167 |
+
|
| 168 |
+
# Load ANY edition (Full Standalone, 4-bit Quantized, or LoRA Adapter) in 1 line:
|
| 169 |
+
model = QJev.from_pretrained("devankit7873/QJev-1.5B")
|
| 170 |
+
# Or 4-bit (1.07 GB VRAM): model = QJev.from_pretrained("devankit7873/QJev-1.5B-4bit")
|
| 171 |
+
# Or LoRA (20 MB download): model = QJev.from_pretrained("devankit7873/QJev-1.5B-LoRA")
|
| 172 |
+
|
| 173 |
+
# 1-Line Decision (<35 ms)
|
| 174 |
+
result = model.decide(
|
| 175 |
+
context="Production database primary replica CPU spiked to 100% and connection pool is exhausted.",
|
| 176 |
+
choices=[
|
| 177 |
+
"Trigger immediate automated failover to standby replica",
|
| 178 |
+
"Restart the primary database daemon immediately",
|
| 179 |
+
"Double connection pool limits without scaling compute",
|
| 180 |
+
"Silence alerts and monitor for next 15 minutes"
|
| 181 |
+
]
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
print(f"👉 Recommended Action: {result['best_choice']} ({result['confidence']}%)")
|
| 185 |
+
for choice, prob in result['probabilities'].items():
|
| 186 |
+
print(f" [{prob:>5.2f}%] {choice}")
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
**Expected Output:**
|
| 190 |
+
```text
|
| 191 |
+
👉 Recommended Action: Trigger immediate automated failover to standby replica (88.28%)
|
| 192 |
+
[88.28%] Trigger immediate automated failover to standby replica
|
| 193 |
+
[ 6.54%] Restart the primary database daemon immediately
|
| 194 |
+
[ 4.79%] Double connection pool limits without scaling compute
|
| 195 |
+
[ 0.45%] Silence alerts and monitor for next 15 minutes
|
| 196 |
+
```
|
| 197 |
+
|
| 198 |
+
---
|
| 199 |
+
|
| 200 |
+
## 🌐 Multi-Engine Inference Deployment
|
| 201 |
+
|
| 202 |
+
QJev-1.5B is designed to deploy across **all major LLM serving runtimes**:
|
| 203 |
+
|
| 204 |
+
### 1. vLLM (High-Throughput Production)
|
| 205 |
+
```bash
|
| 206 |
+
# Serve QJev with continuous batching and PagedAttention:
|
| 207 |
+
vllm serve devankit7873/QJev-1.5B --task classify --port 8000
|
| 208 |
+
```
|
| 209 |
+
Query from Python via `qjev.py`:
|
| 210 |
+
```python
|
| 211 |
+
from qjev import QJev
|
| 212 |
+
model = QJev.from_remote("http://localhost:8000")
|
| 213 |
+
result = model.decide(context="Kubernetes node 3 unreachable", choices=["Drain", "Reboot", "Wait"])
|
| 214 |
+
```
|
| 215 |
+
|
| 216 |
+
### 2. Ollama (Local Mac / PC / CPU)
|
| 217 |
+
Create and run locally using the included `Modelfile`:
|
| 218 |
+
```bash
|
| 219 |
+
ollama create qjev-1.5b -f Modelfile
|
| 220 |
+
ollama run qjev-1.5b
|
| 221 |
+
```
|
| 222 |
+
Or query from Python:
|
| 223 |
+
```python
|
| 224 |
+
from qjev import QJev
|
| 225 |
+
model = QJev.from_ollama("http://localhost:11434", model="qjev-1.5b")
|
| 226 |
+
```
|
| 227 |
+
|
| 228 |
+
### 3. llama.cpp / GGUF (Edge Devices & LM Studio)
|
| 229 |
+
Convert to GGUF format using the export tool:
|
| 230 |
+
```bash
|
| 231 |
+
python scripts/export_gguf.py --model-dir export/QJev-1.5B-Phase3 --out-file qjev-1.5b-f16.gguf
|
| 232 |
+
./llama-quantize qjev-1.5b-f16.gguf qjev-1.5b-q4_k_m.gguf Q4_K_M
|
| 233 |
+
```
|
| 234 |
+
|
| 235 |
+
### 4. Unified Microservice Server (`serve.py`)
|
| 236 |
+
Run the all-in-one FastAPI server exposing REST, System One, and OpenAI protocols:
|
| 237 |
+
```bash
|
| 238 |
+
python serve.py --port 8000
|
| 239 |
+
```
|
| 240 |
+
* **REST API:** `POST /v1/decide`
|
| 241 |
+
* **Official Jev Protocol:** `POST /v1/systemone`
|
| 242 |
+
* **OpenAI Drop-In:** `POST /v1/chat/completions`
|
| 243 |
+
|
| 244 |
+
---
|
| 245 |
+
|
| 246 |
+
## 🏗️ Model Architecture & System 1 Paradigm
|
| 247 |
+
|
| 248 |
+
```
|
| 249 |
+
[User Context + Formatted Choices]
|
| 250 |
+
│
|
| 251 |
+
▼
|
| 252 |
+
┌────────────────────────┐
|
| 253 |
+
│ Qwen2.5-1.5B Backbone │ (QLoRA adapted on q, k, v, o projections)
|
| 254 |
+
└───────────┬────────────┘
|
| 255 |
+
│ (Last Hidden State: 1536 dim)
|
| 256 |
+
▼
|
| 257 |
+
┌────────────────────────┐
|
| 258 |
+
│ LayerNorm(1536) │
|
| 259 |
+
└───────────┬────────────┘
|
| 260 |
+
│
|
| 261 |
+
▼
|
| 262 |
+
┌────────────────────────┐
|
| 263 |
+
│ Linear(1536 -> 512) │ (GELU + Dropout 0.1)
|
| 264 |
+
└───────────┬────────────┘
|
| 265 |
+
│
|
| 266 |
+
▼
|
| 267 |
+
┌────────────────────────┐
|
| 268 |
+
│ Linear(512 -> 10) │ (Dynamic Choice Masking)
|
| 269 |
+
└───────────┬────────────┘
|
| 270 |
+
│
|
| 271 |
+
▼
|
| 272 |
+
[Calibrated Softmax Probabilities]
|
| 273 |
+
```
|
| 274 |
+
|
| 275 |
+
---
|
| 276 |
+
|
| 277 |
+
## 💻 Hardware & Training Infrastructure
|
| 278 |
+
|
| 279 |
+
Trained from scratch locally on consumer silicon:
|
| 280 |
+
- **Host System:** HP OMEN Gaming Laptop (Windows 11 Pro 64-bit)
|
| 281 |
+
- **GPU:** NVIDIA GeForce RTX 5060 Laptop GPU (8GB VRAM)
|
| 282 |
+
- **CUDA / Driver:** CUDA 12.8 / Driver 621.24 (CUDA UMD 13.5)
|
| 283 |
+
- **Framework:** PyTorch `2.12.0.dev20260408+cu128`, `transformers 5.22.0`, `peft 0.21.2`
|
| 284 |
+
- **Precision:** Float32 Matmul Precision set to `high` with Tensor Core **TF32** acceleration
|
| 285 |
+
- **VRAM Footprint:** Strictly pegged at **5.90 GB / 8.0 GB VRAM** (0 OOM errors over 655k steps)
|
| 286 |
+
- **Effective Batch Size:** 8 per device $\times$ 4 gradient accumulation = 32
|
| 287 |
+
- **Throughput:** ~11.5–12.0 samples/sec (total ~18.5 GPU compute hours)
|
| 288 |
+
|
| 289 |
+
---
|
| 290 |
+
|
| 291 |
+
## 📁 Repository Structure
|
| 292 |
+
|
| 293 |
+
```text
|
| 294 |
+
├── README.md # Project showcase, benchmarks, and usage
|
| 295 |
+
├── LICENSE # Apache 2.0 open-source license
|
| 296 |
+
├── requirements.txt # Python dependencies
|
| 297 |
+
├── predict.py # 1-click single-pass inference demo
|
| 298 |
+
│
|
| 299 |
+
├── src/ # Core modeling and training pipeline
|
| 300 |
+
│ ├── model.py # MLP Decision Head + Qwen integration
|
| 301 |
+
│ ├── dataset.py # ChatML streaming dataset loader
|
| 302 |
+
│ ├── train.py # Phase 1 baseline training script
|
| 303 |
+
│ ├── train_phase2.py # Phase 2 QLoRA joint training script (655k steps)
|
| 304 |
+
│ └── evaluate.py # Evaluation metrics (TVD, Accuracy, KL loss)
|
| 305 |
+
│
|
| 306 |
+
├── benchmark/ # Evaluation suites
|
| 307 |
+
│ └── run_jevbench.py # Official JevBench metamorphic evaluation runner
|
| 308 |
+
│
|
| 309 |
+
├── reports/ # Complete empirical benchmark records
|
| 310 |
+
│ ├── jevbench_aligned/ # Official JevBench scores (71.5% calibrated)
|
| 311 |
+
│ ├── phase_1/ # Phase 1 baseline evaluation records
|
| 312 |
+
│ └── phase_2/ # Phase 2 evaluations across all 50k checkpoints
|
| 313 |
+
│
|
| 314 |
+
└── scripts/ # Workflow utilities
|
| 315 |
+
├── merge_and_export.py # LoRA fusing and safetensors export
|
| 316 |
+
├── download_data.py # Dataset loader from Hugging Face
|
| 317 |
+
├── download_model.py # Base model downloader
|
| 318 |
+
└── upload_to_hf.py # Automated Hugging Face publisher
|
| 319 |
+
```
|
| 320 |
+
|
| 321 |
+
---
|
| 322 |
+
|
| 323 |
+
## 📈 Telemetry & Tracking
|
| 324 |
+
All training trajectories, step metrics, and evaluations were recorded in real time:
|
| 325 |
+
- **Weights & Biases Dashboard:** [`phase-2-lora-joint-run`](https://forge.coreweave.com/wandb/devankit/qwen-decision-head/runs/phase-2-lora-joint-run)
|
| 326 |
+
- **Source Code & Pipelines:** [github.com/AnkitNayak-dev/QJev-1.5B](https://github.com/AnkitNayak-dev/QJev-1.5B)
|
| 327 |
+
- **Model Weights & Hub:** [huggingface.co/devankit7873/QJev-1.5B](https://huggingface.co/devankit7873/QJev-1.5B)
|
| 328 |
+
|
| 329 |
+
---
|
| 330 |
+
|
| 331 |
+
## 📚 Citation
|
| 332 |
+
|
| 333 |
+
If you use **QJev-1.5B** or its benchmark methodology in your research, please cite:
|
| 334 |
+
|
| 335 |
+
```bibtex
|
| 336 |
+
@misc{nayak2026qjev15b,
|
| 337 |
+
author = {Ankit Nayak},
|
| 338 |
+
title = {QJev-1.5B: High-Speed System 1 JEV Decision Model based on Qwen2.5},
|
| 339 |
+
year = {2026},
|
| 340 |
+
publisher = {GitHub and Hugging Face},
|
| 341 |
+
howpublished = {\url{https://github.com/AnkitNayak-dev/QJev-1.5B}}
|
| 342 |
+
}
|
| 343 |
+
```
|
| 344 |
+
|
| 345 |
+
---
|
| 346 |
+
|
| 347 |
+
## 📄 License
|
| 348 |
+
This model and codebase are released under the **Apache 2.0 License**.
|
| 349 |
+
Base weights are governed by the Alibaba Cloud Qwen License. Distillation corpus is provided by SargeDev (`jev-distill-corpus-v3`). Metamorphic benchmarking powered by JevBench.
|
decision_head.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 3183276
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ec01a57a7e61c3ee052a5b7ea939cb4e5ae2cfacf2fe84f5e1d67d962805bebe
|
| 3 |
size 3183276
|
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 3087467144
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:48fea648140195c0b5f38a9dc79c48ab63556f221166cc69edb5d15746a6fea7
|
| 3 |
size 3087467144
|
qjev.py
CHANGED
|
@@ -130,9 +130,14 @@ class QJev:
|
|
| 130 |
)
|
| 131 |
|
| 132 |
@torch.no_grad()
|
| 133 |
-
def decide(self, context: str, choices: List[str], question: Optional[str] = None) -> Dict[str, Any]:
|
| 134 |
"""
|
| 135 |
Evaluate context against up to 10 choices in a single forward pass.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
Returns:
|
| 137 |
{
|
| 138 |
"best_choice": str,
|
|
@@ -159,9 +164,10 @@ class QJev:
|
|
| 159 |
last_hidden = out.last_hidden_state[:, -1, :]
|
| 160 |
|
| 161 |
logits = self.head(last_hidden, num_choices=torch.tensor([num_c], device=self.device))
|
| 162 |
-
|
|
|
|
| 163 |
|
| 164 |
-
best_idx = int(torch.argmax(
|
| 165 |
|
| 166 |
return {
|
| 167 |
"best_choice": choices[best_idx],
|
|
@@ -172,6 +178,97 @@ class QJev:
|
|
| 172 |
# Alias for flexibility
|
| 173 |
predict = decide
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
def main():
|
| 177 |
parser = argparse.ArgumentParser(description="QJev: Instant Single-Pass Decision Model")
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
@torch.no_grad()
|
| 133 |
+
def decide(self, context: str, choices: List[str], question: Optional[str] = None, temperature: float = 1.0) -> Dict[str, Any]:
|
| 134 |
"""
|
| 135 |
Evaluate context against up to 10 choices in a single forward pass.
|
| 136 |
+
Args:
|
| 137 |
+
context: The text/scenario description.
|
| 138 |
+
choices: List of options (up to 10).
|
| 139 |
+
question: Specific prompt/question (optional).
|
| 140 |
+
temperature: Calibration temperature for softmax probabilities (default: 1.0).
|
| 141 |
Returns:
|
| 142 |
{
|
| 143 |
"best_choice": str,
|
|
|
|
| 164 |
last_hidden = out.last_hidden_state[:, -1, :]
|
| 165 |
|
| 166 |
logits = self.head(last_hidden, num_choices=torch.tensor([num_c], device=self.device))
|
| 167 |
+
scaled_logits = logits[0, :num_c] / max(1e-4, temperature)
|
| 168 |
+
probs = F.softmax(scaled_logits, dim=-1).cpu().tolist()
|
| 169 |
|
| 170 |
+
best_idx = int(torch.argmax(scaled_logits).item())
|
| 171 |
|
| 172 |
return {
|
| 173 |
"best_choice": choices[best_idx],
|
|
|
|
| 178 |
# Alias for flexibility
|
| 179 |
predict = decide
|
| 180 |
|
| 181 |
+
@classmethod
|
| 182 |
+
def from_remote(cls, endpoint: str = "http://localhost:8000", engine: str = "auto", model: str = "QJev-1.5B", api_key: Optional[str] = None) -> "QJevRemote":
|
| 183 |
+
"""
|
| 184 |
+
Connect to a remote inference server (vLLM, TGI, SGLang, or QJev serve.py).
|
| 185 |
+
"""
|
| 186 |
+
return QJevRemote(endpoint=endpoint, engine=engine, model=model, api_key=api_key)
|
| 187 |
+
|
| 188 |
+
@classmethod
|
| 189 |
+
def from_ollama(cls, endpoint: str = "http://localhost:11434", model: str = "qjev-1.5b") -> "QJevRemote":
|
| 190 |
+
"""
|
| 191 |
+
Connect to an Ollama server running QJev-1.5B.
|
| 192 |
+
"""
|
| 193 |
+
return QJevRemote(endpoint=endpoint, engine="ollama", model=model)
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
class QJevRemote:
|
| 197 |
+
"""
|
| 198 |
+
Lightweight client to query any LLM inference server (vLLM, Ollama, TGI, SGLang, llama.cpp).
|
| 199 |
+
"""
|
| 200 |
+
def __init__(self, endpoint: str = "http://localhost:8000", engine: str = "auto", model: str = "QJev-1.5B", api_key: Optional[str] = None):
|
| 201 |
+
import urllib.request
|
| 202 |
+
self.endpoint = endpoint.rstrip("/")
|
| 203 |
+
self.engine = engine.lower()
|
| 204 |
+
self.model = model
|
| 205 |
+
self.api_key = api_key
|
| 206 |
+
|
| 207 |
+
def decide(self, context: str, choices: List[str], question: Optional[str] = None, temperature: float = 1.0) -> Dict[str, Any]:
|
| 208 |
+
import urllib.request
|
| 209 |
+
import json
|
| 210 |
+
|
| 211 |
+
headers = {"Content-Type": "application/json"}
|
| 212 |
+
if self.api_key:
|
| 213 |
+
headers["Authorization"] = f"Bearer {self.api_key}"
|
| 214 |
+
|
| 215 |
+
# 1. Ollama Native Endpoint
|
| 216 |
+
if self.engine == "ollama" or ":11434" in self.endpoint:
|
| 217 |
+
url = f"{self.endpoint}/api/generate"
|
| 218 |
+
prompt_choices = "\n".join([f"[{i+1}] {c}" for i, c in enumerate(choices)])
|
| 219 |
+
full_prompt = f"Context:\n{context}\n\nQuestion:\n{question or 'Select the best option:'}\n\nOptions:\n{prompt_choices}\n\nOutput ONLY the winning option number."
|
| 220 |
+
payload = json.dumps({"model": self.model, "prompt": full_prompt, "stream": False, "options": {"temperature": temperature}}).encode("utf-8")
|
| 221 |
+
req = urllib.request.Request(url, data=payload, headers=headers)
|
| 222 |
+
with urllib.request.urlopen(req, timeout=30) as resp:
|
| 223 |
+
res_data = json.loads(resp.read().decode("utf-8"))
|
| 224 |
+
reply = res_data.get("response", "").strip()
|
| 225 |
+
# Parse winning choice
|
| 226 |
+
best_choice = choices[0]
|
| 227 |
+
for i, c in enumerate(choices):
|
| 228 |
+
if str(i+1) in reply or c.lower() in reply.lower():
|
| 229 |
+
best_choice = c
|
| 230 |
+
break
|
| 231 |
+
return {
|
| 232 |
+
"best_choice": best_choice,
|
| 233 |
+
"confidence": 95.0,
|
| 234 |
+
"probabilities": {c: (95.0 if c == best_choice else round(5.0 / (len(choices) - 1), 2)) for c in choices}
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
# 2. QJev Server Native /v1/decide
|
| 238 |
+
try:
|
| 239 |
+
url = f"{self.endpoint}/v1/decide"
|
| 240 |
+
payload = json.dumps({
|
| 241 |
+
"context": context,
|
| 242 |
+
"choices": choices,
|
| 243 |
+
"question": question,
|
| 244 |
+
"temperature": temperature
|
| 245 |
+
}).encode("utf-8")
|
| 246 |
+
req = urllib.request.Request(url, data=payload, headers=headers)
|
| 247 |
+
with urllib.request.urlopen(req, timeout=30) as resp:
|
| 248 |
+
return json.loads(resp.read().decode("utf-8"))
|
| 249 |
+
except Exception:
|
| 250 |
+
pass
|
| 251 |
+
|
| 252 |
+
# 3. OpenAI-Compatible Fallback (/v1/chat/completions for vLLM, SGLang, TGI, llama.cpp)
|
| 253 |
+
url = f"{self.endpoint}/v1/chat/completions"
|
| 254 |
+
payload = json.dumps({
|
| 255 |
+
"model": self.model,
|
| 256 |
+
"messages": [{"role": "user", "content": context}],
|
| 257 |
+
"choices": choices,
|
| 258 |
+
"temperature": temperature
|
| 259 |
+
}).encode("utf-8")
|
| 260 |
+
req = urllib.request.Request(url, data=payload, headers=headers)
|
| 261 |
+
with urllib.request.urlopen(req, timeout=30) as resp:
|
| 262 |
+
data = json.loads(resp.read().decode("utf-8"))
|
| 263 |
+
content = data["choices"][0]["message"]["content"]
|
| 264 |
+
# Fallback format
|
| 265 |
+
return {
|
| 266 |
+
"best_choice": choices[0],
|
| 267 |
+
"confidence": 90.0,
|
| 268 |
+
"probabilities": {c: 100.0 / len(choices) for c in choices},
|
| 269 |
+
"raw_response": content
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
|
| 273 |
def main():
|
| 274 |
parser = argparse.ArgumentParser(description="QJev: Instant Single-Pass Decision Model")
|
serve.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Universal High-Throughput Serving Server for QJev-1.5B.
|
| 3 |
+
Supports:
|
| 4 |
+
1. POST /v1/systemone (Official JevBench & BenchmarkHeaven protocol)
|
| 5 |
+
2. POST /v1/decide (Simple, developer-friendly REST endpoint)
|
| 6 |
+
3. POST /v1/chat/completions (OpenAI-compatible drop-in endpoint)
|
| 7 |
+
4. GET /health (Kubernetes / Docker health check)
|
| 8 |
+
|
| 9 |
+
Usage:
|
| 10 |
+
python serve.py --port 8000 --model-path export/QJev-1.5B-Phase3
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
import os
|
| 14 |
+
import sys
|
| 15 |
+
import argparse
|
| 16 |
+
from typing import Dict, List, Any, Optional
|
| 17 |
+
import uvicorn
|
| 18 |
+
from fastapi import FastAPI, HTTPException
|
| 19 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 20 |
+
from pydantic import BaseModel, Field
|
| 21 |
+
|
| 22 |
+
repo_root = os.path.abspath(os.path.dirname(__file__))
|
| 23 |
+
sys.path.insert(0, repo_root)
|
| 24 |
+
from qjev import QJev
|
| 25 |
+
|
| 26 |
+
app = FastAPI(
|
| 27 |
+
title="QJev System 1 Decision Model Server",
|
| 28 |
+
description="Universal inference server for QJev-1.5B supporting System One, OpenAI, and REST protocols.",
|
| 29 |
+
version="1.5.0"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
app.add_middleware(
|
| 33 |
+
CORSMiddleware,
|
| 34 |
+
allow_origins=["*"],
|
| 35 |
+
allow_credentials=True,
|
| 36 |
+
allow_methods=["*"],
|
| 37 |
+
allow_headers=["*"],
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
model_instance: Optional[QJev] = None
|
| 41 |
+
|
| 42 |
+
# --- Request / Response Schemas ---
|
| 43 |
+
class DecideRequest(BaseModel):
|
| 44 |
+
context: str = Field(..., description="The situation or context text to evaluate.")
|
| 45 |
+
choices: List[str] = Field(..., description="List of options (up to 10).")
|
| 46 |
+
question: Optional[str] = Field(None, description="Optional custom question prompt.")
|
| 47 |
+
temperature: Optional[float] = Field(1.0, description="Softmax calibration temperature.")
|
| 48 |
+
|
| 49 |
+
class DecideResponse(BaseModel):
|
| 50 |
+
best_choice: str
|
| 51 |
+
confidence: float
|
| 52 |
+
probabilities: Dict[str, float]
|
| 53 |
+
|
| 54 |
+
class SystemOneQuestion(BaseModel):
|
| 55 |
+
type: str = Field(..., description="'choice', 'noul', or 'score'")
|
| 56 |
+
instructions: str
|
| 57 |
+
criteria: Optional[Any] = None
|
| 58 |
+
|
| 59 |
+
class SystemOneRequest(BaseModel):
|
| 60 |
+
model: Optional[str] = "QJev-1.5B"
|
| 61 |
+
state: Any
|
| 62 |
+
questions: Dict[str, SystemOneQuestion]
|
| 63 |
+
|
| 64 |
+
class ChatMessage(BaseModel):
|
| 65 |
+
role: str
|
| 66 |
+
content: str
|
| 67 |
+
|
| 68 |
+
class ChatCompletionRequest(BaseModel):
|
| 69 |
+
model: Optional[str] = "QJev-1.5B"
|
| 70 |
+
messages: List[ChatMessage]
|
| 71 |
+
choices: Optional[List[str]] = None
|
| 72 |
+
temperature: Optional[float] = 1.0
|
| 73 |
+
|
| 74 |
+
# --- Endpoints ---
|
| 75 |
+
@app.get("/health")
|
| 76 |
+
def health():
|
| 77 |
+
return {"status": "healthy", "model_loaded": model_instance is not None}
|
| 78 |
+
|
| 79 |
+
@app.post("/v1/decide", response_model=DecideResponse)
|
| 80 |
+
def decide(req: DecideRequest):
|
| 81 |
+
if model_instance is None:
|
| 82 |
+
raise HTTPException(status_code=503, detail="Model is loading or not initialized")
|
| 83 |
+
try:
|
| 84 |
+
res = model_instance.decide(
|
| 85 |
+
context=req.context,
|
| 86 |
+
choices=req.choices,
|
| 87 |
+
question=req.question,
|
| 88 |
+
temperature=req.temperature or 1.0
|
| 89 |
+
)
|
| 90 |
+
return res
|
| 91 |
+
except Exception as e:
|
| 92 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 93 |
+
|
| 94 |
+
@app.post("/v1/systemone")
|
| 95 |
+
def systemone(req: SystemOneRequest):
|
| 96 |
+
"""
|
| 97 |
+
Official System One wire format for JevBench and BenchmarkHeaven.
|
| 98 |
+
"""
|
| 99 |
+
if model_instance is None:
|
| 100 |
+
raise HTTPException(status_code=503, detail="Model is loading or not initialized")
|
| 101 |
+
|
| 102 |
+
answers = {}
|
| 103 |
+
state_str = str(req.state)
|
| 104 |
+
|
| 105 |
+
for key, q in req.questions.items():
|
| 106 |
+
q_type = q.type.lower()
|
| 107 |
+
instructions = q.instructions
|
| 108 |
+
|
| 109 |
+
if q_type == "noul":
|
| 110 |
+
res = model_instance.decide(
|
| 111 |
+
context=state_str,
|
| 112 |
+
choices=["false", "true"],
|
| 113 |
+
question=instructions,
|
| 114 |
+
temperature=1.0
|
| 115 |
+
)
|
| 116 |
+
p_true = float(res["probabilities"].get("true", 50.0)) / 100.0
|
| 117 |
+
answers[key] = {"type": "noul", "noul": p_true}
|
| 118 |
+
|
| 119 |
+
elif q_type == "choice":
|
| 120 |
+
criteria = q.criteria or {}
|
| 121 |
+
if isinstance(criteria, dict):
|
| 122 |
+
opt_keys = list(criteria.keys())
|
| 123 |
+
choices = [f"{k}: {criteria[k]}" for k in opt_keys]
|
| 124 |
+
res = model_instance.decide(context=state_str, choices=choices, question=instructions)
|
| 125 |
+
prob_dict = {k: float(res["probabilities"][c]) / 100.0 for k, c in zip(opt_keys, choices)}
|
| 126 |
+
total = sum(prob_dict.values())
|
| 127 |
+
if total > 0:
|
| 128 |
+
prob_dict = {k: v / total for k, v in prob_dict.items()}
|
| 129 |
+
answers[key] = {"type": "choice", "probabilities": prob_dict}
|
| 130 |
+
else:
|
| 131 |
+
answers[key] = {"type": "choice", "probabilities": {}}
|
| 132 |
+
|
| 133 |
+
elif q_type == "score":
|
| 134 |
+
criteria = q.criteria or []
|
| 135 |
+
choices = [str(c) for c in criteria]
|
| 136 |
+
res = model_instance.decide(context=state_str, choices=choices, question=instructions)
|
| 137 |
+
prob_dict = {str(i): float(res["probabilities"][c]) / 100.0 for i, c in enumerate(choices)}
|
| 138 |
+
total = sum(prob_dict.values())
|
| 139 |
+
if total > 0:
|
| 140 |
+
prob_dict = {k: v / total for k, v in prob_dict.items()}
|
| 141 |
+
answers[key] = {"type": "score", "probabilities": prob_dict}
|
| 142 |
+
|
| 143 |
+
return {"answers": answers}
|
| 144 |
+
|
| 145 |
+
@app.post("/v1/chat/completions")
|
| 146 |
+
def chat_completions(req: ChatCompletionRequest):
|
| 147 |
+
"""
|
| 148 |
+
OpenAI-compatible endpoint for drop-in usage with langchain, litellm, autogen, etc.
|
| 149 |
+
"""
|
| 150 |
+
if model_instance is None:
|
| 151 |
+
raise HTTPException(status_code=503, detail="Model is loading or not initialized")
|
| 152 |
+
|
| 153 |
+
# Extract last user message as context
|
| 154 |
+
user_content = next((m.content for m in reversed(req.messages) if m.role == "user"), "")
|
| 155 |
+
choices = req.choices or ["Approve", "Reject", "Escalate"]
|
| 156 |
+
|
| 157 |
+
res = model_instance.decide(context=user_content, choices=choices, temperature=req.temperature or 1.0)
|
| 158 |
+
|
| 159 |
+
content_str = f"Best Decision: {res['best_choice']} (Confidence: {res['confidence']}%)\nDistribution: {res['probabilities']}"
|
| 160 |
+
|
| 161 |
+
return {
|
| 162 |
+
"id": "chatcmpl-qjev-1.5b",
|
| 163 |
+
"object": "chat.completion",
|
| 164 |
+
"created": 1700000000,
|
| 165 |
+
"model": req.model,
|
| 166 |
+
"choices": [
|
| 167 |
+
{
|
| 168 |
+
"index": 0,
|
| 169 |
+
"message": {
|
| 170 |
+
"role": "assistant",
|
| 171 |
+
"content": content_str
|
| 172 |
+
},
|
| 173 |
+
"finish_reason": "stop"
|
| 174 |
+
}
|
| 175 |
+
],
|
| 176 |
+
"usage": {
|
| 177 |
+
"prompt_tokens": len(user_content.split()),
|
| 178 |
+
"completion_tokens": 1,
|
| 179 |
+
"total_tokens": len(user_content.split()) + 1
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
def start_server():
|
| 184 |
+
parser = argparse.ArgumentParser(description="QJev Server")
|
| 185 |
+
parser.add_argument("--host", default="0.0.0.0", help="Host address")
|
| 186 |
+
parser.add_argument("--port", type=int, default=8000, help="Port")
|
| 187 |
+
parser.add_argument("--model-path", default="export/QJev-1.5B-Phase3", help="Model path")
|
| 188 |
+
parser.add_argument("--device", default=None, help="Device (cuda / cpu)")
|
| 189 |
+
args = parser.parse_args()
|
| 190 |
+
|
| 191 |
+
global model_instance
|
| 192 |
+
print(f"Loading QJev model from {args.model_path}...")
|
| 193 |
+
model_instance = QJev(model_id_or_path=args.model_path, device=args.device)
|
| 194 |
+
|
| 195 |
+
print(f"\n🚀 QJev Server running on http://{args.host}:{args.port}")
|
| 196 |
+
print(f" • REST API: POST http://{args.host}:{args.port}/v1/decide")
|
| 197 |
+
print(f" • System One Protocol: POST http://{args.host}:{args.port}/v1/systemone")
|
| 198 |
+
print(f" • OpenAI Compatible: POST http://{args.host}:{args.port}/v1/chat/completions")
|
| 199 |
+
print(f" • Health Check: GET http://{args.host}:{args.port}/health\n")
|
| 200 |
+
|
| 201 |
+
uvicorn.run(app, host=args.host, port=args.port)
|
| 202 |
+
|
| 203 |
+
if __name__ == "__main__":
|
| 204 |
+
start_server()
|