Skip to content

Commit

Permalink
More robust json parsing (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
neubig authored Sep 27, 2024
1 parent 3a38c51 commit ae91343
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions github_resolver/resolve_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import dataclasses
import re
import shutil
from typing import Any, Awaitable, TextIO
import requests
Expand Down Expand Up @@ -219,7 +220,15 @@ def guess_success(issue: GithubIssue, history: ShortTermHistory, llm_config: LLM
(1) has the issue been successfully resolved?
(2) If the issue has been resolved, please provide an explanation of what was done in the PR that can be sent to a human reviewer on github. If the issue has not been resolved, please provide an explanation of why.
Answer in JSON in the format {{success: bool, explanation: str}}."""
Answer in JSON in the format below:
```json
{{
"success": true/false,
"explanation": "..."
}}
```
"""

response = completion(
model=llm_config.model,
Expand All @@ -229,8 +238,13 @@ def guess_success(issue: GithubIssue, history: ShortTermHistory, llm_config: LLM
)

answer = response.choices[0].message.content.strip()
pattern = r'```json\s*([\s\S]*?)\s*```'
match = re.search(pattern, answer)
try:
json_answer = json.loads(answer)
if match:
json_answer = json.loads(match.group(1))
else:
json_answer = json.loads(answer)
return json_answer['success'], json_answer['explanation']
except json.JSONDecodeError:
return False, f"Failed to decode JSON from LLM response: {answer}"
Expand Down

0 comments on commit ae91343

Please sign in to comment.