Spaces:
Running on Zero
Running on Zero
electblake commited on
Commit ·
6d005e7
1
Parent(s): eca64c6
fix: finalize responses without reasoning prose
Browse files
app.py
CHANGED
|
@@ -123,10 +123,52 @@ def generate(
|
|
| 123 |
dim=-1,
|
| 124 |
)
|
| 125 |
|
| 126 |
-
|
| 127 |
input_ids=reasoning_ids,
|
| 128 |
attention_mask=torch.ones_like(reasoning_ids),
|
| 129 |
-
max_new_tokens=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
do_sample=True,
|
| 131 |
temperature=0.6,
|
| 132 |
top_p=0.95,
|
|
@@ -134,7 +176,7 @@ def generate(
|
|
| 134 |
)
|
| 135 |
|
| 136 |
return tokenizer.decode(
|
| 137 |
-
|
| 138 |
skip_special_tokens=True,
|
| 139 |
).strip()
|
| 140 |
|
|
|
|
| 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,
|
|
|
|
| 176 |
)
|
| 177 |
|
| 178 |
return tokenizer.decode(
|
| 179 |
+
final_ids[0, final_input_ids.shape[-1] :],
|
| 180 |
skip_special_tokens=True,
|
| 181 |
).strip()
|
| 182 |
|