Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ and this project adheres to

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

## Fixed

- 🐛(frontend) Fix hidden menu on Firefox #468
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is wrongly committed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its ok, I don't want to make a commit for that.

- 🐛(backend) fix sanitize problem IA #490


## [1.8.2] - 2024-11-28

Expand Down
10 changes: 7 additions & 3 deletions src/backend/core/services/ai_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ def call_ai_api(self, system_content, text):
)

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")
Expand Down
21 changes: 21 additions & 0 deletions src/backend/core/tests/test_services_ai_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,24 @@ def test_api_ai__success_sanitize(mock_create):
response = AIService().transform("hello", "prompt")

assert response == {"answer": "Salut\n \tle \nmonde"}


@override_settings(
AI_BASE_URL="http://example.com", AI_API_KEY="test-key", AI_MODEL="test-model"
)
@patch("openai.resources.chat.completions.Completions.create")
def test_api_ai__success_when_sanitize_fails(mock_create):
"""The AI request should work as expected even with badly formatted response."""

# pylint: disable=C0303
answer = """{
"answer" :
"Salut le monde"
}"""
mock_create.return_value = MagicMock(
choices=[MagicMock(message=MagicMock(content=answer))]
)

response = AIService().transform("hello", "prompt")

assert response == {"answer": "Salut le monde"}
Loading