Skip to content

Commit 4f8494f

Browse files
fix: auto retry if a combine batch failed to make sure we don't loose intermedaite answers
1 parent c6266ea commit 4f8494f

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

WDoc/WDoc.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,10 +1691,34 @@ def source_replace(input: str) -> str:
16911691
"intermediate_answers": b
16921692
} for b in batches
16931693
]
1694-
temp_interm_answ = [
1695-
thinking_answer_parser(a["final_answer"])["answer"]
1696-
for a in final_answer_chain.batch(batch_args)
1697-
]
1694+
temp_interm_answ = []
1695+
batch_result = final_answer_chain.batch(batch_args)
1696+
n_trial = 3
1697+
for ia, a in enumerate(batch_result):
1698+
trial = 0
1699+
for trial in range(n_trial):
1700+
try:
1701+
o = thinking_answer_parser(
1702+
a["final_answer"],
1703+
strict=True,
1704+
)["answer"]
1705+
temp_interm_answ.append(o)
1706+
break
1707+
except Exception as e:
1708+
red(
1709+
f"Error at trial {trial} when separating "
1710+
"thinking from answer from LLM output.\n\n"
1711+
f"The full answer is: '{a}'\n\n"
1712+
f"The error was: '{e}'\n\n"
1713+
"Retrying "
1714+
"this specific batch to make sure we don't"
1715+
" loose intermediate answers")
1716+
# modify the batch slightly to bypass the cache
1717+
altered_batch = batch_args[ia]
1718+
altered_batch["question_to_answer"] += "."
1719+
altered_batch["intermediate_answers"] += "."
1720+
a = final_answer_chain.batch([altered_batch])[0]
1721+
16981722
all_intermediate_answers.append(temp_interm_answ)
16991723
pbar.n = pbar.total - len(temp_interm_answ) + 1
17001724
pbar.update(0)

WDoc/utils/misc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ def new_func(self, *args, **kwargs):
707707
THINE = "</thinking>"
708708
ANSW = "<answer>"
709709
ANSWE = "</answer>"
710-
def thinking_answer_parser(output: str) -> dict:
710+
def thinking_answer_parser(output: str, strict: bool = False) -> dict:
711711
"""separate the <thinking> and <answer> tags in an answer"""
712712
try:
713713
# fix </answer> instead of <answer>
@@ -739,6 +739,8 @@ def thinking_answer_parser(output: str) -> dict:
739739

740740
return {"thinking": thinking, "answer": answer}
741741
except Exception as err:
742+
if strict: # otherwise combining answers could snowball into losing lots of text
743+
raise
742744
red(f"Error when parsing LLM output to get thinking and answer part.\nError: '{err}'\nOriginal output: '{output}'\nWill continue if not using --debug")
743745
if is_debug:
744746
raise

0 commit comments

Comments
 (0)