Skip to content

Commit

Permalink
fix: add fallback for parsing AI responses with leading '+' symbols (#…
Browse files Browse the repository at this point in the history
…104)

Signed-off-by: NxPKG <iconmamundentist@gmail.com>
  • Loading branch information
NxPKG authored Jan 31, 2025
1 parent 13e59e6 commit 20b40c9
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pr_insight/algo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ def try_fix_yaml(response_text: str,
get_logger().info(f"Successfully parsed AI prediction after adding |-\n")
return data
except:
get_logger().info(f"Failed to parse AI prediction after adding |-\n")
pass

# second fallback - try to extract only range from first ```yaml to ````
snippet_pattern = r'```(yaml)?[\s\S]*?```'
Expand Down Expand Up @@ -779,8 +779,18 @@ def try_fix_yaml(response_text: str,
except:
pass

# fifth fallback - try to remove leading '+' (sometimes added by AI for 'existing code' and 'improved code')
response_text_lines_copy = response_text_lines.copy()
for i in range(0, len(response_text_lines_copy)):
response_text_lines_copy[i] = ' ' + response_text_lines_copy[i][1:]
try:
data = yaml.safe_load('\n'.join(response_text_lines_copy))
get_logger().info(f"Successfully parsed AI prediction after removing leading '+'")
return data
except:
pass

# fifth fallback - try to remove last lines
# sixth fallback - try to remove last lines
data = {}
for i in range(1, len(response_text_lines)):
response_text_lines_tmp = '\n'.join(response_text_lines[:-i])
Expand Down

0 comments on commit 20b40c9

Please sign in to comment.