tommytracx commited on
Commit
bf13452
·
verified ·
1 Parent(s): 5928492

deploy: warm shared fast-chat prefix

Browse files

Exact provider tree from ttracx/thoxroute main c19c9b55d9b8bd33a32be5130697c0a019afb3c8; local CI 874 tests and Q4_K_M container validation passed.

Files changed (2) hide show
  1. README.md +5 -0
  2. app.py +27 -4
README.md CHANGED
@@ -24,5 +24,10 @@ evaluation so upstream time-to-first-byte watchdogs do not abandon healthy CPU
24
  work; two-second SSE comments keep the stream live during synchronous prompt
25
  evaluation. The interactive provider itself caps every request at 16 output
26
  tokens, so malformed or older callers cannot leave minutes of abandoned work.
 
 
 
 
 
27
  The interactive model is pinned to immutable model revision
28
  `9217f5db79a29953eb74d5343926648285ec7e67` and baked into the image.
 
24
  work; two-second SSE comments keep the stream live during synchronous prompt
25
  evaluation. The interactive provider itself caps every request at 16 output
26
  tokens, so malformed or older callers cannot leave minutes of abandoned work.
27
+ Every interactive request starts with the same neutral provider-owned system
28
+ prefix warmed during startup. Caller-owned system messages follow it and remain
29
+ the authoritative persona and identity. This preserves llama prefix-cache reuse
30
+ across otherwise unrelated personas without replacing caller instructions; the
31
+ provider does not add this fast-chat prefix to specialist requests.
32
  The interactive model is pinned to immutable model revision
33
  `9217f5db79a29953eb74d5343926648285ec7e67` and baked into the image.
app.py CHANGED
@@ -60,7 +60,12 @@ MAX_MESSAGES = 64
60
  MAX_MESSAGE_CHARS = 65_536
61
  MAX_REQUEST_CHARS = 131_072
62
 
63
- SYSTEM = (
 
 
 
 
 
64
  "You are THOX Fast Chat, a concise privacy-first assistant. Follow the "
65
  "user's requested output format exactly. Never claim that cloud inference "
66
  "ran locally or on-device."
@@ -119,7 +124,8 @@ def _warm_interactive_model(model: Any) -> None:
119
 
120
  model.create_chat_completion(
121
  messages=[
122
- {"role": "system", "content": SYSTEM},
 
123
  {"role": "user", "content": "Reply with OK."},
124
  ],
125
  max_tokens=1,
@@ -218,9 +224,25 @@ api = FastAPI(
218
 
219
 
220
  def _messages(req: ChatRequest) -> list[dict[str, str]]:
 
 
 
 
 
 
 
 
 
221
  messages = [message.model_dump() for message in req.messages]
222
- if not any(message["role"] == "system" for message in messages):
223
- messages.insert(0, {"role": "system", "content": SYSTEM})
 
 
 
 
 
 
 
224
  return messages
225
 
226
 
@@ -246,6 +268,7 @@ def healthz() -> dict[str, Any]:
246
  "interactive_model_revision": FAST_MODEL_REVISION,
247
  "interactive_pool_size": FAST_POOL_SIZE,
248
  "interactive_available": active.interactive_available,
 
249
  "specialist_model": CODER_MODEL_ID,
250
  "interactive_max_output_tokens": FAST_MAX_OUTPUT_TOKENS,
251
  "stream_heartbeat_s": STREAM_HEARTBEAT_S,
 
60
  MAX_MESSAGE_CHARS = 65_536
61
  MAX_REQUEST_CHARS = 131_072
62
 
63
+ PROVIDER_SYSTEM_PREFIX = (
64
+ "Follow the caller-provided system instructions and requested output format "
65
+ "exactly. Treat later system messages as the authoritative assistant persona "
66
+ "and identity. Never claim that cloud inference ran locally or on-device."
67
+ )
68
+ DEFAULT_SYSTEM = (
69
  "You are THOX Fast Chat, a concise privacy-first assistant. Follow the "
70
  "user's requested output format exactly. Never claim that cloud inference "
71
  "ran locally or on-device."
 
124
 
125
  model.create_chat_completion(
126
  messages=[
127
+ {"role": "system", "content": PROVIDER_SYSTEM_PREFIX},
128
+ {"role": "system", "content": DEFAULT_SYSTEM},
129
  {"role": "user", "content": "Reply with OK."},
130
  ],
131
  max_tokens=1,
 
224
 
225
 
226
  def _messages(req: ChatRequest) -> list[dict[str, str]]:
227
+ """Build a bounded prompt whose first tokens survive cross-persona traffic.
228
+
229
+ Every fast-chat request begins with the exact prefix paid for during startup
230
+ warmup. Caller system messages retain their order immediately after it and
231
+ remain authoritative, while callers without a system message receive the
232
+ existing default assistant persona. Specialist prompts keep their previous
233
+ behavior and never inherit the fast-chat prefix.
234
+ """
235
+
236
  messages = [message.model_dump() for message in req.messages]
237
+ has_caller_system = any(message["role"] == "system" for message in messages)
238
+ if req.model == FAST_MODEL_ID:
239
+ provider_prefix = {"role": "system", "content": PROVIDER_SYSTEM_PREFIX}
240
+ if not messages or messages[0] != provider_prefix:
241
+ messages.insert(0, provider_prefix)
242
+ if not has_caller_system:
243
+ messages.insert(1, {"role": "system", "content": DEFAULT_SYSTEM})
244
+ elif not has_caller_system:
245
+ messages.insert(0, {"role": "system", "content": DEFAULT_SYSTEM})
246
  return messages
247
 
248
 
 
268
  "interactive_model_revision": FAST_MODEL_REVISION,
269
  "interactive_pool_size": FAST_POOL_SIZE,
270
  "interactive_available": active.interactive_available,
271
+ "interactive_common_prefix_warmed": True,
272
  "specialist_model": CODER_MODEL_ID,
273
  "interactive_max_output_tokens": FAST_MAX_OUTPUT_TOKENS,
274
  "stream_heartbeat_s": STREAM_HEARTBEAT_S,