🐛(backend) fix sanitize problem IA

Albert send us back a malformed IA json, the
sanitize function was not able to handle it correctly.
We add a try catch on it, to not use the sanitizer if
the json.loads fails.
This commit is contained in:
Anthony LC
2024-12-11 13:57:05 +01:00
committed by Anthony LC
parent a8310fa0ff
commit ed90769081
3 changed files with 33 additions and 4 deletions

View File

@@ -67,10 +67,14 @@ class AIService:
)
content = response.choices[0].message.content
sanitized_content = re.sub(r"(?<!\\)\n", "\\\\n", content)
sanitized_content = re.sub(r"(?<!\\)\t", "\\\\t", sanitized_content)
json_response = json.loads(sanitized_content)
try:
sanitized_content = re.sub(r"(?<!\\)\n", "\\\\n", content)
sanitized_content = re.sub(r"(?<!\\)\t", "\\\\t", sanitized_content)
json_response = json.loads(sanitized_content)
except (json.JSONDecodeError, IndexError):
json_response = json.loads(content)
if "answer" not in json_response:
raise RuntimeError("AI response does not contain an answer")