From bf5af4fd29abed21b7ba7c4dc512831849ebb652 Mon Sep 17 00:00:00 2001 From: okirmis Date: Mon, 23 Dec 2024 00:51:32 +0100 Subject: [PATCH 1/2] Fix bug #17352 by catching errors for malformed LLM responses --- llama-index-core/llama_index/core/indices/utils.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/llama-index-core/llama_index/core/indices/utils.py b/llama-index-core/llama_index/core/indices/utils.py index 4580da5c80909..ae1c0a4e22b4e 100644 --- a/llama-index-core/llama_index/core/indices/utils.py +++ b/llama-index-core/llama_index/core/indices/utils.py @@ -116,8 +116,18 @@ def default_parse_choice_select_answer_fn( continue answer_nums.append(answer_num) # extract just the first digits after the colon. - _answer_relevance = re.findall(r"\d+", line_tokens[1].split(":")[1].strip())[0] - answer_relevances.append(float(_answer_relevance)) + try: + _answer_relevance = re.findall(r"\d+", line_tokens[1].split(":")[1].strip())[0] + answer_relevances.append(float(_answer_relevance)) + except (IndexError, ValueError) as e: + if not raise_error: + continue + else: + raise ValueError( + f"Invalid answer line: {answer_line}. " + "Answer line must be of the form: " + "answer_num: , answer_relevance: " + ) return answer_nums, answer_relevances From e69030ea181639e8ce2a60b8a70fc7900f800c0c Mon Sep 17 00:00:00 2001 From: Oskar Kirmis Date: Mon, 23 Dec 2024 17:23:17 +0100 Subject: [PATCH 2/2] Fix formatting --- llama-index-core/llama_index/core/indices/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/llama-index-core/llama_index/core/indices/utils.py b/llama-index-core/llama_index/core/indices/utils.py index ae1c0a4e22b4e..42cd0a0cc23d0 100644 --- a/llama-index-core/llama_index/core/indices/utils.py +++ b/llama-index-core/llama_index/core/indices/utils.py @@ -117,7 +117,9 @@ def default_parse_choice_select_answer_fn( answer_nums.append(answer_num) # extract just the first digits after the colon. try: - _answer_relevance = re.findall(r"\d+", line_tokens[1].split(":")[1].strip())[0] + _answer_relevance = re.findall( + r"\d+", line_tokens[1].split(":")[1].strip() + )[0] answer_relevances.append(float(_answer_relevance)) except (IndexError, ValueError) as e: if not raise_error: