55from aiohttp import web
66from pydantic import BaseModel , Field
77from servicelib .aiohttp import status
8+ from servicelib .mimetype_constants import MIMETYPE_APPLICATION_JSON
89from tenacity import (
910 retry ,
1011 retry_if_exception_type ,
1819_logger = logging .getLogger (__name__ )
1920
2021
21- _JSON_CONTENT_TYPE = "application/json"
22-
23-
2422class 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
5753class 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
0 commit comments