fly88oj commited on
Commit
f7673f6
·
verified ·
1 Parent(s): 0293503

Upload test-upload.txt with huggingface_hub

Browse files
Files changed (1) hide show
  1. test-upload.txt +286 -0
test-upload.txt ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Decidex
2
+
3
+ **State in. Typed decisions out. One forward pass.**
4
+
5
+ A local, open reimplementation of the Jev (TypeSafe System One) decision
6
+ model & API — no text generation, no parsing, calibrated probabilities with
7
+ every answer, trained toward the official model's actual outputs.
8
+
9
+ **English** | [简体中文](README.zh-CN.md) | [日本語](README.ja.md) | [Español](README.es.md) | [Français](README.fr.md) | [Deutsch](README.de.md)
10
+
11
+ [Research notes](RESEARCH.md) · Official API reference: docs.typesafe.ai/api · [Replications survey](REPLICATIONS.md) · [Reproduce everything](REPRODUCE.md)
12
+
13
+ **Decidex** is an open, local reimplementation of [TypeSafe AI's "Jev"](https://typesafe.ai/blog/introducing-system-one-models-and-jev):
14
+ **unstructured state in, typed calibrated decisions out.** No text
15
+ generation, no JSON repair, no hallucinated formats — questions and answers
16
+ are typed values you define up front, every question is evaluated
17
+ **independently and in parallel** against the same state, and one call
18
+ returns in milliseconds.
19
+
20
+ ```
21
+ ┌─────────────┐ state (string / JSON) ┌──────────────────┐
22
+ │ your code │ ─────────────────────────► │ Decidex service │
23
+ │ (if/routing)│ questions (Choice/Score/ │ ┌────────────┐ │
24
+ └─────────────┘ Noul, any number) │ │ frozen LM │ │ one forward
25
+ ▲ │ │ direct │ │ pass reads
26
+ │ answers: typed values + probs │ │ logits │ │ probabilities
27
+ └────────────────────────────────────└──└────────────┘──┘
28
+ ```
29
+
30
+ ## Quick start
31
+
32
+ ```bash
33
+ python -m venv .venv
34
+ .venv/Scripts/python -m pip install -e ".[all]"
35
+
36
+ # Dependency-free smoke test (lexical stub engine)
37
+ .venv/Scripts/python -m decidex demo --engine stub
38
+
39
+ # Start the service (default: Qwen3-4B direct-logit readout; downloads the
40
+ # model on first run; point HF_HOME at a large drive)
41
+ HF_HOME=/path/to/hf-cache .venv/Scripts/python -m decidex serve --engine llm --port 8600
42
+ ```
43
+
44
+ Call it — field-for-field identical to the official API:
45
+
46
+ ```bash
47
+ curl -s http://127.0.0.1:8600/v1/systemone -H "Content-Type: application/json" -d '{
48
+ "state": "Help! My payouts have been failing for 3 days.",
49
+ "model": "decidex-latest",
50
+ "questions": {
51
+ "is_urgent": {"type": "noul", "instructions": "Does this convey urgency?"},
52
+ "department": {"type": "choice", "instructions": "Which team should handle this?",
53
+ "criteria": {"billing": "Payments, invoicing, refunds",
54
+ "technical": "Bugs, outages, integrations",
55
+ "sales": "Pricing, upgrades, new accounts"}},
56
+ "frustration": {"type": "score", "instructions": "How frustrated is the customer?",
57
+ "criteria": ["Calm", "Frustrated", "Very angry"]}
58
+ }
59
+ }'
60
+ ```
61
+
62
+ Python SDK — interface-compatible with the official `typesafe_sdk`; change
63
+ the import and base URL and existing code runs against your local server:
64
+
65
+ ```python
66
+ from decidex import Choice, DecidexClient, Noul, Score
67
+
68
+ with DecidexClient() as client: # http://127.0.0.1:8600
69
+ response = client.system_one(
70
+ state={"message": "I was charged twice for order A-104.", "order": {"id": "A-104"}},
71
+ questions={
72
+ "refund_requested": Noul("Does `message` request a refund?"),
73
+ "request_type": Choice("What is the main request in `message`?",
74
+ criteria={"refund": "Wants money returned",
75
+ "information": "Asking a question",
76
+ "other": "Anything else"}),
77
+ "frustration": Score("How frustrated is the customer?",
78
+ criteria=["Calm", "Frustrated", "Very angry"]),
79
+ },
80
+ )
81
+ print(response.answers["refund_requested"].noul) # 0..1
82
+ print(response.answers["request_type"].choice) # "refund"
83
+ print(response.answers["request_type"].confidence) # 0..1
84
+ print(response.answers["frustration"].score) # 0..2, may land between levels
85
+ ```
86
+
87
+ Full worked example (ticket triage with confidence-gated routing, the
88
+ official intent-routing pattern): [`examples/ticket_triage.py`](examples/ticket_triage.py)
89
+
90
+ ## The three primitives
91
+
92
+ | Type | criteria | Returns | Semantics |
93
+ |---|---|---|---|
94
+ | `choice` | `map<option, description|null>`, ≤255 entries | `choice` + `probabilities` + `confidence` | selected option = argmax of probabilities |
95
+ | `score` | 1–26 ordered level descriptions (official prose recommends 2–10) | `score` + `legend` + `probabilities` + `confidence` | `score = Σ i·p_i` (may land between levels) |
96
+ | `noul` | optional `{true, false}` descriptions | `noul` ∈ [0,1] | probability the answer is yes; the value itself is the belief, no confidence |
97
+
98
+ `confidence = clamp((K·p_max − 1)/(K − 1), 0, 1)` — the formula published in
99
+ the interactive demo source of the official docs, verified against both
100
+ worked examples in the official API reference (see
101
+ [RESEARCH.md](RESEARCH.md#12-the-three-primitives-the-whole-api)).
102
+
103
+ ## Compatibility with the official API
104
+
105
+ **Verified drop-in.** Both official SDKs run against a Decidex server by
106
+ changing only the base URL — no other code changes:
107
+
108
+ - **Python** `typesafe-sdk` (tested with 0.7.0): `TypeSafeClient(api_key="x", base_url="http://127.0.0.1:8600")`, or zero code change at all via `TYPESAFE_BASE_URL` + `TYPESAFE_API_KEY` env vars. The official docs' examples work verbatim, including the grouped `.nouls` / `.choices` / `.scores` accessors, dict-style questions, null choice descriptions, and `client.models.list()`.
109
+ - **JavaScript/TypeScript** `@typesafe-ai/sdk` (tested with 0.6.0): `new TypeSafeClient({ apiKey: "x", baseURL: "http://127.0.0.1:8600" })` — `systemOne` / `models.list` / typed answers all pass.
110
+ - Raw HTTP matches the official shapes too, including the FastAPI-style
111
+ `{"detail": [{"loc", "msg", "type"}]}` 422 body (the official contract is
112
+ embedded in `typesafe-sdk` as generated OpenAPI schemas; Decidex follows it).
113
+
114
+ Contract details aligned with the official `openapi.json`:
115
+
116
+ - `POST /v1/systemone`, `GET /v1/models` — same paths, same request/response fields (`model` / `answers` / `usage`).
117
+ - `GET /v1/models` returns the official `ModelMetadataList` shape: `{models: [{name, description, release_date}]}`.
118
+ - Error semantics: `401`, `422` (FastAPI-style detail naming the offending field), `429`/`529` (retryable; the SDK backs off exponentially and honors `Retry-After`).
119
+ - `instructions` is optional on all three question types; noul `criteria.true/false` accept null; score `legend` echoes your criteria entries verbatim (structured values included).
120
+ - Model names: `decidex-latest`/`decidex-1.1.0` plus the official `jev-latest`/`jev-1.13.0` aliases.
121
+ - Validation limits: Choice ≤255 options; Score accepts 1–26 levels (official openapi allows ≥1, docs prose recommends 2–10 — we enforce the letter-readout cap of 26 with a clear error).
122
+
123
+ Run the compatibility check yourself (needs a running server and
124
+ `pip install -e ".[compat]"`):
125
+
126
+ ```bash
127
+ .venv/Scripts/python tests/check_official_sdk_compat.py
128
+ ```
129
+
130
+ ## Architecture
131
+
132
+ ```
133
+ decidex/
134
+ ├── server.py FastAPI: /v1/systemone validation + parallel evaluation + official response shapes
135
+ ├── sdk.py DecidexClient + Choice/Score/Noul (mirrors typesafe_sdk)
136
+ ├── calib.py Official formulas: softmax, confidence, Σi·p_i, distributions summing to 1
137
+ ├── render.py state/instructions/criteria → text (string | object | array)
138
+ ├── cli.py python -m decidex serve | demo
139
+ └── engines/
140
+ ├── llm_logits.py ★ default engine: one forward pass reads option-letter logits from a frozen LM (the SemIf approach)
141
+ ├── embedding.py alternative: sentence-transformer cosine similarity (CPU-friendly, heuristic)
142
+ └── base.py Engine interface + dependency-free lexical stub (for tests)
143
+ ```
144
+
145
+ **The LLM direct-logit engine** (the core reproduction): options are
146
+ labeled `A./B./C.…` in the prompt; a single forward pass reads the logits of
147
+ the option letters at the final position and softmaxes them into
148
+ probabilities — **no tokens are sampled and nothing is generated**, which is
149
+ the mechanical equivalent of Jev's "parallel sampler, free outputs" (the
150
+ community replication SemIf measured the same scheme at 0 output tokens and
151
+ 5.2× faster than autoregressive JSON). All questions in a request share the
152
+ state and are scored in padded parallel batches; beyond 26 options the
153
+ engine switches to per-option relevance probes with renormalization — the
154
+ same two-stage shape TypeSafe describes for high-cardinality choices.
155
+
156
+ ## Measured on the development machine (RTX 5080 / Qwen3-4B)
157
+
158
+ | Scenario | Before optimization | After optimization |
159
+ |---|---|---|
160
+ | Single question (warm p50) | 53 ms | **49 ms** |
161
+ | 10-question parallel fan-out (warm) | 151 ms | **66 ms** |
162
+ | 10 questions over a 3.3k-token document, cold | OOM crash | **2.2 s** |
163
+ | …same document again (prefix cache hit) | — | **193 ms** |
164
+ | 32 concurrent requests | — | all succeed, `/health` p50 2 ms under load, zero 5xx |
165
+
166
+ Accuracy on the 53-item authored benchmark (including hard sarcasm /
167
+ negation / near-miss items): noul 24/24, choice 16/16 (calibration gap
168
+ −0.0009), score 84.6% hard items — 100% with the opt-in
169
+ `ensemble_rounds=3`. Every optimization and rejection is documented with
170
+ measurements in [OPTIMIZATION.md](OPTIMIZATION.md).
171
+
172
+ A measured head-to-head against the official API (via OpenRouter):
173
+ [COMPARISON.md](COMPARISON.md) — choice top-1 agreement 23/23, distribution
174
+ JS divergence 0.0025; documented root causes for the remaining noul/score gaps.
175
+
176
+ ## Configuration
177
+
178
+ | Env var / flag | Default | Notes |
179
+ |---|---|---|
180
+ | `--engine` | `llm` (serve) / `stub` (demo) | `llm` \| `embedding` \| `stub` |
181
+ | `--model` | engine default (Qwen/Qwen3-4B / all-MiniLM-L6-v2) | HF model name or local path; any causal LM works |
182
+ | `--temperature` | 1.0 (llm) / 0.05 (embedding) | probability sharpness; raise to soften overconfident logits |
183
+ | `--device` / `DECIDEX_DEVICE` | auto cuda→cpu | pick a GPU on multi-GPU machines, e.g. `cuda:1` |
184
+ | `--api-key` / `DECIDEX_API_KEY` | none (open locally) | when set, Bearer auth is enforced |
185
+ | `DECIDEX_BASE_URL` | `http://127.0.0.1:8600` | SDK default target |
186
+ | `DECIDEX_MAX_INPUT_TOKENS` | 65536 | server-side context cap (the llm engine additionally enforces its own 8192; the smaller wins) |
187
+ | `DECIDEX_PREFIX_REUSE` | 1 | KV prefix reuse: one forward per shared state instead of one per question |
188
+ | `DECIDEX_PREFIX_CACHE_GB` | 4 | LRU budget (GB) for cross-request state-prefix KV caches; 0 disables |
189
+ | `DECIDEX_MAX_QUEUE` | 8 | max concurrently-waiting evaluations; beyond that, 429 + `Retry-After` |
190
+ | `--lora` / `DECIDEX_LORA` | none | optional LoRA adapter distilled from the official API (see COMPARISON.md §7) |
191
+ | `--dtype` | `auto` | `int8` / `int4` bitsandbytes quantization — fits 7–8B models in 16GB |
192
+
193
+ **Recommended tiers** (measured against the official API, `COMPARISON.md` §9-10):
194
+ latency `Qwen3-4B` (~49ms, decision agreement 0.885); 16GB GPUs
195
+ `Qwen3-8B --dtype int4 --lora benchmarks/adapters/decidex-core-8b` (~80ms,
196
+ 0.923); best agreement on a 24GB GPU `Qwen3-8B --lora
197
+ benchmarks/adapters/decidex-core-8b` (~64ms, total 84/86, choice 16/16,
198
+ score modal 0.909) or `adapters/decidex-true-8b` (noul 52/52 perfect,
199
+ noul MAE 0.061).
200
+ | `HF_HOME` | `~/.cache/huggingface` | model cache; point at a large drive |
201
+
202
+ > Network tip: if PyPI is slow from your network, install with
203
+ > `-i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir`; HuggingFace
204
+ > direct downloads generally work.
205
+
206
+ ## Differences from the official Jev (honest list)
207
+
208
+ | Aspect | Official Jev | Decidex |
209
+ |---|---|---|
210
+ | Model | undisclosed in-house architecture | frozen open LM (default Qwen3-4B; `--model` accepts any causal LM) |
211
+ | Training | RLCD (RL for calibrated decisions) | none; probabilities come from logits + a temperature knob |
212
+ | Calibration | officially calibrated per-answer confidence | single temperature knob; formulas match but calibration quality depends on the base model |
213
+ | Latency | 70–500 ms (dedicated service) | 53 ms single noul / 130 ms 10-question fan-out (measured, 5080 + 4B) |
214
+ | Context | 64k tokens | 8k by default for the llm engine (`max_input_tokens`), 64k server cap |
215
+ | Token accounting | exact | exact when the engine has a tokenizer, else chars/4 estimate |
216
+
217
+ ## Tests
218
+
219
+ ```bash
220
+ .venv/Scripts/python -m pytest tests -m "not slow" # contract+math+SDK, no ML deps, seconds
221
+ .venv/Scripts/python -m pytest tests -m slow # live engine smoke tests (needs the ML stack)
222
+ ```
223
+
224
+ ## GGUF builds (llama.cpp / Ollama / LM Studio)
225
+
226
+ The recommended adapter (v7, merged into Qwen3-8B) ships as GGUF in `gguf/`
227
+ (gitignored — regenerate with the commands below):
228
+
229
+ | File | Size | Best for |
230
+ |---|---|---|
231
+ | `decidex-core-8b-r1-Q4_K_M.gguf` | 5.0 GB | 16GB GPUs / 16GB+ RAM CPU |
232
+ | `decidex-core-8b-r1-Q6_K.gguf` | 6.7 GB | quality-first |
233
+ | `decidex-core-8b-r1-Q8_0.gguf` | 8.7 GB | closest to BF16 behavior |
234
+ | `decidex-core-8b-r1-f16.gguf` | 15.6 GB | master for re-quantizing |
235
+
236
+ Full per-platform usage guide (llama-server with probability readout, Ollama,
237
+ LM Studio): [GGUF-USAGE.md](GGUF-USAGE.md). Ready-made client:
238
+ `examples/gguf_decision_client.py` (verified live: noul 0.9503 / score 1.0404
239
+ vs official docs' 0.95 / 1.05).
240
+
241
+ These are decision-engine models: use them with the same completion prompt
242
+ Decidex's engine renders (state + question + lettered options + `Answer:`),
243
+ greedy decoding, and read the first letter. Serving through Decidex itself
244
+ still uses the HF/PEFT path (`--model <merged> --lora ...`) — the GGUFs are
245
+ for the llama.cpp ecosystem (llama-server/Ollama/LM Studio).
246
+
247
+ Regenerate: merge adapter (`PeftModel.merge_and_unload`) →
248
+ `llama.cpp/convert_hf_to_gguf.py <merged> --outtype f16` →
249
+ `llama-quantize <f16> <out> Q4_K_M|Q6_K|Q8_0`.
250
+
251
+ ### MTP (speculative decoding)
252
+
253
+ Qwen3-8B checkpoints ship no NextN/MTP weights (only Qwen3-Next / Qwen3.5 /
254
+ Qwen3.8-class hybrids do), so MTP for these GGUFs uses llama.cpp's
255
+ draft-model speculative decoding — the same mechanism an MTP head uses
256
+ internally. The repo ships a **distilled draft**: Qwen3-0.6B trained on the
257
+ same decision corpus (letter distribution matched to the target, so draft
258
+ acceptance is high):
259
+
260
+ | File | Size |
261
+ |---|---|
262
+ | `gguf/decidex-draft-0.6b-Q8_0.gguf` | 0.6 GB |
263
+
264
+ Launch any of the three quants with MTP (verified: all three load and
265
+ generate with the draft attached):
266
+
267
+ ```bash
268
+ llama-server -m gguf/decidex-core-8b-r1-Q4_K_M.gguf --model-draft gguf/decidex-draft-0.6b-Q8_0.gguf --spec-draft-n-max 4 -ngl 99 # GPU; tune 2-8
269
+ # same for Q6_K / Q8_0 targets
270
+ ```
271
+
272
+ Honest notes: speculative decoding accelerates **generation**; Decidex's
273
+ native logit-readout mode generates zero tokens, so MTP benefits
274
+ llama-server chat/completions use of the merged model, not the decision
275
+ API itself. Benchmark speedups on your GPU — on CPU the draft adds
276
+ overhead per step.
277
+
278
+ ## Acknowledgments & license
279
+
280
+ - Built from public evidence: TypeSafe AI's docs and blog (API shapes,
281
+ formulas) and the community replication
282
+ [SemIf](https://github.com/TheoLeeCJ/SemIf) (the direct-logit approach).
283
+ This project is not affiliated with TypeSafe AI; Jev and TypeSafe are
284
+ trademarks of their respective owners.
285
+ - Project code: MIT (see [LICENSE](LICENSE)). Contributing:
286
+ [CONTRIBUTING.md](CONTRIBUTING.md). Changes: [CHANGELOG.md](CHANGELOG.md).