Spaces:
Sleeping
Sleeping
Deploy Progression API (llama-cpp-python)
Browse files- .dockerignore +11 -0
- .env.example +12 -0
- .gitignore +7 -0
- Dockerfile +54 -0
- README.md +301 -4
- app/__init__.py +0 -0
- app/config.py +34 -0
- app/json_utils.py +20 -0
- app/main.py +160 -0
- app/model_runtime.py +146 -0
- app/prompts.py +79 -0
- app/schemas.py +59 -0
- requirements-dev.txt +4 -0
- requirements.txt +5 -0
- scripts/local_curl.sh +19 -0
- tests/__init__.py +0 -0
- tests/conftest.py +78 -0
- tests/test_api.py +150 -0
- tests/test_json_utils.py +18 -0
- tests/test_prompt.py +69 -0
.dockerignore
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.git
|
| 2 |
+
.gitignore
|
| 3 |
+
.venv
|
| 4 |
+
venv
|
| 5 |
+
__pycache__
|
| 6 |
+
*.pyc
|
| 7 |
+
*.pyo
|
| 8 |
+
*.pyd
|
| 9 |
+
.env
|
| 10 |
+
.DS_Store
|
| 11 |
+
tests
|
.env.example
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Required on Hugging Face Spaces because the GGUF model repository is private.
|
| 2 |
+
# Locally you can skip this and run `hf auth login` instead.
|
| 3 |
+
HF_TOKEN=hf_your_read_token_here
|
| 4 |
+
|
| 5 |
+
MODEL_REPO_ID=mjpsm/activity-progression-model-600-qwen3.5-0.8b-gguf
|
| 6 |
+
MODEL_FILENAME=activity-progression-model-600-qwen3.5-0.8b-Q8_0.gguf
|
| 7 |
+
MODEL_VERSION=v1
|
| 8 |
+
|
| 9 |
+
LLAMA_CONTEXT_SIZE=2048
|
| 10 |
+
LLAMA_THREADS=2
|
| 11 |
+
# 0 = CPU only. -1 = offload all layers (Metal on Apple Silicon).
|
| 12 |
+
LLAMA_GPU_LAYERS=0
|
.gitignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.venv/
|
| 2 |
+
venv/
|
| 3 |
+
__pycache__/
|
| 4 |
+
*.py[cod]
|
| 5 |
+
.env
|
| 6 |
+
.DS_Store
|
| 7 |
+
.pytest_cache/
|
Dockerfile
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# syntax=docker/dockerfile:1
|
| 2 |
+
|
| 3 |
+
# ============================================================
|
| 4 |
+
# Build Python wheels (llama-cpp-python compiles llama.cpp as a
|
| 5 |
+
# library here; no standalone llama-server is built)
|
| 6 |
+
# ============================================================
|
| 7 |
+
|
| 8 |
+
FROM python:3.11-slim-bookworm AS wheel-builder
|
| 9 |
+
|
| 10 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 11 |
+
build-essential \
|
| 12 |
+
cmake \
|
| 13 |
+
git \
|
| 14 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
+
|
| 16 |
+
# GGML_NATIVE=OFF keeps the build portable across Space CPUs.
|
| 17 |
+
ENV CMAKE_ARGS="-DGGML_NATIVE=OFF -DGGML_OPENMP=ON"
|
| 18 |
+
|
| 19 |
+
WORKDIR /wheels
|
| 20 |
+
|
| 21 |
+
COPY requirements.txt .
|
| 22 |
+
RUN pip wheel --no-cache-dir --wheel-dir /wheels -r requirements.txt
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# ============================================================
|
| 26 |
+
# Runtime image
|
| 27 |
+
# ============================================================
|
| 28 |
+
|
| 29 |
+
FROM python:3.11-slim-bookworm
|
| 30 |
+
|
| 31 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 32 |
+
PYTHONUNBUFFERED=1 \
|
| 33 |
+
HF_HOME=/home/user/.cache/huggingface
|
| 34 |
+
|
| 35 |
+
# Spaces run the container as uid 1000; give it a writable home for the
|
| 36 |
+
# model cache.
|
| 37 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 38 |
+
libgomp1 \
|
| 39 |
+
&& rm -rf /var/lib/apt/lists/* \
|
| 40 |
+
&& useradd -m -u 1000 user
|
| 41 |
+
|
| 42 |
+
WORKDIR /app
|
| 43 |
+
|
| 44 |
+
COPY requirements.txt .
|
| 45 |
+
RUN --mount=type=bind,from=wheel-builder,source=/wheels,target=/wheels \
|
| 46 |
+
pip install --no-cache-dir --no-index --find-links /wheels -r requirements.txt
|
| 47 |
+
|
| 48 |
+
COPY app ./app
|
| 49 |
+
|
| 50 |
+
USER user
|
| 51 |
+
|
| 52 |
+
EXPOSE 7860
|
| 53 |
+
|
| 54 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,10 +1,307 @@
|
|
| 1 |
---
|
| 2 |
-
title: Activity Progression
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: blue
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: MyVillage Activity Progression API
|
| 3 |
+
emoji: 🧭
|
| 4 |
+
colorFrom: indigo
|
| 5 |
colorTo: blue
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# MyVillage Activity Progression API
|
| 12 |
+
|
| 13 |
+
FastAPI inference service that runs the Progression GGUF model **in-process**
|
| 14 |
+
with `llama-cpp-python`.
|
| 15 |
+
|
| 16 |
+
```text
|
| 17 |
+
Uvicorn
|
| 18 |
+
↓
|
| 19 |
+
FastAPI
|
| 20 |
+
↓
|
| 21 |
+
llama-cpp-python
|
| 22 |
+
↓
|
| 23 |
+
Q8_0 GGUF
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
`llama-server` is **no longer used**. There is no subprocess, no internal
|
| 27 |
+
inference port, and no second server to start. Uvicorn is the only process.
|
| 28 |
+
|
| 29 |
+
## Model
|
| 30 |
+
|
| 31 |
+
| | |
|
| 32 |
+
|---|---|
|
| 33 |
+
| Hugging Face repo | `mjpsm/activity-progression-model-600-qwen3.5-0.8b-gguf` |
|
| 34 |
+
| GGUF file | `activity-progression-model-600-qwen3.5-0.8b-Q8_0.gguf` |
|
| 35 |
+
| Quantization | `Q8_0` |
|
| 36 |
+
| Runtime | `llama-cpp-python` |
|
| 37 |
+
|
| 38 |
+
On startup the API downloads the GGUF with `huggingface_hub.hf_hub_download()`
|
| 39 |
+
(cached after the first run) and loads it into memory once. Requests are
|
| 40 |
+
serialized through a single inference lock, and the blocking generation call
|
| 41 |
+
runs in a worker thread so the event loop stays responsive.
|
| 42 |
+
|
| 43 |
+
## Local installation (macOS / Linux)
|
| 44 |
+
|
| 45 |
+
```bash
|
| 46 |
+
python3 -m venv .venv
|
| 47 |
+
source .venv/bin/activate
|
| 48 |
+
|
| 49 |
+
pip install -r requirements.txt
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
### Apple Silicon notes
|
| 53 |
+
|
| 54 |
+
`llama-cpp-python` compiles llama.cpp during `pip install`, so you need the
|
| 55 |
+
Xcode Command Line Tools (`xcode-select --install`). `cmake` is fetched
|
| 56 |
+
automatically by the build.
|
| 57 |
+
|
| 58 |
+
- Use a native **arm64** Python (`python3 -c "import platform; print(platform.machine())"`
|
| 59 |
+
should print `arm64`). An x86_64/Rosetta Python produces a much slower build.
|
| 60 |
+
- The default macOS build already includes Metal support. The API still runs
|
| 61 |
+
on **CPU by default** (`LLAMA_GPU_LAYERS=0`). To use Metal:
|
| 62 |
+
|
| 63 |
+
```bash
|
| 64 |
+
LLAMA_GPU_LAYERS=-1 python -m uvicorn app.main:app --host 0.0.0.0 --port 8000
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
- If your install was built without Metal and you want it, rebuild:
|
| 68 |
+
|
| 69 |
+
```bash
|
| 70 |
+
CMAKE_ARGS="-DGGML_METAL=on" pip install --force-reinstall --no-cache-dir llama-cpp-python
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
## Hugging Face authentication
|
| 74 |
+
|
| 75 |
+
The model repo is currently public, so no authentication is needed. If it is
|
| 76 |
+
made private again, either log in once:
|
| 77 |
+
|
| 78 |
+
```bash
|
| 79 |
+
hf auth login
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
or export a token with read access to the repo:
|
| 83 |
+
|
| 84 |
+
```bash
|
| 85 |
+
export HF_TOKEN=hf_...
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
No token is stored in this repository. Do not commit `.env`.
|
| 89 |
+
|
| 90 |
+
## Run
|
| 91 |
+
|
| 92 |
+
```bash
|
| 93 |
+
python -m uvicorn app.main:app --host 0.0.0.0 --port 8000
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
That is the only command. Check readiness:
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
curl -s http://localhost:8000/health | python3 -m json.tool
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
```json
|
| 103 |
+
{
|
| 104 |
+
"status": "ok",
|
| 105 |
+
"model_ready": true
|
| 106 |
+
}
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
If the model failed to download or load, the API still starts, logs the
|
| 110 |
+
error, and reports `"status": "degraded", "model_ready": false`;
|
| 111 |
+
`/v1/progression/generate` then returns `503`.
|
| 112 |
+
|
| 113 |
+
## Local curl test
|
| 114 |
+
|
| 115 |
+
```bash
|
| 116 |
+
curl -s -X POST "http://localhost:8000/v1/progression/generate" -H "Content-Type: application/json" -d '{
|
| 117 |
+
"version": "v1",
|
| 118 |
+
"knowledge_submission": "I created the video and showed the finished result first. It received 1,800 views and more engagement than my previous videos. I noticed viewers stayed longer when the final result appeared before the process.",
|
| 119 |
+
"wisdom": {
|
| 120 |
+
"content": "When I presented in class, showing the most interesting result before explaining the process got peoples attention.",
|
| 121 |
+
"chapterTitle": "Lead With the Result",
|
| 122 |
+
"BookTypeName": "Testimony",
|
| 123 |
+
"BookType": "Personal Record"
|
| 124 |
+
},
|
| 125 |
+
"village_goal": "Learn how to create engaging social media content",
|
| 126 |
+
"previous_activity_title": "Test a stronger opening hook"
|
| 127 |
+
}' | python3 -m json.tool
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
The same request is in `scripts/local_curl.sh`.
|
| 131 |
+
|
| 132 |
+
## Routes
|
| 133 |
+
|
| 134 |
+
- `GET /`
|
| 135 |
+
- `GET /health`
|
| 136 |
+
- `GET /models`
|
| 137 |
+
- `GET /docs`
|
| 138 |
+
- `POST /v1/progression/generate`
|
| 139 |
+
|
| 140 |
+
## Request schema
|
| 141 |
+
|
| 142 |
+
```json
|
| 143 |
+
{
|
| 144 |
+
"version": "v1",
|
| 145 |
+
"knowledge_submission": "string",
|
| 146 |
+
"wisdom": {
|
| 147 |
+
"content": "string",
|
| 148 |
+
"chapterTitle": "string",
|
| 149 |
+
"BookTypeName": "string",
|
| 150 |
+
"BookType": "string"
|
| 151 |
+
},
|
| 152 |
+
"village_goal": "string",
|
| 153 |
+
"previous_activity_title": "string"
|
| 154 |
+
}
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
- The `wisdom` field names and capitalization are exact.
|
| 158 |
+
- Unknown fields are rejected with `422`.
|
| 159 |
+
- An unknown `version` returns `404`.
|
| 160 |
+
|
| 161 |
+
### Generation parameters are internal
|
| 162 |
+
|
| 163 |
+
Generation settings are **not** part of the public API; a request containing
|
| 164 |
+
a `generation` field is rejected. They are fixed in `app/config.py`:
|
| 165 |
+
|
| 166 |
+
| Setting | Value |
|
| 167 |
+
|---|---|
|
| 168 |
+
| `max_new_tokens` | `450` |
|
| 169 |
+
| `temperature` | `0.0` |
|
| 170 |
+
| `top_p` | `1.0` |
|
| 171 |
+
| `repeat_penalty` | `1.08` |
|
| 172 |
+
| `seed` | `42` |
|
| 173 |
+
|
| 174 |
+
## Response schema
|
| 175 |
+
|
| 176 |
+
```json
|
| 177 |
+
{
|
| 178 |
+
"progression": {
|
| 179 |
+
"knowledge_state": "COMPLETED | PARTIAL | UNCLEAR | BLOCKED | ADVANCED",
|
| 180 |
+
"demonstrated_state": ["string"],
|
| 181 |
+
"next_gap": "string",
|
| 182 |
+
"activity_plan": {
|
| 183 |
+
"action": "string",
|
| 184 |
+
"target": "string",
|
| 185 |
+
"avoid_repeating": ["string"]
|
| 186 |
+
}
|
| 187 |
+
},
|
| 188 |
+
"usage": {
|
| 189 |
+
"input_tokens": 312,
|
| 190 |
+
"output_tokens": 147
|
| 191 |
+
}
|
| 192 |
+
}
|
| 193 |
+
```
|
| 194 |
+
|
| 195 |
+
`usage` is counted with the loaded model's own tokenizer:
|
| 196 |
+
|
| 197 |
+
- `input_tokens` — tokens in the final raw SFT prompt sent to the model
|
| 198 |
+
- `output_tokens` — tokens in the generated completion
|
| 199 |
+
|
| 200 |
+
Model metadata is not returned here; use `GET /models`.
|
| 201 |
+
|
| 202 |
+
If the model output contains no parseable JSON object, or the JSON does not
|
| 203 |
+
match the Progression schema, the API returns `502` with the raw output for
|
| 204 |
+
debugging. Malformed output is never silently repaired.
|
| 205 |
+
|
| 206 |
+
## Prompt format
|
| 207 |
+
|
| 208 |
+
The model was fine-tuned on a raw completion prompt, so the API uses raw
|
| 209 |
+
completion (never a chat template or chat-completion API).
|
| 210 |
+
|
| 211 |
+
`previous_activity_title` is converted to the structure used for SFT:
|
| 212 |
+
|
| 213 |
+
```json
|
| 214 |
+
{
|
| 215 |
+
"village_goal": "...",
|
| 216 |
+
"previous_activity": {
|
| 217 |
+
"title": "..."
|
| 218 |
+
},
|
| 219 |
+
"knowledge_submission": "...",
|
| 220 |
+
"wisdom": {
|
| 221 |
+
"content": "...",
|
| 222 |
+
"chapterTitle": "...",
|
| 223 |
+
"BookTypeName": "...",
|
| 224 |
+
"BookType": "..."
|
| 225 |
+
}
|
| 226 |
+
}
|
| 227 |
+
```
|
| 228 |
+
|
| 229 |
+
and serialized as compact JSON into:
|
| 230 |
+
|
| 231 |
+
```text
|
| 232 |
+
<SYSTEM_INSTRUCTION>
|
| 233 |
+
|
| 234 |
+
INPUT:
|
| 235 |
+
<compact JSON>
|
| 236 |
+
|
| 237 |
+
OUTPUT:
|
| 238 |
+
```
|
| 239 |
+
|
| 240 |
+
Do not change this format unless the model is retrained and re-evaluated
|
| 241 |
+
with the new one.
|
| 242 |
+
|
| 243 |
+
## Configuration
|
| 244 |
+
|
| 245 |
+
Optional environment variables (see `.env.example`):
|
| 246 |
+
|
| 247 |
+
| Variable | Default | Purpose |
|
| 248 |
+
|---|---|---|
|
| 249 |
+
| `HF_TOKEN` | unset | Read token, only needed if the model repo is private |
|
| 250 |
+
| `LLAMA_CONTEXT_SIZE` | `2048` | Context window |
|
| 251 |
+
| `LLAMA_THREADS` | `2` | CPU threads for generation |
|
| 252 |
+
| `LLAMA_GPU_LAYERS` | `0` | Layers to offload (`-1` = all) |
|
| 253 |
+
|
| 254 |
+
## Tests
|
| 255 |
+
|
| 256 |
+
```bash
|
| 257 |
+
pip install -r requirements-dev.txt
|
| 258 |
+
python -m pytest
|
| 259 |
+
```
|
| 260 |
+
|
| 261 |
+
The tests use a fake runtime and do not download or load the model.
|
| 262 |
+
|
| 263 |
+
## Docker / Hugging Face Space deployment
|
| 264 |
+
|
| 265 |
+
The image contains only Uvicorn + FastAPI + `llama-cpp-python` and listens
|
| 266 |
+
on port `7860`.
|
| 267 |
+
|
| 268 |
+
Local Docker run:
|
| 269 |
+
|
| 270 |
+
```bash
|
| 271 |
+
docker build -t progression-api .
|
| 272 |
+
docker run --rm -p 7860:7860 progression-api
|
| 273 |
+
```
|
| 274 |
+
|
| 275 |
+
### Space setup
|
| 276 |
+
|
| 277 |
+
1. Create a **Docker Space**.
|
| 278 |
+
2. Only if the model repo is private, add this secret under
|
| 279 |
+
**Space → Settings → Variables and secrets → New secret**:
|
| 280 |
+
|
| 281 |
+
```text
|
| 282 |
+
HF_TOKEN=<Hugging Face token with read access to the private model repo>
|
| 283 |
+
```
|
| 284 |
+
|
| 285 |
+
3. Push this repo to the Space:
|
| 286 |
+
|
| 287 |
+
```bash
|
| 288 |
+
git clone https://huggingface.co/spaces/mjpsm/YOUR-SPACE-NAME
|
| 289 |
+
cd YOUR-SPACE-NAME
|
| 290 |
+
# copy this repo's files in, then:
|
| 291 |
+
git add .
|
| 292 |
+
git commit -m "Deploy Progression API (llama-cpp-python)"
|
| 293 |
+
git push
|
| 294 |
+
```
|
| 295 |
+
|
| 296 |
+
Hugging Face builds the Docker image and exposes port `7860`.
|
| 297 |
+
|
| 298 |
+
```bash
|
| 299 |
+
curl -s "https://YOUR-SPACE-HOST/health" | python3 -m json.tool
|
| 300 |
+
```
|
| 301 |
+
|
| 302 |
+
## CPU behavior
|
| 303 |
+
|
| 304 |
+
One model instance is loaded and generation is serialized through one
|
| 305 |
+
inference lock, so concurrent requests queue rather than compete for
|
| 306 |
+
memory/compute on a small Space. Benchmark real Progression requests before
|
| 307 |
+
raising `LLAMA_THREADS`.
|
app/__init__.py
ADDED
|
File without changes
|
app/config.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
MODEL_REPO_ID = os.getenv(
|
| 5 |
+
"MODEL_REPO_ID",
|
| 6 |
+
"mjpsm/activity-progression-model-600-qwen3.5-0.8b-gguf",
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
MODEL_FILENAME = os.getenv(
|
| 10 |
+
"MODEL_FILENAME",
|
| 11 |
+
"activity-progression-model-600-qwen3.5-0.8b-Q8_0.gguf",
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
MODEL_VERSION = os.getenv("MODEL_VERSION", "v1")
|
| 15 |
+
QUANTIZATION = "Q8_0"
|
| 16 |
+
RUNTIME_NAME = "llama-cpp-python"
|
| 17 |
+
PROMPT_FORMAT = "raw_sft_completion"
|
| 18 |
+
|
| 19 |
+
# Optional. When unset, huggingface_hub falls back to the token stored by
|
| 20 |
+
# `hf auth login`, which is what local development uses.
|
| 21 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 22 |
+
|
| 23 |
+
LLAMA_CONTEXT_SIZE = int(os.getenv("LLAMA_CONTEXT_SIZE", "2048"))
|
| 24 |
+
LLAMA_THREADS = int(os.getenv("LLAMA_THREADS", "2"))
|
| 25 |
+
# 0 = CPU only. Set to -1 to offload every layer (e.g. Metal on Apple Silicon).
|
| 26 |
+
LLAMA_GPU_LAYERS = int(os.getenv("LLAMA_GPU_LAYERS", "0"))
|
| 27 |
+
|
| 28 |
+
# Internal generation settings. These match the settings the model was
|
| 29 |
+
# evaluated with and are deliberately NOT part of the public request schema.
|
| 30 |
+
MAX_NEW_TOKENS = 450
|
| 31 |
+
TEMPERATURE = 0.0
|
| 32 |
+
TOP_P = 1.0
|
| 33 |
+
REPEAT_PENALTY = 1.08
|
| 34 |
+
SEED = 42
|
app/json_utils.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def extract_json(text: str) -> dict | None:
|
| 6 |
+
"""
|
| 7 |
+
Match the JSON extraction logic used during checkpoint evaluation:
|
| 8 |
+
find the first decodable JSON object in the generated text.
|
| 9 |
+
"""
|
| 10 |
+
decoder = json.JSONDecoder()
|
| 11 |
+
|
| 12 |
+
for match in re.finditer(r"\{", text):
|
| 13 |
+
try:
|
| 14 |
+
obj, _ = decoder.raw_decode(text[match.start():])
|
| 15 |
+
if isinstance(obj, dict):
|
| 16 |
+
return obj
|
| 17 |
+
except json.JSONDecodeError:
|
| 18 |
+
pass
|
| 19 |
+
|
| 20 |
+
return None
|
app/main.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from contextlib import asynccontextmanager
|
| 4 |
+
|
| 5 |
+
from fastapi import FastAPI, HTTPException
|
| 6 |
+
|
| 7 |
+
from app.config import (
|
| 8 |
+
MODEL_FILENAME,
|
| 9 |
+
MODEL_REPO_ID,
|
| 10 |
+
MODEL_VERSION,
|
| 11 |
+
PROMPT_FORMAT,
|
| 12 |
+
QUANTIZATION,
|
| 13 |
+
RUNTIME_NAME,
|
| 14 |
+
)
|
| 15 |
+
from app.json_utils import extract_json
|
| 16 |
+
from app.model_runtime import ModelNotReadyError, runtime
|
| 17 |
+
from app.prompts import build_prompt
|
| 18 |
+
from app.schemas import (
|
| 19 |
+
ProgressionModelOutput,
|
| 20 |
+
ProgressionRequest,
|
| 21 |
+
ProgressionResponse,
|
| 22 |
+
Usage,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@asynccontextmanager
|
| 27 |
+
async def lifespan(app: FastAPI):
|
| 28 |
+
await runtime.start()
|
| 29 |
+
yield
|
| 30 |
+
await runtime.stop()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
app = FastAPI(
|
| 34 |
+
title="MyVillage Activity Progression API",
|
| 35 |
+
version="2.0.0",
|
| 36 |
+
description=(
|
| 37 |
+
"In-process GGUF inference API (llama-cpp-python) for the "
|
| 38 |
+
"MyVillage Activity Progression Model."
|
| 39 |
+
),
|
| 40 |
+
lifespan=lifespan,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
@app.get("/")
|
| 45 |
+
async def root():
|
| 46 |
+
return {
|
| 47 |
+
"name": "MyVillage Activity Progression API",
|
| 48 |
+
"status": "running",
|
| 49 |
+
"model": MODEL_REPO_ID,
|
| 50 |
+
"model_file": MODEL_FILENAME,
|
| 51 |
+
"quantization": QUANTIZATION,
|
| 52 |
+
"runtime": RUNTIME_NAME,
|
| 53 |
+
"docs": "/docs",
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@app.get("/health")
|
| 58 |
+
async def health():
|
| 59 |
+
model_ready = runtime.ready()
|
| 60 |
+
|
| 61 |
+
return {
|
| 62 |
+
"status": "ok" if model_ready else "degraded",
|
| 63 |
+
"model_ready": model_ready,
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
@app.get("/models")
|
| 68 |
+
async def models():
|
| 69 |
+
return {
|
| 70 |
+
"progression": {
|
| 71 |
+
MODEL_VERSION: {
|
| 72 |
+
"repo_id": MODEL_REPO_ID,
|
| 73 |
+
"filename": MODEL_FILENAME,
|
| 74 |
+
"quantization": QUANTIZATION,
|
| 75 |
+
"runtime": RUNTIME_NAME,
|
| 76 |
+
"prompt_format": PROMPT_FORMAT,
|
| 77 |
+
"status": (
|
| 78 |
+
"ready"
|
| 79 |
+
if runtime.ready()
|
| 80 |
+
else "not_ready"
|
| 81 |
+
),
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@app.post(
|
| 88 |
+
"/v1/progression/generate",
|
| 89 |
+
response_model=ProgressionResponse,
|
| 90 |
+
)
|
| 91 |
+
async def generate_progression(
|
| 92 |
+
req: ProgressionRequest,
|
| 93 |
+
):
|
| 94 |
+
if req.version != MODEL_VERSION:
|
| 95 |
+
raise HTTPException(
|
| 96 |
+
status_code=404,
|
| 97 |
+
detail=(
|
| 98 |
+
f"Unknown progression version '{req.version}'. "
|
| 99 |
+
f"Available version: '{MODEL_VERSION}'."
|
| 100 |
+
),
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
if not runtime.ready():
|
| 104 |
+
raise HTTPException(
|
| 105 |
+
status_code=503,
|
| 106 |
+
detail="Progression model is not ready.",
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
prompt = build_prompt(req)
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
completion = await runtime.complete(prompt)
|
| 113 |
+
except ModelNotReadyError as exc:
|
| 114 |
+
raise HTTPException(
|
| 115 |
+
status_code=503,
|
| 116 |
+
detail="Progression model is not ready.",
|
| 117 |
+
) from exc
|
| 118 |
+
except Exception as exc:
|
| 119 |
+
raise HTTPException(
|
| 120 |
+
status_code=502,
|
| 121 |
+
detail=f"Model generation failed: {exc}",
|
| 122 |
+
) from exc
|
| 123 |
+
|
| 124 |
+
raw_output = completion.text
|
| 125 |
+
parsed = extract_json(raw_output)
|
| 126 |
+
|
| 127 |
+
if parsed is None:
|
| 128 |
+
raise HTTPException(
|
| 129 |
+
status_code=502,
|
| 130 |
+
detail={
|
| 131 |
+
"message": (
|
| 132 |
+
"Model generation did not contain parseable JSON."
|
| 133 |
+
),
|
| 134 |
+
"raw_output": raw_output,
|
| 135 |
+
},
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
try:
|
| 139 |
+
validated = ProgressionModelOutput.model_validate(parsed)
|
| 140 |
+
except Exception as exc:
|
| 141 |
+
raise HTTPException(
|
| 142 |
+
status_code=502,
|
| 143 |
+
detail={
|
| 144 |
+
"message": (
|
| 145 |
+
"Model returned JSON that did not match the "
|
| 146 |
+
"Progression schema."
|
| 147 |
+
),
|
| 148 |
+
"raw_output": raw_output,
|
| 149 |
+
"parsed_output": parsed,
|
| 150 |
+
"validation_error": str(exc),
|
| 151 |
+
},
|
| 152 |
+
) from exc
|
| 153 |
+
|
| 154 |
+
return ProgressionResponse(
|
| 155 |
+
progression=validated.progression,
|
| 156 |
+
usage=Usage(
|
| 157 |
+
input_tokens=completion.input_tokens,
|
| 158 |
+
output_tokens=completion.output_tokens,
|
| 159 |
+
),
|
| 160 |
+
)
|
app/model_runtime.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import asyncio
|
| 4 |
+
import logging
|
| 5 |
+
import threading
|
| 6 |
+
from dataclasses import dataclass
|
| 7 |
+
|
| 8 |
+
from huggingface_hub import hf_hub_download
|
| 9 |
+
from llama_cpp import Llama
|
| 10 |
+
|
| 11 |
+
from app.config import (
|
| 12 |
+
HF_TOKEN,
|
| 13 |
+
LLAMA_CONTEXT_SIZE,
|
| 14 |
+
LLAMA_GPU_LAYERS,
|
| 15 |
+
LLAMA_THREADS,
|
| 16 |
+
MAX_NEW_TOKENS,
|
| 17 |
+
MODEL_FILENAME,
|
| 18 |
+
MODEL_REPO_ID,
|
| 19 |
+
REPEAT_PENALTY,
|
| 20 |
+
SEED,
|
| 21 |
+
TEMPERATURE,
|
| 22 |
+
TOP_P,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
logger = logging.getLogger(__name__)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class ModelNotReadyError(RuntimeError):
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@dataclass(frozen=True)
|
| 33 |
+
class Completion:
|
| 34 |
+
text: str
|
| 35 |
+
input_tokens: int
|
| 36 |
+
output_tokens: int
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class ModelRuntime:
|
| 40 |
+
"""Owns the single in-process GGUF model instance."""
|
| 41 |
+
|
| 42 |
+
def __init__(self) -> None:
|
| 43 |
+
self.llm: Llama | None = None
|
| 44 |
+
self.model_path: str | None = None
|
| 45 |
+
self.load_error: str | None = None
|
| 46 |
+
# A thread lock (not asyncio.Lock) because generation runs in a
|
| 47 |
+
# worker thread; a Llama instance is not safe for concurrent use.
|
| 48 |
+
self.inference_lock = threading.Lock()
|
| 49 |
+
|
| 50 |
+
def download_model(self) -> str:
|
| 51 |
+
path = hf_hub_download(
|
| 52 |
+
repo_id=MODEL_REPO_ID,
|
| 53 |
+
filename=MODEL_FILENAME,
|
| 54 |
+
repo_type="model",
|
| 55 |
+
token=HF_TOKEN or None,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
self.model_path = path
|
| 59 |
+
return path
|
| 60 |
+
|
| 61 |
+
def load(self) -> None:
|
| 62 |
+
model_path = self.download_model()
|
| 63 |
+
|
| 64 |
+
self.llm = Llama(
|
| 65 |
+
model_path=model_path,
|
| 66 |
+
n_ctx=LLAMA_CONTEXT_SIZE,
|
| 67 |
+
n_threads=LLAMA_THREADS,
|
| 68 |
+
n_gpu_layers=LLAMA_GPU_LAYERS,
|
| 69 |
+
verbose=False,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
async def start(self) -> None:
|
| 73 |
+
"""
|
| 74 |
+
Download and load the model once.
|
| 75 |
+
|
| 76 |
+
A failure is recorded rather than raised so the API still starts
|
| 77 |
+
and /health can report the model as not ready.
|
| 78 |
+
"""
|
| 79 |
+
try:
|
| 80 |
+
await asyncio.to_thread(self.load)
|
| 81 |
+
self.load_error = None
|
| 82 |
+
except Exception as exc:
|
| 83 |
+
self.llm = None
|
| 84 |
+
self.load_error = f"{type(exc).__name__}: {exc}"
|
| 85 |
+
logger.exception("Failed to load the Progression model.")
|
| 86 |
+
|
| 87 |
+
async def stop(self) -> None:
|
| 88 |
+
llm, self.llm = self.llm, None
|
| 89 |
+
|
| 90 |
+
if llm is not None:
|
| 91 |
+
with self.inference_lock:
|
| 92 |
+
llm.close()
|
| 93 |
+
|
| 94 |
+
def ready(self) -> bool:
|
| 95 |
+
return self.llm is not None
|
| 96 |
+
|
| 97 |
+
def _tokenize(self, llm: Llama, text: str, *, prompt: bool) -> list[int]:
|
| 98 |
+
# Prompt tokenization mirrors what Llama.create_completion does
|
| 99 |
+
# internally (BOS handling + special-token parsing), so the count
|
| 100 |
+
# equals what the model actually evaluates.
|
| 101 |
+
return llm.tokenize(
|
| 102 |
+
text.encode("utf-8"),
|
| 103 |
+
add_bos=prompt,
|
| 104 |
+
special=prompt,
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
def _complete(self, prompt: str) -> Completion:
|
| 108 |
+
with self.inference_lock:
|
| 109 |
+
llm = self.llm
|
| 110 |
+
|
| 111 |
+
if llm is None:
|
| 112 |
+
raise ModelNotReadyError("Progression model is not loaded.")
|
| 113 |
+
|
| 114 |
+
input_tokens = len(self._tokenize(llm, prompt, prompt=True))
|
| 115 |
+
|
| 116 |
+
result = llm.create_completion(
|
| 117 |
+
prompt,
|
| 118 |
+
max_tokens=MAX_NEW_TOKENS,
|
| 119 |
+
temperature=TEMPERATURE,
|
| 120 |
+
top_p=TOP_P,
|
| 121 |
+
repeat_penalty=REPEAT_PENALTY,
|
| 122 |
+
seed=SEED,
|
| 123 |
+
echo=False,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
text = result["choices"][0]["text"]
|
| 127 |
+
output_tokens = len(self._tokenize(llm, text, prompt=False))
|
| 128 |
+
|
| 129 |
+
return Completion(
|
| 130 |
+
text=text.strip(),
|
| 131 |
+
input_tokens=input_tokens,
|
| 132 |
+
output_tokens=output_tokens,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
async def complete(self, prompt: str) -> Completion:
|
| 136 |
+
"""
|
| 137 |
+
Raw completion (no chat template) with the internal generation
|
| 138 |
+
settings.
|
| 139 |
+
|
| 140 |
+
The blocking llama.cpp call runs in a worker thread so the event
|
| 141 |
+
loop stays responsive; the lock serializes access to the model.
|
| 142 |
+
"""
|
| 143 |
+
return await asyncio.to_thread(self._complete, prompt)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
runtime = ModelRuntime()
|
app/prompts.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from app.schemas import ProgressionRequest
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
# Exact system instruction used by the Progression fine-tuning workflow.
|
| 6 |
+
SYSTEM_INSTRUCTION = """You are the MyVillage Activity Progression Model.
|
| 7 |
+
Determine the villager's progression from the provided input and return ONLY valid JSON.
|
| 8 |
+
|
| 9 |
+
Required output schema:
|
| 10 |
+
{
|
| 11 |
+
"progression": {
|
| 12 |
+
"knowledge_state": "COMPLETED | PARTIAL | UNCLEAR | BLOCKED | ADVANCED",
|
| 13 |
+
"demonstrated_state": ["string"],
|
| 14 |
+
"next_gap": "string",
|
| 15 |
+
"activity_plan": {
|
| 16 |
+
"action": "string",
|
| 17 |
+
"target": "string",
|
| 18 |
+
"avoid_repeating": ["string"]
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
Rules:
|
| 24 |
+
- Ground demonstrated_state only in the current knowledge submission.
|
| 25 |
+
- Planned future work is not demonstrated work.
|
| 26 |
+
- Preserve observation as observation unless understanding is actually shown.
|
| 27 |
+
- Use one primary next_gap.
|
| 28 |
+
- Use one major activity_plan.action.
|
| 29 |
+
- Do not repeat work already demonstrated.
|
| 30 |
+
- Wisdom is an older personal record from prior learning, not current-session evidence.
|
| 31 |
+
- Wisdom may inform a useful next step when relevant, but must never be invented as current demonstrated work.
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def compact_json(value: object) -> str:
|
| 36 |
+
"""Match the SFT compact-JSON formatting exactly."""
|
| 37 |
+
return json.dumps(
|
| 38 |
+
value,
|
| 39 |
+
ensure_ascii=False,
|
| 40 |
+
separators=(",", ":"),
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def training_input(req: ProgressionRequest) -> dict:
|
| 45 |
+
"""
|
| 46 |
+
Convert the clean API schema into the exact model input structure.
|
| 47 |
+
|
| 48 |
+
Training used:
|
| 49 |
+
village_goal
|
| 50 |
+
previous_activity: {"title": "..."}
|
| 51 |
+
knowledge_submission
|
| 52 |
+
wisdom
|
| 53 |
+
"""
|
| 54 |
+
return {
|
| 55 |
+
"village_goal": req.village_goal,
|
| 56 |
+
"previous_activity": {
|
| 57 |
+
"title": req.previous_activity_title,
|
| 58 |
+
},
|
| 59 |
+
"knowledge_submission": req.knowledge_submission,
|
| 60 |
+
"wisdom": req.wisdom.model_dump(),
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def build_prompt(req: ProgressionRequest) -> str:
|
| 65 |
+
"""
|
| 66 |
+
Exact raw-completion prompt format used during SFT.
|
| 67 |
+
|
| 68 |
+
IMPORTANT:
|
| 69 |
+
This model was not trained using an inference chat wrapper here.
|
| 70 |
+
Do not replace this with apply_chat_template/system+user messages.
|
| 71 |
+
"""
|
| 72 |
+
input_obj = training_input(req)
|
| 73 |
+
|
| 74 |
+
return (
|
| 75 |
+
SYSTEM_INSTRUCTION.strip()
|
| 76 |
+
+ "\n\nINPUT:\n"
|
| 77 |
+
+ compact_json(input_obj)
|
| 78 |
+
+ "\n\nOUTPUT:\n"
|
| 79 |
+
)
|
app/schemas.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Literal
|
| 2 |
+
from pydantic import BaseModel, ConfigDict, Field
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
KnowledgeState = Literal[
|
| 6 |
+
"COMPLETED",
|
| 7 |
+
"PARTIAL",
|
| 8 |
+
"UNCLEAR",
|
| 9 |
+
"BLOCKED",
|
| 10 |
+
"ADVANCED",
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class Wisdom(BaseModel):
|
| 15 |
+
# Field names and capitalization are part of the model's input format.
|
| 16 |
+
model_config = ConfigDict(extra="forbid")
|
| 17 |
+
|
| 18 |
+
content: str
|
| 19 |
+
chapterTitle: str
|
| 20 |
+
BookTypeName: str
|
| 21 |
+
BookType: str
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class ProgressionRequest(BaseModel):
|
| 25 |
+
# Unknown fields (e.g. generation settings) are rejected.
|
| 26 |
+
model_config = ConfigDict(extra="forbid")
|
| 27 |
+
|
| 28 |
+
version: str = "v1"
|
| 29 |
+
knowledge_submission: str = Field(min_length=1)
|
| 30 |
+
wisdom: Wisdom
|
| 31 |
+
village_goal: str = Field(min_length=1)
|
| 32 |
+
previous_activity_title: str = Field(min_length=1)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class ActivityPlan(BaseModel):
|
| 36 |
+
action: str
|
| 37 |
+
target: str
|
| 38 |
+
avoid_repeating: list[str]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class Progression(BaseModel):
|
| 42 |
+
knowledge_state: KnowledgeState
|
| 43 |
+
demonstrated_state: list[str]
|
| 44 |
+
next_gap: str
|
| 45 |
+
activity_plan: ActivityPlan
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class ProgressionModelOutput(BaseModel):
|
| 49 |
+
progression: Progression
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class Usage(BaseModel):
|
| 53 |
+
input_tokens: int
|
| 54 |
+
output_tokens: int
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class ProgressionResponse(BaseModel):
|
| 58 |
+
progression: Progression
|
| 59 |
+
usage: Usage
|
requirements-dev.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-r requirements.txt
|
| 2 |
+
pytest>=8,<10
|
| 3 |
+
# Required by fastapi.testclient only; the API itself makes no HTTP calls.
|
| 4 |
+
httpx>=0.28,<1
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.118.0
|
| 2 |
+
uvicorn[standard]==0.37.0
|
| 3 |
+
huggingface_hub>=0.35.3,<3
|
| 4 |
+
pydantic>=2.11,<3
|
| 5 |
+
llama-cpp-python>=0.3.36,<0.4
|
scripts/local_curl.sh
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -e
|
| 3 |
+
|
| 4 |
+
BASE_URL="${BASE_URL:-http://localhost:8000}"
|
| 5 |
+
|
| 6 |
+
curl -s -X POST "${BASE_URL}/v1/progression/generate" \
|
| 7 |
+
-H "Content-Type: application/json" \
|
| 8 |
+
-d '{
|
| 9 |
+
"version": "v1",
|
| 10 |
+
"knowledge_submission": "I created the video and showed the finished result first. It received 1,800 views and more engagement than my previous videos. I noticed viewers stayed longer when the final result appeared before the process.",
|
| 11 |
+
"wisdom": {
|
| 12 |
+
"content": "When I presented in class, showing the most interesting result before explaining the process got peoples attention.",
|
| 13 |
+
"chapterTitle": "Lead With the Result",
|
| 14 |
+
"BookTypeName": "Testimony",
|
| 15 |
+
"BookType": "Personal Record"
|
| 16 |
+
},
|
| 17 |
+
"village_goal": "Learn how to create engaging social media content",
|
| 18 |
+
"previous_activity_title": "Test a stronger opening hook"
|
| 19 |
+
}' | python3 -m json.tool
|
tests/__init__.py
ADDED
|
File without changes
|
tests/conftest.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
from fastapi.testclient import TestClient
|
| 5 |
+
|
| 6 |
+
from app import main
|
| 7 |
+
from app.model_runtime import Completion
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
VALID_REQUEST = {
|
| 11 |
+
"version": "v1",
|
| 12 |
+
"knowledge_submission": "I completed the task.",
|
| 13 |
+
"wisdom": {
|
| 14 |
+
"content": "I learned something before.",
|
| 15 |
+
"chapterTitle": "Past learning",
|
| 16 |
+
"BookTypeName": "Testimony",
|
| 17 |
+
"BookType": "Personal Record",
|
| 18 |
+
},
|
| 19 |
+
"village_goal": "Learn the skill",
|
| 20 |
+
"previous_activity_title": "Practice the skill",
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
VALID_OUTPUT = {
|
| 24 |
+
"progression": {
|
| 25 |
+
"knowledge_state": "COMPLETED",
|
| 26 |
+
"demonstrated_state": ["Completed the task"],
|
| 27 |
+
"next_gap": "Apply it in a new setting",
|
| 28 |
+
"activity_plan": {
|
| 29 |
+
"action": "Apply",
|
| 30 |
+
"target": "A new setting",
|
| 31 |
+
"avoid_repeating": ["Completing the same task"],
|
| 32 |
+
},
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class FakeRuntime:
|
| 38 |
+
"""Stands in for ModelRuntime so tests never load the GGUF."""
|
| 39 |
+
|
| 40 |
+
def __init__(self) -> None:
|
| 41 |
+
self.is_ready = True
|
| 42 |
+
self.output = json.dumps(VALID_OUTPUT)
|
| 43 |
+
self.prompts: list[str] = []
|
| 44 |
+
|
| 45 |
+
async def start(self) -> None:
|
| 46 |
+
pass
|
| 47 |
+
|
| 48 |
+
async def stop(self) -> None:
|
| 49 |
+
pass
|
| 50 |
+
|
| 51 |
+
def ready(self) -> bool:
|
| 52 |
+
return self.is_ready
|
| 53 |
+
|
| 54 |
+
async def complete(self, prompt: str) -> Completion:
|
| 55 |
+
self.prompts.append(prompt)
|
| 56 |
+
return Completion(
|
| 57 |
+
text=self.output,
|
| 58 |
+
input_tokens=len(prompt.split()),
|
| 59 |
+
output_tokens=len(self.output.split()),
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@pytest.fixture
|
| 64 |
+
def fake_runtime(monkeypatch):
|
| 65 |
+
fake = FakeRuntime()
|
| 66 |
+
monkeypatch.setattr(main, "runtime", fake)
|
| 67 |
+
return fake
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
@pytest.fixture
|
| 71 |
+
def client(fake_runtime):
|
| 72 |
+
with TestClient(main.app) as test_client:
|
| 73 |
+
yield test_client
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@pytest.fixture
|
| 77 |
+
def valid_request():
|
| 78 |
+
return json.loads(json.dumps(VALID_REQUEST))
|
tests/test_api.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
from tests.conftest import VALID_OUTPUT
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
GENERATE = "/v1/progression/generate"
|
| 7 |
+
|
| 8 |
+
OLD_METADATA_FIELDS = {
|
| 9 |
+
"metadata",
|
| 10 |
+
"model_repo",
|
| 11 |
+
"model_file",
|
| 12 |
+
"version",
|
| 13 |
+
"quantization",
|
| 14 |
+
"runtime",
|
| 15 |
+
"elapsed_seconds",
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def test_health_ready(client):
|
| 20 |
+
assert client.get("/health").json() == {
|
| 21 |
+
"status": "ok",
|
| 22 |
+
"model_ready": True,
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_health_degraded(client, fake_runtime):
|
| 27 |
+
fake_runtime.is_ready = False
|
| 28 |
+
|
| 29 |
+
assert client.get("/health").json() == {
|
| 30 |
+
"status": "degraded",
|
| 31 |
+
"model_ready": False,
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def test_models(client):
|
| 36 |
+
entry = client.get("/models").json()["progression"]["v1"]
|
| 37 |
+
|
| 38 |
+
assert entry["runtime"] == "llama-cpp-python"
|
| 39 |
+
assert entry["quantization"] == "Q8_0"
|
| 40 |
+
assert entry["prompt_format"] == "raw_sft_completion"
|
| 41 |
+
assert entry["status"] == "ready"
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_generate_success(client, fake_runtime, valid_request):
|
| 45 |
+
response = client.post(GENERATE, json=valid_request)
|
| 46 |
+
|
| 47 |
+
assert response.status_code == 200
|
| 48 |
+
|
| 49 |
+
body = response.json()
|
| 50 |
+
|
| 51 |
+
assert set(body) == {"progression", "usage"}
|
| 52 |
+
assert body["progression"] == VALID_OUTPUT["progression"]
|
| 53 |
+
assert set(body["usage"]) == {"input_tokens", "output_tokens"}
|
| 54 |
+
assert body["usage"]["input_tokens"] > 0
|
| 55 |
+
assert body["usage"]["output_tokens"] > 0
|
| 56 |
+
assert not OLD_METADATA_FIELDS & set(body)
|
| 57 |
+
|
| 58 |
+
# The exact raw SFT prompt is what reaches the model.
|
| 59 |
+
assert len(fake_runtime.prompts) == 1
|
| 60 |
+
assert fake_runtime.prompts[0].endswith("\n\nOUTPUT:\n")
|
| 61 |
+
assert (
|
| 62 |
+
'"previous_activity":{"title":"Practice the skill"}'
|
| 63 |
+
in fake_runtime.prompts[0]
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def test_generation_field_rejected(client, fake_runtime, valid_request):
|
| 68 |
+
valid_request["generation"] = {"temperature": 1.0, "seed": 7}
|
| 69 |
+
|
| 70 |
+
response = client.post(GENERATE, json=valid_request)
|
| 71 |
+
|
| 72 |
+
assert response.status_code == 422
|
| 73 |
+
assert fake_runtime.prompts == []
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def test_public_schema_has_no_generation_field(client):
|
| 77 |
+
schema = client.get("/openapi.json").json()
|
| 78 |
+
properties = schema["components"]["schemas"]["ProgressionRequest"][
|
| 79 |
+
"properties"
|
| 80 |
+
]
|
| 81 |
+
|
| 82 |
+
assert set(properties) == {
|
| 83 |
+
"version",
|
| 84 |
+
"knowledge_submission",
|
| 85 |
+
"wisdom",
|
| 86 |
+
"village_goal",
|
| 87 |
+
"previous_activity_title",
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def test_old_wisdom_schema_rejected(client, valid_request):
|
| 92 |
+
valid_request["wisdom"] = {
|
| 93 |
+
"title": "Past learning",
|
| 94 |
+
"description": "Older record",
|
| 95 |
+
"content": "I learned something before.",
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
assert client.post(GENERATE, json=valid_request).status_code == 422
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def test_wisdom_capitalization_enforced(client, valid_request):
|
| 102 |
+
wisdom = valid_request["wisdom"]
|
| 103 |
+
wisdom["bookType"] = wisdom.pop("BookType")
|
| 104 |
+
|
| 105 |
+
assert client.post(GENERATE, json=valid_request).status_code == 422
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def test_unknown_version(client, fake_runtime, valid_request):
|
| 109 |
+
valid_request["version"] = "v2"
|
| 110 |
+
|
| 111 |
+
response = client.post(GENERATE, json=valid_request)
|
| 112 |
+
|
| 113 |
+
assert response.status_code == 404
|
| 114 |
+
assert "v2" in response.json()["detail"]
|
| 115 |
+
assert fake_runtime.prompts == []
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def test_model_not_ready(client, fake_runtime, valid_request):
|
| 119 |
+
fake_runtime.is_ready = False
|
| 120 |
+
|
| 121 |
+
assert client.post(GENERATE, json=valid_request).status_code == 503
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def test_text_before_json_is_accepted(client, fake_runtime, valid_request):
|
| 125 |
+
fake_runtime.output = "Sure:\n" + json.dumps(VALID_OUTPUT)
|
| 126 |
+
|
| 127 |
+
response = client.post(GENERATE, json=valid_request)
|
| 128 |
+
|
| 129 |
+
assert response.status_code == 200
|
| 130 |
+
assert response.json()["progression"] == VALID_OUTPUT["progression"]
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def test_malformed_json_returns_502(client, fake_runtime, valid_request):
|
| 134 |
+
fake_runtime.output = '{"progression": {"knowledge_state": "COMP'
|
| 135 |
+
|
| 136 |
+
response = client.post(GENERATE, json=valid_request)
|
| 137 |
+
|
| 138 |
+
assert response.status_code == 502
|
| 139 |
+
assert response.json()["detail"]["raw_output"] == fake_runtime.output
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def test_invalid_schema_returns_502(client, fake_runtime, valid_request):
|
| 143 |
+
fake_runtime.output = json.dumps(
|
| 144 |
+
{"progression": {"knowledge_state": "DONE"}}
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
response = client.post(GENERATE, json=valid_request)
|
| 148 |
+
|
| 149 |
+
assert response.status_code == 502
|
| 150 |
+
assert "validation_error" in response.json()["detail"]
|
tests/test_json_utils.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from app.json_utils import extract_json
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def test_clean_json():
|
| 5 |
+
assert extract_json('{"progression":{"a":1}}') == {
|
| 6 |
+
"progression": {"a": 1},
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_text_before_and_after_json():
|
| 11 |
+
text = 'Here is the result:\n{"progression":{"a":1}}\nDone.'
|
| 12 |
+
|
| 13 |
+
assert extract_json(text) == {"progression": {"a": 1}}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_malformed_json():
|
| 17 |
+
assert extract_json('{"progression": {"a": 1') is None
|
| 18 |
+
assert extract_json("no json here") is None
|
tests/test_prompt.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
from app.prompts import SYSTEM_INSTRUCTION, build_prompt, training_input
|
| 4 |
+
from app.schemas import ProgressionRequest
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def make_request(valid_request) -> ProgressionRequest:
|
| 8 |
+
return ProgressionRequest(**valid_request)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def test_prompt_matches_sft_shape(valid_request):
|
| 12 |
+
req = make_request(valid_request)
|
| 13 |
+
prompt = build_prompt(req)
|
| 14 |
+
|
| 15 |
+
assert prompt.count("\n\nINPUT:\n") == 1
|
| 16 |
+
assert prompt.count("\n\nOUTPUT:\n") == 1
|
| 17 |
+
assert prompt.startswith(SYSTEM_INSTRUCTION.strip() + "\n\nINPUT:\n")
|
| 18 |
+
assert prompt.endswith("\n\nOUTPUT:\n")
|
| 19 |
+
assert '"previous_activity":{"title":"Practice the skill"}' in prompt
|
| 20 |
+
assert '"knowledge_submission":"I completed the task."' in prompt
|
| 21 |
+
assert "previous_activity_title" not in prompt
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_prompt_contains_compact_json(valid_request):
|
| 25 |
+
req = make_request(valid_request)
|
| 26 |
+
prompt = build_prompt(req)
|
| 27 |
+
|
| 28 |
+
body = prompt.split("\n\nINPUT:\n")[1].split("\n\nOUTPUT:\n")[0]
|
| 29 |
+
|
| 30 |
+
assert "\n" not in body
|
| 31 |
+
assert body == json.dumps(
|
| 32 |
+
training_input(req),
|
| 33 |
+
ensure_ascii=False,
|
| 34 |
+
separators=(",", ":"),
|
| 35 |
+
)
|
| 36 |
+
assert json.loads(body) == training_input(req)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_prompt_does_not_escape_non_ascii(valid_request):
|
| 40 |
+
valid_request["knowledge_submission"] = "J'ai terminé la tâche."
|
| 41 |
+
prompt = build_prompt(make_request(valid_request))
|
| 42 |
+
|
| 43 |
+
assert "J'ai terminé la tâche." in prompt
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_training_input_structure(valid_request):
|
| 47 |
+
model_input = training_input(make_request(valid_request))
|
| 48 |
+
|
| 49 |
+
assert list(model_input) == [
|
| 50 |
+
"village_goal",
|
| 51 |
+
"previous_activity",
|
| 52 |
+
"knowledge_submission",
|
| 53 |
+
"wisdom",
|
| 54 |
+
]
|
| 55 |
+
assert model_input["previous_activity"] == {
|
| 56 |
+
"title": "Practice the skill",
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def test_wisdom_field_names_preserved(valid_request):
|
| 61 |
+
model_input = training_input(make_request(valid_request))
|
| 62 |
+
|
| 63 |
+
assert list(model_input["wisdom"]) == [
|
| 64 |
+
"content",
|
| 65 |
+
"chapterTitle",
|
| 66 |
+
"BookTypeName",
|
| 67 |
+
"BookType",
|
| 68 |
+
]
|
| 69 |
+
assert model_input["wisdom"] == valid_request["wisdom"]
|