Fix-up Search Actor GPT's response for decoding it as valid JSON

This commit is contained in:
Debanjum Singh Solanky 2023-03-18 03:30:30 -06:00
parent f63fd0995e
commit 939d7731da

View file

@ -167,7 +167,13 @@ Q: {text}
# Extract, Clean Message from GPT's Response
response_text = response["choices"][0]["text"]
try:
questions = json.loads(response_text.strip(empty_escape_sequences))
questions = json.loads(
# Clean response to increase likelihood of valid JSON. E.g replace ' with " to enclose strings
response_text.strip(empty_escape_sequences)
.replace("['", '["')
.replace("']", '"]')
.replace("', '", '", "')
)
except json.decoder.JSONDecodeError:
logger.warn(f"GPT returned invalid JSON. Set question to empty list.\n{response_text}")
questions = [text]