electblake commited on
Commit
f91a7ad
·
1 Parent(s): 6d005e7

fix: decode deterministic answer after reasoning

Browse files
Files changed (1) hide show
  1. app.py +2 -48
app.py CHANGED
@@ -123,60 +123,14 @@ def generate(
123
  dim=-1,
124
  )
125
 
126
- draft_ids = model.generate(
127
  input_ids=reasoning_ids,
128
  attention_mask=torch.ones_like(reasoning_ids),
129
  max_new_tokens=512,
130
- do_sample=True,
131
- temperature=0.6,
132
- top_p=0.95,
133
- top_k=20,
134
- )
135
-
136
- draft = tokenizer.decode(
137
- draft_ids[0, reasoning_ids.shape[-1] :],
138
- skip_special_tokens=True,
139
- ).strip()
140
- final_messages = [
141
- {
142
- "role": "system",
143
- "content": (
144
- "Return only the final answer requested by the user. Do not include "
145
- "analysis, reasoning, self-correction, or commentary. Preserve any "
146
- "requested output format exactly."
147
- ),
148
- },
149
- {
150
- "role": "user",
151
- "content": f"Request:\n{user_prompt}\n\nDraft response:\n{draft}",
152
- },
153
- ]
154
- final_inputs = tokenizer.apply_chat_template(
155
- final_messages,
156
- add_generation_prompt=True,
157
- tokenize=True,
158
- return_dict=True,
159
- return_tensors="pt",
160
- ).to(model.device)
161
- final_input_ids = torch.cat(
162
- [
163
- final_inputs["input_ids"],
164
- torch.tensor([[think_end_token]], device=model.device),
165
- ],
166
- dim=-1,
167
- )
168
- final_ids = model.generate(
169
- input_ids=final_input_ids,
170
- attention_mask=torch.ones_like(final_input_ids),
171
- max_new_tokens=512,
172
- do_sample=True,
173
- temperature=0.6,
174
- top_p=0.95,
175
- top_k=20,
176
  )
177
 
178
  return tokenizer.decode(
179
- final_ids[0, final_input_ids.shape[-1] :],
180
  skip_special_tokens=True,
181
  ).strip()
182
 
 
123
  dim=-1,
124
  )
125
 
126
+ answer_ids = model.generate(
127
  input_ids=reasoning_ids,
128
  attention_mask=torch.ones_like(reasoning_ids),
129
  max_new_tokens=512,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  )
131
 
132
  return tokenizer.decode(
133
+ answer_ids[0, reasoning_ids.shape[-1] :],
134
  skip_special_tokens=True,
135
  ).strip()
136