Spaces:
Running on Zero
Running on Zero
Add JSON skeleton to compact ZeroGPU prompt
Browse files- figment/model_client.py +13 -8
- tests/test_model_client.py +18 -0
figment/model_client.py
CHANGED
|
@@ -15,7 +15,7 @@ import wave
|
|
| 15 |
from typing import Any
|
| 16 |
|
| 17 |
from .config import FigmentConfig, load_config
|
| 18 |
-
from .prompt_builder import OUTPUT_SCHEMA
|
| 19 |
from .zerogpu_runtime import generate_zero_gpu_json
|
| 20 |
|
| 21 |
|
|
@@ -129,12 +129,15 @@ class ModelClient:
|
|
| 129 |
auth_headers={},
|
| 130 |
)
|
| 131 |
if self.config.model_backend == "hf_zerogpu":
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
if self.config.model_backend == "hosted_omni":
|
| 139 |
endpoint = self.config.omni_endpoint_url or self.config.hf_endpoint_url or self.config.nvidia_base_url
|
| 140 |
if not endpoint:
|
|
@@ -251,7 +254,8 @@ class ModelClient:
|
|
| 251 |
|
| 252 |
|
| 253 |
ZERO_GPU_SYSTEM_PROMPT = """You are Figment, an offline protocol navigator for a trained responder.
|
| 254 |
-
Use ONLY the compact protocol-card facts in CONTEXT. Return
|
|
|
|
| 255 |
Do not diagnose, prescribe, dose medication, autonomously triage, discharge, or downgrade deterministic red flags.
|
| 256 |
Every candidate_protocol_pathways item must cite a retrieved clinical card_id. source_cards must cite every card used.
|
| 257 |
If information is missing or uncertain, list it instead of inventing it."""
|
|
@@ -271,6 +275,7 @@ def _zero_gpu_compact_prompt(prompt: str, context: dict[str, Any]) -> str:
|
|
| 271 |
}
|
| 272 |
return (
|
| 273 |
f"{ZERO_GPU_SYSTEM_PROMPT}\n\n"
|
|
|
|
| 274 |
f"CONTEXT:\n{json.dumps(compact_context, sort_keys=True, separators=(',', ':'))}"
|
| 275 |
)
|
| 276 |
|
|
|
|
| 15 |
from typing import Any
|
| 16 |
|
| 17 |
from .config import FigmentConfig, load_config
|
| 18 |
+
from .prompt_builder import OUTPUT_SCHEMA, REQUIRED_JSON_SKELETON
|
| 19 |
from .zerogpu_runtime import generate_zero_gpu_json
|
| 20 |
|
| 21 |
|
|
|
|
| 129 |
auth_headers={},
|
| 130 |
)
|
| 131 |
if self.config.model_backend == "hf_zerogpu":
|
| 132 |
+
try:
|
| 133 |
+
return generate_zero_gpu_json(
|
| 134 |
+
prompt=_zero_gpu_compact_prompt(prompt, context),
|
| 135 |
+
model_repo=self.config.zerogpu_model_repo,
|
| 136 |
+
model_subfolder=self.config.zerogpu_model_subfolder,
|
| 137 |
+
model_id=self.config.local_model_id,
|
| 138 |
+
)
|
| 139 |
+
except (json.JSONDecodeError, RuntimeError, ValueError) as exc:
|
| 140 |
+
raise ModelClientError(f"hf_zerogpu backend failed for {self.config.local_model_id}") from exc
|
| 141 |
if self.config.model_backend == "hosted_omni":
|
| 142 |
endpoint = self.config.omni_endpoint_url or self.config.hf_endpoint_url or self.config.nvidia_base_url
|
| 143 |
if not endpoint:
|
|
|
|
| 254 |
|
| 255 |
|
| 256 |
ZERO_GPU_SYSTEM_PROMPT = """You are Figment, an offline protocol navigator for a trained responder.
|
| 257 |
+
Use ONLY the compact protocol-card facts in CONTEXT. Return exactly one valid JSON object, with no markdown, prose, or comments.
|
| 258 |
+
The object must follow REQUIRED_JSON_SKELETON and include every required key.
|
| 259 |
Do not diagnose, prescribe, dose medication, autonomously triage, discharge, or downgrade deterministic red flags.
|
| 260 |
Every candidate_protocol_pathways item must cite a retrieved clinical card_id. source_cards must cite every card used.
|
| 261 |
If information is missing or uncertain, list it instead of inventing it."""
|
|
|
|
| 275 |
}
|
| 276 |
return (
|
| 277 |
f"{ZERO_GPU_SYSTEM_PROMPT}\n\n"
|
| 278 |
+
f"REQUIRED_JSON_SKELETON:\n{json.dumps(REQUIRED_JSON_SKELETON, sort_keys=True, separators=(',', ':'))}\n\n"
|
| 279 |
f"CONTEXT:\n{json.dumps(compact_context, sort_keys=True, separators=(',', ':'))}"
|
| 280 |
)
|
| 281 |
|
tests/test_model_client.py
CHANGED
|
@@ -203,11 +203,29 @@ def test_hf_zerogpu_compact_prompt_preserves_route_facts_without_verbose_card_te
|
|
| 203 |
assert len(compact) < 3000
|
| 204 |
assert "PED-DEHYD-RED-FLAGS-v1" in compact
|
| 205 |
assert "chief_concern" in compact
|
|
|
|
| 206 |
assert "required_keys" in compact
|
| 207 |
assert "source_note" not in compact
|
| 208 |
assert "VERBOSE" not in compact
|
| 209 |
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
def test_custom_hf_endpoint_prefers_hf_token_over_nvidia_key(monkeypatch: Any) -> None:
|
| 212 |
captured: dict[str, Any] = {}
|
| 213 |
|
|
|
|
| 203 |
assert len(compact) < 3000
|
| 204 |
assert "PED-DEHYD-RED-FLAGS-v1" in compact
|
| 205 |
assert "chief_concern" in compact
|
| 206 |
+
assert "REQUIRED_JSON_SKELETON" in compact
|
| 207 |
assert "required_keys" in compact
|
| 208 |
assert "source_note" not in compact
|
| 209 |
assert "VERBOSE" not in compact
|
| 210 |
|
| 211 |
|
| 212 |
+
def test_hf_zerogpu_backend_wraps_invalid_model_json(monkeypatch: Any) -> None:
|
| 213 |
+
def fake_generate_zero_gpu_json(**_: Any) -> dict[str, Any]:
|
| 214 |
+
raise json.JSONDecodeError("bad", "not json", 0)
|
| 215 |
+
|
| 216 |
+
monkeypatch.setattr("figment.model_client.generate_zero_gpu_json", fake_generate_zero_gpu_json)
|
| 217 |
+
client = ModelClient(
|
| 218 |
+
FigmentConfig(
|
| 219 |
+
model_backend="hf_zerogpu",
|
| 220 |
+
model_stack="local_4b_parakeet",
|
| 221 |
+
local_model_id="figment-sft-v14p-lora-merged-bf16",
|
| 222 |
+
)
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
with pytest.raises(ModelClientError, match="hf_zerogpu backend failed"):
|
| 226 |
+
client.generate_json("Return JSON.", {"intake": {}, "retrieved_cards": []})
|
| 227 |
+
|
| 228 |
+
|
| 229 |
def test_custom_hf_endpoint_prefers_hf_token_over_nvidia_key(monkeypatch: Any) -> None:
|
| 230 |
captured: dict[str, Any] = {}
|
| 231 |
|