Skip to content

Commit 997e3fb

Browse files
feat(tutor): respond with 204 when wrongly formatted(#85)
* feat(tutor): respond with 204 when wrongly formatted * fix: prompt * adds summaries model
1 parent ea985ee commit 997e3fb

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

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

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

3-
from fastapi import APIRouter, File, HTTPException, Response, UploadFile
3+
from fastapi import APIRouter, File, Response, UploadFile
44

55
from src.app.api.dependencies import get_settings
66
from src.app.models.search import EnhancedSearchQuery
@@ -12,6 +12,7 @@
1212
from src.app.services.tutor.agents import TEMPLATES
1313
from src.app.services.tutor.models import (
1414
ExtractorOutputList,
15+
SummariesList,
1516
SyllabusFeedback,
1617
SyllabusResponse,
1718
SyllabusResponseAgent,
@@ -46,7 +47,7 @@
4647

4748
@router.post("/files/content")
4849
async def extract_files_content(
49-
files: Annotated[list[UploadFile], File()],
50+
files: Annotated[list[UploadFile], File()], response: Response
5051
) -> ExtractorOutputList | None:
5152
files_content = await get_files_content(files)
5253
files_content_str = ("__DOCUMENT_SEPARATOR__").join(files_content)
@@ -72,18 +73,19 @@ async def extract_files_content(
7273

7374
except Exception as e:
7475
logger.error(f"Error in extractor schema: {e}")
75-
HTTPException(status_code=400, detail="error in response schema")
76+
response.status_code = 204
77+
return None
7678

7779

7880
@router.post("/search_extracts")
7981
async def tutor_search_extract(
80-
summaries: list[str],
82+
summaries: SummariesList,
8183
response: Response,
8284
):
8385

8486
try:
8587
qp = EnhancedSearchQuery(
86-
query=summaries,
88+
query=summaries.summaries,
8789
nb_results=10,
8890
sdg_filter=None,
8991
corpora=None,
@@ -154,11 +156,12 @@ async def tutor_search(
154156
else:
155157
raise ValueError("Unexpected response format")
156158

159+
print(jsn)
157160
themes_extracted = ExtractorOutputList(**jsn)
158161

159162
except Exception as e:
160163
logger.error(f"Error in chat schema: {e}")
161-
# todo: handle error
164+
response.status_code = 204
162165
return TutorSearchResponse(
163166
extracts=[],
164167
nb_results=0,

src/app/services/tutor/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
from qdrant_client.models import ScoredPoint
66

77

8+
class SummariesList(BaseModel):
9+
summaries: list[str]
10+
11+
812
class ExtractorOutput(BaseModel):
913
summary: str
1014
themes: list[str]

src/app/services/tutor/prompts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
role="An assistant to summarize a text and extract the main themes from it",
33
backstory="You are specialised in analysing documents, summarizing them and extracting the main themes. You value precision and clarity.",
44
goal="Analyse each document, summarize it and extract the main themes, explaining why each theme was identified.",
5-
expected_output="You must follow the following JSON schema: {extracts: [{'original_document': 'Document', 'summary': 'Summary', 'themes': ['Theme 1', 'Theme 2', ...]}, {'original_document': 'Document', 'summary': 'Sumamry', 'themes': ['Theme 1', 'Theme 2', ...]}, ...]} an entry by document",
5+
expected_output="You must follow the following JSON schema: {extracts: [{'original_document': 'Document', 'summary': 'Summary', 'themes': ['Theme 1', 'Theme 2', ...]}, {'original_document': 'Document', 'summary': 'Sumamry', 'themes': ['Theme 1', 'Theme 2', ...]}, ...]} an entry by document make sure to use double quotes when formating the JSON",
66
"""
77

88
extractor_user_prompt = """

src/app/tests/api/api_v1/test_tutor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ def test_tutor_file(self, *mocks):
3939
files={"files": ("test.txt", file)},
4040
headers={"x-API-Key": "test"},
4141
)
42-
assert reponse.status_code == 200
42+
assert reponse.status_code == 204

0 commit comments

Comments
 (0)