Skip to content

Commit ed90769

Browse files
committed
🐛(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.
1 parent a8310fa commit ed90769

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ and this project adheres to
1818

1919
- 🔒️(collaboration) increase collaboration access security #472
2020
- 🔨(frontend) encapsulated title to its own component #474
21-
- 🐛(frontend) Fix hidden menu on Firefox #468
2221
- ⚡️(backend) optimize number of queries on document list view #411
2322
- ♻️(frontend) stop to use provider with version #480
2423
- 🚚(collaboration) change the websocket key name #480
2524

25+
## Fixed
26+
27+
- 🐛(frontend) Fix hidden menu on Firefox #468
28+
- 🐛(backend) fix sanitize problem IA #490
29+
2630

2731
## [1.8.2] - 2024-11-28
2832

src/backend/core/services/ai_services.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ def call_ai_api(self, system_content, text):
6767
)
6868

6969
content = response.choices[0].message.content
70-
sanitized_content = re.sub(r"(?<!\\)\n", "\\\\n", content)
71-
sanitized_content = re.sub(r"(?<!\\)\t", "\\\\t", sanitized_content)
7270

73-
json_response = json.loads(sanitized_content)
71+
try:
72+
sanitized_content = re.sub(r"(?<!\\)\n", "\\\\n", content)
73+
sanitized_content = re.sub(r"(?<!\\)\t", "\\\\t", sanitized_content)
74+
75+
json_response = json.loads(sanitized_content)
76+
except (json.JSONDecodeError, IndexError):
77+
json_response = json.loads(content)
7478

7579
if "answer" not in json_response:
7680
raise RuntimeError("AI response does not contain an answer")

src/backend/core/tests/test_services_ai_services.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,24 @@ def test_api_ai__success_sanitize(mock_create):
102102
response = AIService().transform("hello", "prompt")
103103

104104
assert response == {"answer": "Salut\n \tle \nmonde"}
105+
106+
107+
@override_settings(
108+
AI_BASE_URL="http://example.com", AI_API_KEY="test-key", AI_MODEL="test-model"
109+
)
110+
@patch("openai.resources.chat.completions.Completions.create")
111+
def test_api_ai__success_when_sanitize_fails(mock_create):
112+
"""The AI request should work as expected even with badly formatted response."""
113+
114+
# pylint: disable=C0303
115+
answer = """{
116+
"answer" :
117+
"Salut le monde"
118+
}"""
119+
mock_create.return_value = MagicMock(
120+
choices=[MagicMock(message=MagicMock(content=answer))]
121+
)
122+
123+
response = AIService().transform("hello", "prompt")
124+
125+
assert response == {"answer": "Salut le monde"}

0 commit comments

Comments
 (0)