44"""
55
66import logging
7- from typing import Any , Final
7+ from typing import Annotated , Any , Final
88from urllib .parse import urljoin
99
1010import httpx
2828
2929
3030class ChatResponse (BaseModel ):
31- answer : str = Field (description = "Answer from the chatbot" )
31+ answer : Annotated [ str , Field (description = "Answer from the chatbot" )]
3232
3333
3434def _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
139146def get_chatbot_rest_client (app : web .Application ) -> ChatbotRestClient :
140147 app_key : ChatbotRestClient = app [_APPKEY ]
0 commit comments