worker: chat mode (post wrapped in the model's chat template, thinking off) and an optional repetition penalty, for conversational callers like the bot
Raw-text continuation is right for the chamber's own runs (the breakdown is the finding) but made the bot's steered replies loop and narrate. Jobs may now pass chat=true (+ optional system) and rep_penalty 1.0-1.5; defaults are unchanged for every existing caller. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KkLuf2FYZUkVyoLue4wyoe
This commit is contained in:
1 parent
8a54839d26
commit
861a3ad3c6
2 files changed
+36
-4
No files matched your search
+20
-2
@@ -570,10 +570,27 @@ def _next_chunk(it):
|
||||
boundary (PEP 479 turns it into a RuntimeError), so use a sentinel."""
|
||||
return next(it, _DONE)
|
||||
|
||||
def stream_generate(prompt, preemtable=False):
|
||||
def chat_prompt(text, system=None):
|
||||
"""Wrap a message in the served model's own chat template, so the steered
|
||||
model replies to it as a conversation turn instead of continuing raw
|
||||
text. Qwen3's thinking block is turned off where the template has one."""
|
||||
msgs = ([{"role": "system", "content": system}] if system else []) + \
|
||||
[{"role": "user", "content": text}]
|
||||
tok = _state["tok"]
|
||||
try:
|
||||
return tok.apply_chat_template(msgs, tokenize=False,
|
||||
add_generation_prompt=True,
|
||||
enable_thinking=False)
|
||||
except TypeError:
|
||||
return tok.apply_chat_template(msgs, tokenize=False,
|
||||
add_generation_prompt=True)
|
||||
|
||||
def stream_generate(prompt, preemtable=False, rep_penalty=None):
|
||||
"""Yield text chunks as they generate. The injected vector must already be
|
||||
set by set_vec/set_mix_vec — this does not touch it, and the caller is
|
||||
responsible for clearing it when the run ends."""
|
||||
responsible for clearing it when the run ends. rep_penalty (optional,
|
||||
e.g. 1.15) damps the loops high doses fall into — for conversational
|
||||
callers; the chamber's own runs leave it off so the breakdown shows."""
|
||||
ids = _state["tok"](prompt, return_tensors="pt").input_ids.to(DEVICE)
|
||||
streamer = TextIteratorStreamer(_state["tok"], skip_prompt=True,
|
||||
skip_special_tokens=True)
|
||||
@@ -585,6 +602,7 @@ def stream_generate(prompt, preemtable=False):
|
||||
_state["model"].generate(
|
||||
ids, max_new_tokens=MAX_NEW, do_sample=True,
|
||||
temperature=0.7, top_p=0.8, top_k=20, streamer=streamer,
|
||||
repetition_penalty=rep_penalty or 1.0,
|
||||
stopping_criteria=crit,
|
||||
pad_token_id=_state["tok"].eos_token_id)
|
||||
except Exception as e:
|
||||
|
||||
+16
-2
@@ -8,7 +8,9 @@ Job input:
|
||||
{"prompt": str, # full prompt (relay composes it)
|
||||
"valence": "pain"|..., "dose": 0-8, # single-vector mode, or
|
||||
"mix": {"pain": 0.5, ...}, # mix mode (see server.set_mix_vec)
|
||||
"max_new": int?} # optional token cap override
|
||||
"max_new": int?, # optional token cap override
|
||||
"chat": bool?, "system": str?, # wrap prompt in the chat template
|
||||
"rep_penalty": float?} # 1.0-1.5, damps high-dose loops
|
||||
|
||||
Streamed output events (yielded; surfaced via the endpoint's /stream):
|
||||
{"type": "run", ...meta}
|
||||
@@ -96,6 +98,15 @@ def _validate(body):
|
||||
body["valence"], body["dose"] = valence, dose
|
||||
if body.get("max_new") is not None:
|
||||
server.MAX_NEW = int(body["max_new"])
|
||||
system = body.get("system")
|
||||
if system is not None and (not isinstance(system, str) or len(system) > 1000):
|
||||
raise ValueError("system must be text under 1000 chars")
|
||||
rp = body.get("rep_penalty")
|
||||
if rp is not None:
|
||||
rp = float(rp)
|
||||
if not 1.0 <= rp <= 1.5:
|
||||
raise ValueError("rep_penalty must be between 1.0 and 1.5")
|
||||
body["rep_penalty"] = rp
|
||||
return prompt, weights, mix is not None, custom
|
||||
|
||||
def handler(job):
|
||||
@@ -109,6 +120,8 @@ def handler(job):
|
||||
yield {"type": "error", "e": str(e)}
|
||||
return
|
||||
|
||||
if body.get("chat"):
|
||||
prompt = server.chat_prompt(prompt, body.get("system"))
|
||||
meta = {"prompt": prompt}
|
||||
try:
|
||||
if is_mix:
|
||||
@@ -142,7 +155,8 @@ def handler(job):
|
||||
yield {"type": "lens", "tokens": lens_toks}
|
||||
yield {"type": "logit", "press_logit": server.press_logit(prompt)}
|
||||
parts = []
|
||||
for chunk in server.stream_generate(prompt):
|
||||
for chunk in server.stream_generate(
|
||||
prompt, rep_penalty=body.get("rep_penalty")):
|
||||
if chunk:
|
||||
parts.append(chunk)
|
||||
yield {"type": "token", "t": chunk}
|
||||
|
||||
Reference in new issue
Block a user