Skip to content

Commit e7a5300

Browse files
feat(tutor): adds retries to tutor endpoints (#102)
* feat(tutor): adds retries to tutor endpoints * lint
1 parent 867bf94 commit e7a5300

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Annotated
22

3+
import backoff
34
from fastapi import APIRouter, Depends, File, Response, UploadFile
45

56
from src.app.api.dependencies import get_settings
@@ -34,7 +35,25 @@
3435
settings = get_settings()
3536

3637

38+
def backoff_hdlr(details):
39+
logger.info(
40+
"Backing off {wait:0.1f} seconds after {tries} tries "
41+
"calling function {target} with args {args} and kwargs "
42+
"{kwargs}".format(**details)
43+
)
44+
45+
3746
@router.post("/files/content")
47+
@backoff.on_exception(
48+
wait_gen=backoff.expo,
49+
exception=Exception,
50+
logger=logger,
51+
max_tries=3,
52+
max_time=180,
53+
jitter=backoff.random_jitter,
54+
on_backoff=backoff_hdlr,
55+
factor=2,
56+
)
3857
async def extract_files_content(
3958
files: Annotated[list[UploadFile], File()],
4059
response: Response,
@@ -65,7 +84,7 @@ async def extract_files_content(
6584
except Exception as e:
6685
logger.error(f"Error in extractor schema: {e}")
6786
response.status_code = 204
68-
return None
87+
raise e
6988

7089

7190
@router.post("/search_extracts")
@@ -208,6 +227,16 @@ async def tutor_search(
208227
return resp
209228

210229

230+
@backoff.on_exception(
231+
wait_gen=backoff.expo,
232+
exception=Exception,
233+
logger=logger,
234+
max_tries=3,
235+
max_time=180,
236+
jitter=backoff.random_jitter,
237+
on_backoff=backoff_hdlr,
238+
factor=2,
239+
)
211240
@router.post("/syllabus")
212241
async def create_syllabus(
213242
body: TutorSyllabusRequest, lang: str = "en"
@@ -260,6 +289,16 @@ async def create_syllabus(
260289
"""
261290

262291

292+
@backoff.on_exception(
293+
wait_gen=backoff.expo,
294+
exception=Exception,
295+
logger=logger,
296+
max_tries=3,
297+
max_time=180,
298+
jitter=backoff.random_jitter,
299+
on_backoff=backoff_hdlr,
300+
factor=2,
301+
)
263302
@router.post("/syllabus/feedback")
264303
async def handle_syllabus_feedback(
265304
body: SyllabusFeedback, chatfactory=Depends(get_chat_service)
@@ -300,3 +339,4 @@ async def handle_syllabus_feedback(
300339

301340
except Exception as e:
302341
logger.error(f"Error in chat schema: {e}")
342+
raise e

src/app/models/documents.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class Collection_schema(BaseModel):
1414
class DocumentPayloadModel(BaseModel):
1515
document_corpus: str
1616
document_desc: str
17-
document_details: dict[str, list[dict] | list[str] | str | int | float | dict | None]
17+
document_details: dict[
18+
str, list[dict] | list[str] | str | int | float | dict | None
19+
]
1820
document_id: uuid.UUID
1921
document_lang: str
2022
document_sdg: list[int]

0 commit comments

Comments
 (0)