Skip to content

Commit fdad989

Browse files
review @pcrespov
1 parent e18dcb9 commit fdad989

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
import logging
7-
from typing import Any, Final
7+
from typing import Annotated, Any, Final
88
from urllib.parse import urljoin
99

1010
import httpx
@@ -28,7 +28,7 @@
2828

2929

3030
class ChatResponse(BaseModel):
31-
answer: str = Field(description="Answer from the chatbot")
31+
answer: Annotated[str, Field(description="Answer from the chatbot")]
3232

3333

3434
def _should_retry(response: httpx.Response | None) -> bool:
@@ -76,8 +76,7 @@ async def _request() -> httpx.Response:
7676
try:
7777
response = await _request()
7878
response.raise_for_status()
79-
response_data: dict[str, Any] = response.json()
80-
return response_data
79+
return response.json()
8180
except Exception:
8281
_logger.error( # noqa: TRY400
8382
"Failed to fetch chatbot settings from %s", url
@@ -86,7 +85,7 @@ async def _request() -> httpx.Response:
8685

8786
async def ask_question(self, question: str) -> ChatResponse:
8887
"""Asks a question to the chatbot"""
89-
url = urljoin(f"{self._chatbot_settings.base_url}", "/v1/chat")
88+
url = urljoin(self._chatbot_settings.base_url, "/v1/chat")
9089

9190
@_chatbot_retry()
9291
async def _request() -> httpx.Response:
@@ -135,6 +134,14 @@ async def setup_chatbot_rest_client(app: web.Application) -> None:
135134

136135
app[_APPKEY] = client
137136

137+
# Add cleanup on app shutdown
138+
async def cleanup_chatbot_client(app: web.Application) -> None:
139+
client = app.get(_APPKEY)
140+
if client:
141+
await client._client.aclose() # noqa: SLF001
142+
143+
app.on_cleanup.append(cleanup_chatbot_client)
144+
138145

139146
def get_chatbot_rest_client(app: web.Application) -> ChatbotRestClient:
140147
app_key: ChatbotRestClient = app[_APPKEY]

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
from functools import cached_property
22

33
from aiohttp import web
4+
from models_library.basic_types import PortInt
5+
from pydantic import ConfigDict
46
from settings_library.base import BaseCustomSettings
57
from settings_library.utils_service import MixinServiceSettings, URLPart
68

79
from ..application_keys import APP_SETTINGS_APPKEY
810

911

1012
class ChatbotSettings(BaseCustomSettings, MixinServiceSettings):
13+
model_config = ConfigDict(str_strip_whitespace=True, str_min_length=1)
14+
1115
CHATBOT_HOST: str
12-
CHATBOT_PORT: int
16+
CHATBOT_PORT: PortInt
1317
CHATBOT_LLM_MODEL: str = "gpt-3.5-turbo"
1418
CHATBOT_EMBEDDING_MODEL: str = "openai/text-embedding-3-large"
1519

0 commit comments

Comments
 (0)