Improve JSON parsing.

This commit is contained in:
OK
2023-03-29 19:22:54 +02:00
parent 7f3cb66043
commit ddc44bb9da
3 changed files with 20 additions and 6 deletions
+16 -4
View File
@@ -1,4 +1,5 @@
import json
import multiline
import openai
import aiohttp
import logging
@@ -17,6 +18,17 @@ def pp(*args, **kw):
return pformat(*args, **kw)
def parse_response(content: str) -> Dict:
content = content.strip()
try:
return json.loads(content)
except Exception:
try:
return multiline.loads(content, multiline=True)
except Exception as err:
raise err
class AIMessageBase(object):
def __init__(self) -> None:
pass
@@ -180,15 +192,15 @@ class AIResponder(object):
if answer is None:
continue
try:
response = json.loads(answer['content'])
response = parse_response(answer['content'])
except Exception as err:
logging.warning(f"failed to parse the answer: {pp(err)}\n{repr(answer['content'])}")
answer['content'] = await self.fix(answer['content'])
try:
response = json.loads(answer['content'])
response = parse_response(answer['content'])
except Exception as err:
logging.error(f"failed to parse the answer: {pp(err)}\n{repr(answer['content'])}")
return AIResponse(None, False, f"ERROR: I could not parse this answer: {repr(answer['content'])}", None, False)
logging.error(f"failed to parse the fixed answer: {pp(err)}\n{repr(answer['content'])}")
continue
if 'hack' not in response or type(response.get('picture', None)) not in (type(None), str):
continue
logging.info(f"got this answer:\n{pp(response)}")