add the code
Browse files- response_formatter.py +39 -0
response_formatter.py
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from schemas import CodeTaskType, CodeXResponse
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def clean_text(text: str) -> str:
|
| 5 |
+
if not text:
|
| 6 |
+
return ""
|
| 7 |
+
return text.strip()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def build_response(
|
| 11 |
+
task_type: CodeTaskType,
|
| 12 |
+
model_output: str,
|
| 13 |
+
model_used: str,
|
| 14 |
+
used_fallback: bool,
|
| 15 |
+
) -> CodeXResponse:
|
| 16 |
+
cleaned_output = clean_text(model_output)
|
| 17 |
+
|
| 18 |
+
return CodeXResponse(
|
| 19 |
+
task_type=task_type,
|
| 20 |
+
answer=cleaned_output,
|
| 21 |
+
model_used=model_used,
|
| 22 |
+
used_fallback=used_fallback,
|
| 23 |
+
success=True,
|
| 24 |
+
error=None,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def build_error_response(
|
| 29 |
+
task_type: CodeTaskType,
|
| 30 |
+
error_message: str,
|
| 31 |
+
) -> CodeXResponse:
|
| 32 |
+
return CodeXResponse(
|
| 33 |
+
task_type=task_type,
|
| 34 |
+
answer="",
|
| 35 |
+
model_used="none",
|
| 36 |
+
used_fallback=False,
|
| 37 |
+
success=False,
|
| 38 |
+
error=clean_text(error_message),
|
| 39 |
+
)
|