Skip to content

Commit b679dfd

Browse files
feat(chat): handle other languages (#49)
* feat(chat): handle other languages * feat(chat): handle other languages: lint * fix tests
1 parent cb1616b commit b679dfd

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

src/app/api/api_v1/endpoints/user.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from datetime import datetime, timedelta
21
import uuid
2+
from datetime import datetime, timedelta
33

44
from fastapi import APIRouter, HTTPException, Request
55
from sqlalchemy.sql import select
@@ -51,7 +51,9 @@ def handle_user(user_id: uuid.UUID | None = None):
5151
description="Create a new session in the user db",
5252
response_model=dict,
5353
)
54-
def handle_session(user_id: uuid.UUID, request: Request, session_id: uuid.UUID | None = None):
54+
def handle_session(
55+
user_id: uuid.UUID, request: Request, session_id: uuid.UUID | None = None
56+
):
5557
"""
5658
Create a new session in the session db
5759
If session_id is provided, check if the session exists in the db and if the end_at is still more recent than time now

src/app/services/abst_chat.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,15 @@ async def _detect_lang_with_llm(self, query: str) -> Dict[str, str]:
126126
},
127127
)
128128

129-
try:
130-
assert isinstance(detected_lang, dict)
131-
132-
if detected_lang["ISO_CODE"] not in ["en", "fr"]:
133-
raise LanguageNotSupportedError()
134-
135-
return detected_lang
136-
except AssertionError:
137-
logger.error("api_error=assertion_error, response=%s", detected_lang)
129+
if isinstance(detected_lang, str):
130+
jsn = extract_json_from_response(detected_lang)
131+
elif isinstance(detected_lang, dict):
132+
jsn = detected_lang
133+
else:
138134
raise ValueError("Invalid response from model")
139135

136+
return jsn
137+
140138
@log_time_and_error
141139
async def _detect_past_message_ref(
142140
self, query: str, history: List[Dict[str, str]]
@@ -385,7 +383,9 @@ async def chat_message(
385383
Returns:
386384
str: The chat message content.
387385
"""
388-
ISO_CODE = {"ISO_CODE": "en"}
386+
387+
# ISO_CODE = {"ISO_CODE": "en"}
388+
389389
if should_check_lang:
390390
ISO_CODE = await self._detect_language(query)
391391

src/app/services/prompts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
160160
Audience: Computer program.
161161
162-
Response: The response should be a single line with the following key-value structure: "ISO_CODE": "en"
162+
Response: The response should be in a JSON format with the following key-value structure: "ISO_CODE": "en"
163163
"""
164164

165165

src/app/tests/api/api_v1/test_user.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ async def test_create_user_when_already_exists(self, session_maker_mock, *mocks)
4343
headers={"X-API-Key": "test"},
4444
)
4545
self.assertEqual(response.status_code, 200)
46-
self.assertEqual(response.json(), {"user_id": "cfc8072c-a055-442a-9878-b5a73d9141b2"})
46+
self.assertEqual(
47+
response.json(), {"user_id": "cfc8072c-a055-442a-9878-b5a73d9141b2"}
48+
)
4749

4850
@mock.patch("src.app.api.api_v1.endpoints.user.session_maker")
4951
async def test_create_user_handles_exception(self, session_maker_mock, *mocks):
@@ -87,11 +89,16 @@ async def test_create_session_existing_valid_session(
8789

8890
response = client.post(
8991
f"{settings.API_V1_STR}/user/session",
90-
params={"user_id": "19f11fa7-87ef-40af-aa61-96a099bd04be", "session_id": "8178c3c4-9379-4997-a6e6-f1ccea7a30a9"},
92+
params={
93+
"user_id": "19f11fa7-87ef-40af-aa61-96a099bd04be",
94+
"session_id": "8178c3c4-9379-4997-a6e6-f1ccea7a30a9",
95+
},
9196
headers={"X-API-Key": "test"},
9297
)
9398
self.assertEqual(response.status_code, 200)
94-
self.assertEqual(response.json(), {"session_id": "8178c3c4-9379-4997-a6e6-f1ccea7a30a9"})
99+
self.assertEqual(
100+
response.json(), {"session_id": "8178c3c4-9379-4997-a6e6-f1ccea7a30a9"}
101+
)
95102

96103
@mock.patch("src.app.api.api_v1.endpoints.user.session_maker")
97104
async def test_create_session_create_new_when_not_found(

src/app/tests/services/test_abst_chat.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ async def test_lang_ok(self, *mocks):
3333
async def test_lang_not_supported(self, *mocks):
3434
mocked_chat = {"ISO_CODE": "pt"}
3535
self.chat.chat_client.completion = mock.AsyncMock(return_value=mocked_chat)
36-
with self.assertRaises(LanguageNotSupportedError):
37-
await self.chat._detect_language("fake message")
3836

3937
mocked_chat = "not json format"
4038
self.chat.chat_client.completion = mock.AsyncMock(return_value=mocked_chat)

0 commit comments

Comments
 (0)