Update response_formatter.py
Browse files- response_formatter.py +76 -9
response_formatter.py
CHANGED
|
@@ -1,12 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from schemas import CodeTaskType, CodeXResponse, ResponseMeta
|
| 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,
|
|
@@ -14,15 +69,24 @@ def build_response(
|
|
| 14 |
used_fallback: bool,
|
| 15 |
retrieval_used: bool = False,
|
| 16 |
source_count: int = 0,
|
|
|
|
| 17 |
) -> CodeXResponse:
|
| 18 |
cleaned_output = clean_text(model_output)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return CodeXResponse(
|
| 21 |
task_type=task_type,
|
| 22 |
-
answer=
|
| 23 |
-
code_output=
|
| 24 |
-
explanation=
|
| 25 |
-
warnings=
|
| 26 |
sources=[],
|
| 27 |
needs_clarification=False,
|
| 28 |
meta=ResponseMeta(
|
|
@@ -30,7 +94,7 @@ def build_response(
|
|
| 30 |
fallback_used=used_fallback,
|
| 31 |
retrieval_used=retrieval_used,
|
| 32 |
source_count=source_count,
|
| 33 |
-
processing_time_ms=
|
| 34 |
),
|
| 35 |
)
|
| 36 |
|
|
@@ -38,13 +102,16 @@ def build_response(
|
|
| 38 |
def build_error_response(
|
| 39 |
task_type: CodeTaskType,
|
| 40 |
error_message: str,
|
|
|
|
| 41 |
) -> CodeXResponse:
|
|
|
|
|
|
|
| 42 |
return CodeXResponse(
|
| 43 |
task_type=task_type,
|
| 44 |
-
answer="",
|
| 45 |
code_output=None,
|
| 46 |
explanation=None,
|
| 47 |
-
warnings=[
|
| 48 |
sources=[],
|
| 49 |
needs_clarification=False,
|
| 50 |
meta=ResponseMeta(
|
|
@@ -52,6 +119,6 @@ def build_error_response(
|
|
| 52 |
fallback_used=False,
|
| 53 |
retrieval_used=False,
|
| 54 |
source_count=0,
|
| 55 |
-
processing_time_ms=
|
| 56 |
),
|
| 57 |
)
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
from schemas import CodeTaskType, CodeXResponse, ResponseMeta
|
| 5 |
|
| 6 |
|
| 7 |
+
def clean_text(text: Optional[str]) -> str:
|
| 8 |
if not text:
|
| 9 |
return ""
|
| 10 |
+
text = str(text).strip()
|
| 11 |
+
text = re.sub(r"\r\n", "\n", text)
|
| 12 |
+
text = re.sub(r"\n{3,}", "\n\n", text)
|
| 13 |
return text.strip()
|
| 14 |
|
| 15 |
|
| 16 |
+
def extract_section(text: str, label: str) -> Optional[str]:
|
| 17 |
+
pattern = rf"{label}:\s*(.*?)(?=\n[A-Z][A-Za-z ]*:\s|\Z)"
|
| 18 |
+
match = re.search(pattern, text, flags=re.DOTALL)
|
| 19 |
+
if match:
|
| 20 |
+
value = clean_text(match.group(1))
|
| 21 |
+
return value if value else None
|
| 22 |
+
return None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def extract_code_block(text: str) -> Optional[str]:
|
| 26 |
+
fenced = re.findall(r"```(?:\w+)?\n(.*?)```", text, flags=re.DOTALL)
|
| 27 |
+
if fenced:
|
| 28 |
+
return clean_text(fenced[0])
|
| 29 |
+
|
| 30 |
+
code_section = extract_section(text, "Code")
|
| 31 |
+
if code_section:
|
| 32 |
+
return code_section
|
| 33 |
+
|
| 34 |
+
return None
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def extract_explanation(text: str) -> Optional[str]:
|
| 38 |
+
for label in ["Explanation", "Root Cause", "Review", "Suggestions"]:
|
| 39 |
+
value = extract_section(text, label)
|
| 40 |
+
if value:
|
| 41 |
+
return value
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def build_main_answer(text: str, explanation: Optional[str], code_output: Optional[str]) -> str:
|
| 46 |
+
if explanation:
|
| 47 |
+
return explanation
|
| 48 |
+
|
| 49 |
+
cleaned = clean_text(text)
|
| 50 |
+
|
| 51 |
+
if code_output:
|
| 52 |
+
cleaned = re.sub(r"```(?:\w+)?\n.*?```", "", cleaned, flags=re.DOTALL)
|
| 53 |
+
cleaned = re.sub(r"Code:\s*.*", "", cleaned, flags=re.DOTALL)
|
| 54 |
+
cleaned = clean_text(cleaned)
|
| 55 |
+
|
| 56 |
+
if cleaned:
|
| 57 |
+
return cleaned
|
| 58 |
+
|
| 59 |
+
if code_output:
|
| 60 |
+
return "Code generated successfully."
|
| 61 |
+
|
| 62 |
+
return "Request processed successfully."
|
| 63 |
+
|
| 64 |
+
|
| 65 |
def build_response(
|
| 66 |
task_type: CodeTaskType,
|
| 67 |
model_output: str,
|
|
|
|
| 69 |
used_fallback: bool,
|
| 70 |
retrieval_used: bool = False,
|
| 71 |
source_count: int = 0,
|
| 72 |
+
processing_time_ms: Optional[int] = None,
|
| 73 |
) -> CodeXResponse:
|
| 74 |
cleaned_output = clean_text(model_output)
|
| 75 |
|
| 76 |
+
code_output = extract_code_block(cleaned_output)
|
| 77 |
+
explanation = extract_explanation(cleaned_output)
|
| 78 |
+
answer = build_main_answer(cleaned_output, explanation, code_output)
|
| 79 |
+
|
| 80 |
+
warnings = []
|
| 81 |
+
if not cleaned_output:
|
| 82 |
+
warnings.append("Model returned an empty response.")
|
| 83 |
+
|
| 84 |
return CodeXResponse(
|
| 85 |
task_type=task_type,
|
| 86 |
+
answer=answer,
|
| 87 |
+
code_output=code_output,
|
| 88 |
+
explanation=explanation,
|
| 89 |
+
warnings=warnings,
|
| 90 |
sources=[],
|
| 91 |
needs_clarification=False,
|
| 92 |
meta=ResponseMeta(
|
|
|
|
| 94 |
fallback_used=used_fallback,
|
| 95 |
retrieval_used=retrieval_used,
|
| 96 |
source_count=source_count,
|
| 97 |
+
processing_time_ms=processing_time_ms,
|
| 98 |
),
|
| 99 |
)
|
| 100 |
|
|
|
|
| 102 |
def build_error_response(
|
| 103 |
task_type: CodeTaskType,
|
| 104 |
error_message: str,
|
| 105 |
+
processing_time_ms: Optional[int] = None,
|
| 106 |
) -> CodeXResponse:
|
| 107 |
+
cleaned_error = clean_text(error_message)
|
| 108 |
+
|
| 109 |
return CodeXResponse(
|
| 110 |
task_type=task_type,
|
| 111 |
+
answer="Request processing failed.",
|
| 112 |
code_output=None,
|
| 113 |
explanation=None,
|
| 114 |
+
warnings=[cleaned_error] if cleaned_error else ["Unknown error occurred."],
|
| 115 |
sources=[],
|
| 116 |
needs_clarification=False,
|
| 117 |
meta=ResponseMeta(
|
|
|
|
| 119 |
fallback_used=False,
|
| 120 |
retrieval_used=False,
|
| 121 |
source_count=0,
|
| 122 |
+
processing_time_ms=processing_time_ms,
|
| 123 |
),
|
| 124 |
)
|