mirror of
https://github.com/qodo-ai/pr-agent.git
synced 2025-07-05 05:10:38 +08:00
Try to fix broken json output
This commit is contained in:
@ -68,11 +68,7 @@ class PRReviewer:
|
|||||||
model = settings.config.model
|
model = settings.config.model
|
||||||
response, finish_reason = await self.ai_handler.chat_completion(model=model, temperature=0.2,
|
response, finish_reason = await self.ai_handler.chat_completion(model=model, temperature=0.2,
|
||||||
system=system_prompt, user=user_prompt)
|
system=system_prompt, user=user_prompt)
|
||||||
try:
|
|
||||||
json.loads(response)
|
|
||||||
except json.decoder.JSONDecodeError:
|
|
||||||
logging.warning("Could not decode JSON")
|
|
||||||
response = {}
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def _prepare_pr_review(self) -> str:
|
def _prepare_pr_review(self) -> str:
|
||||||
@ -80,6 +76,20 @@ class PRReviewer:
|
|||||||
try:
|
try:
|
||||||
data = json.loads(review)
|
data = json.loads(review)
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
|
# Try to fix JSON if it is broken/incomplete: parse until the last valid code suggestion
|
||||||
|
if review.rfind("'Code suggestions': [") > 0 or review.rfind('"Code suggestions": [') > 0:
|
||||||
|
last_code_suggestion_ind = review.rfind(",")
|
||||||
|
valid_json = False
|
||||||
|
data = {}
|
||||||
|
while last_code_suggestion_ind > 0 and not valid_json:
|
||||||
|
try:
|
||||||
|
data = json.loads(review[:last_code_suggestion_ind] + "]}}")
|
||||||
|
valid_json = True
|
||||||
|
review = review[:last_code_suggestion_ind] + "]}}"
|
||||||
|
except json.decoder.JSONDecodeError:
|
||||||
|
review = review[:last_code_suggestion_ind]
|
||||||
|
last_code_suggestion_ind = review.rfind("},") + 1
|
||||||
|
if not valid_json:
|
||||||
logging.error("Unable to decode JSON response from AI")
|
logging.error("Unable to decode JSON response from AI")
|
||||||
data = {}
|
data = {}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user