Make the JSON porse a bit more robust.
This commit is contained in:
parent
b1ece64874
commit
b26b98611a
@ -32,28 +32,21 @@ def parse_response(content: str) -> Dict:
|
|||||||
def parse_maybe_json(json_string):
|
def parse_maybe_json(json_string):
|
||||||
if json_string is None:
|
if json_string is None:
|
||||||
return None
|
return None
|
||||||
if isinstance(json_string, list):
|
if isinstance(json_string, (list, dict)):
|
||||||
return ' '.join([str(x) for x in json_string])
|
return ' '.join(map(str, (json_string.values() if isinstance(json_string, dict) else json_string)))
|
||||||
if isinstance(json_string, dict):
|
|
||||||
return ' '.join([str(x) for x in json_string.values()])
|
|
||||||
json_string = str(json_string).strip()
|
json_string = str(json_string).strip()
|
||||||
try:
|
try:
|
||||||
parsed_json = parse_response(json_string)
|
parsed_json = parse_response(json_string)
|
||||||
except Exception:
|
except Exception:
|
||||||
if json_string.startswith('{') and json_string.endswith('}'):
|
for b, e in [('{', '}'), ('[', ']')]:
|
||||||
return parse_maybe_json(json_string[1:-1])
|
if json_string.startswith(b) and json_string.endswith(e):
|
||||||
|
return parse_maybe_json(json_string[1:-1])
|
||||||
return json_string
|
return json_string
|
||||||
|
if isinstance(parsed_json, str):
|
||||||
|
return parsed_json
|
||||||
if isinstance(parsed_json, (list, dict)):
|
if isinstance(parsed_json, (list, dict)):
|
||||||
concatenated_values = []
|
return '\n'.join(map(str, (parsed_json.values() if isinstance(parsed_json, dict) else parsed_json)))
|
||||||
for value in parsed_json.values() if isinstance(parsed_json, dict) else parsed_json:
|
return str(parsed_json)
|
||||||
concatenated_values.append(str(value))
|
|
||||||
return '\n'.join(concatenated_values)
|
|
||||||
|
|
||||||
result = str(parsed_json)
|
|
||||||
if result.lower() in ('', 'none', 'null', '"none"', '"null"', "'none'", "'null'"):
|
|
||||||
return None
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
class AIMessageBase(object):
|
class AIMessageBase(object):
|
||||||
|
|||||||
@ -77,6 +77,15 @@ class TestFunctionality(TestBotBase):
|
|||||||
json_struct = '{"This is a string."}'
|
json_struct = '{"This is a string."}'
|
||||||
expected_output = 'This is a string.'
|
expected_output = 'This is a string.'
|
||||||
self.assertEqual(parse_maybe_json(json_struct), expected_output)
|
self.assertEqual(parse_maybe_json(json_struct), expected_output)
|
||||||
|
json_struct = '["This is a string."]'
|
||||||
|
expected_output = 'This is a string.'
|
||||||
|
self.assertEqual(parse_maybe_json(json_struct), expected_output)
|
||||||
|
json_struct = '{This is a string.}'
|
||||||
|
expected_output = 'This is a string.'
|
||||||
|
self.assertEqual(parse_maybe_json(json_struct), expected_output)
|
||||||
|
json_struct = '[This is a string.]'
|
||||||
|
expected_output = 'This is a string.'
|
||||||
|
self.assertEqual(parse_maybe_json(json_struct), expected_output)
|
||||||
|
|
||||||
async def test_message_lings(self) -> None:
|
async def test_message_lings(self) -> None:
|
||||||
request = AIMessage('Lala', 'Hello there!', 'chat', False,)
|
request = AIMessage('Lala', 'Hello there!', 'chat', False,)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user