Skip to content

Commit 215abdb

Browse files
authored
Improve multiturn stopping condition (#814)
* Improve multiturn stopping condition * improve
1 parent 4b00214 commit 215abdb

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

ch05/11_qwen3/qwen3-chat-interface/qwen3-chat-interface-multiturn.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ def build_prompt_from_history(history, add_assistant_header=True):
115115
DEVICE = get_device(DEVICE)
116116
MODEL, TOKENIZER = get_model_and_tokenizer(QWEN3_CONFIG, REPO_ID, LOCAL_DIR, DEVICE, REASONING)
117117

118+
# Even though the official TOKENIZER.eos_token_id is either <|im_end|> (reasoning)
119+
# or <|endoftext|> (base), the reasoning model sometimes emits both.
120+
EOS_TOKEN_IDS = (TOKENIZER.encode("<|im_end|>")[0], TOKENIZER.encode("<|endoftext|>")[0])
121+
118122

119123
@chainlit.on_chat_start
120124
async def on_start():
@@ -147,9 +151,11 @@ async def main(message: chainlit.Message):
147151
model=MODEL,
148152
token_ids=input_ids_tensor,
149153
max_new_tokens=MAX_NEW_TOKENS,
150-
eos_token_id=TOKENIZER.eos_token_id
154+
# eos_token_id=TOKENIZER.eos_token_id
151155
):
152156
token_id = tok.squeeze(0)
157+
if token_id in EOS_TOKEN_IDS:
158+
break
153159
piece = TOKENIZER.decode(token_id.tolist())
154160
await out_msg.stream_token(piece)
155161

0 commit comments

Comments
 (0)