Skip to content

Commit 82740f3

Browse files
review @pcrespov
1 parent 51fd6c5 commit 82740f3

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

services/web/server/src/simcore_service_webserver/chatbot/_client.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from aiohttp import web
66
from pydantic import BaseModel, Field
77
from servicelib.aiohttp import status
8+
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
89
from tenacity import (
910
retry,
1011
retry_if_exception_type,
@@ -18,9 +19,6 @@
1819
_logger = logging.getLogger(__name__)
1920

2021

21-
_JSON_CONTENT_TYPE = "application/json"
22-
23-
2422
class ChatResponse(BaseModel):
2523
answer: Annotated[str, Field(description="Answer from the chatbot")]
2624

@@ -34,24 +32,22 @@ def _should_retry(response: httpx.Response | None) -> bool:
3432
)
3533

3634

37-
def _chatbot_retry():
38-
"""Retry configuration for chatbot API calls"""
39-
return retry(
40-
retry=(
41-
retry_if_result(_should_retry)
42-
| retry_if_exception_type(
43-
(
44-
httpx.ConnectError,
45-
httpx.TimeoutException,
46-
httpx.NetworkError,
47-
httpx.ProtocolError,
48-
)
35+
_CHATBOT_RETRY = retry(
36+
retry=(
37+
retry_if_result(_should_retry)
38+
| retry_if_exception_type(
39+
(
40+
httpx.ConnectError,
41+
httpx.TimeoutException,
42+
httpx.NetworkError,
43+
httpx.ProtocolError,
4944
)
50-
),
51-
stop=stop_after_attempt(3),
52-
wait=wait_exponential(multiplier=1, min=1, max=10),
53-
reraise=True,
54-
)
45+
)
46+
),
47+
stop=stop_after_attempt(3),
48+
wait=wait_exponential(multiplier=1, min=1, max=10),
49+
reraise=True,
50+
)
5551

5652

5753
class ChatbotRestClient:
@@ -63,7 +59,7 @@ async def get_settings(self) -> dict[str, Any]:
6359
"""Fetches chatbot settings"""
6460
url = httpx.URL(self._chatbot_settings.base_url).join("/v1/chat/settings")
6561

66-
@_chatbot_retry()
62+
@_CHATBOT_RETRY
6763
async def _request() -> httpx.Response:
6864
return await self._client.get(url)
6965

@@ -82,7 +78,7 @@ async def ask_question(self, question: str) -> ChatResponse:
8278
"""Asks a question to the chatbot"""
8379
url = httpx.URL(self._chatbot_settings.base_url).join("/v1/chat")
8480

85-
@_chatbot_retry()
81+
@_CHATBOT_RETRY
8682
async def _request() -> httpx.Response:
8783
return await self._client.post(
8884
url,
@@ -92,16 +88,15 @@ async def _request() -> httpx.Response:
9288
"embedding_model": self._chatbot_settings.CHATBOT_EMBEDDING_MODEL,
9389
},
9490
headers={
95-
"Content-Type": _JSON_CONTENT_TYPE,
96-
"Accept": _JSON_CONTENT_TYPE,
91+
"Content-Type": MIMETYPE_APPLICATION_JSON,
92+
"Accept": MIMETYPE_APPLICATION_JSON,
9793
},
9894
)
9995

10096
try:
10197
response = await _request()
10298
response.raise_for_status()
103-
response_data: dict[str, Any] = response.json()
104-
return ChatResponse(**response_data)
99+
return ChatResponse.model_validate(response.json())
105100
except Exception:
106101
_logger.error( # noqa: TRY400
107102
"Failed to ask question to chatbot at %s", url

services/web/server/tests/unit/with_dbs/04/test_chatbot_client.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import pytest
1111
import respx
1212
from aiohttp.test_utils import TestClient
13-
from pytest_mock import MockerFixture
1413
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
1514
from pytest_simcore.helpers.typing_env import EnvVarsDict
1615
from simcore_service_webserver.chatbot._client import (
@@ -24,7 +23,6 @@
2423
def app_environment(
2524
monkeypatch: pytest.MonkeyPatch,
2625
app_environment: EnvVarsDict,
27-
mocker: MockerFixture,
2826
):
2927
return app_environment | setenvs_from_dict(
3028
monkeypatch,

0 commit comments

Comments
 (0)