Improve JSON parsing.
This commit is contained in:
@@ -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)}")
|
||||
|
||||
Reference in New Issue
Block a user