mirror of
https://github.com/qodo-ai/pr-agent.git
synced 2025-12-12 10:55:17 +00:00
add max_iter
This commit is contained in:
parent
72af2a1f9c
commit
b8a71b369d
1 changed files with 4 additions and 2 deletions
|
|
@ -65,13 +65,14 @@ def parse_code_suggestion(code_suggestions: dict) -> str:
|
||||||
return markdown_text
|
return markdown_text
|
||||||
|
|
||||||
|
|
||||||
def try_fix_json(review):
|
def try_fix_json(review, max_iter=10):
|
||||||
# Try to fix JSON if it is broken/incomplete: parse until the last valid code suggestion
|
# Try to fix JSON if it is broken/incomplete: parse until the last valid code suggestion
|
||||||
data = {}
|
data = {}
|
||||||
if review.rfind("'Code suggestions': [") > 0 or review.rfind('"Code suggestions": [') > 0:
|
if review.rfind("'Code suggestions': [") > 0 or review.rfind('"Code suggestions": [') > 0:
|
||||||
last_code_suggestion_ind = [m.end() for m in re.finditer(r"\}\s*,", review)][-1] - 1
|
last_code_suggestion_ind = [m.end() for m in re.finditer(r"\}\s*,", review)][-1] - 1
|
||||||
valid_json = False
|
valid_json = False
|
||||||
while last_code_suggestion_ind > 0 and not valid_json:
|
iter_count = 0
|
||||||
|
while last_code_suggestion_ind > 0 and not valid_json and iter_count < max_iter:
|
||||||
try:
|
try:
|
||||||
data = json.loads(review[:last_code_suggestion_ind] + "]}}")
|
data = json.loads(review[:last_code_suggestion_ind] + "]}}")
|
||||||
valid_json = True
|
valid_json = True
|
||||||
|
|
@ -80,6 +81,7 @@ def try_fix_json(review):
|
||||||
review = review[:last_code_suggestion_ind]
|
review = review[:last_code_suggestion_ind]
|
||||||
# Use regular expression to find the last occurrence of "}," with any number of whitespaces or newlines
|
# Use regular expression to find the last occurrence of "}," with any number of whitespaces or newlines
|
||||||
last_code_suggestion_ind = [m.end() for m in re.finditer(r"\}\s*,", review)][-1] - 1
|
last_code_suggestion_ind = [m.end() for m in re.finditer(r"\}\s*,", review)][-1] - 1
|
||||||
|
iter_count += 1
|
||||||
if not valid_json:
|
if not valid_json:
|
||||||
logging.error("Unable to decode JSON response from AI")
|
logging.error("Unable to decode JSON response from AI")
|
||||||
data = {}
|
data = {}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue