hashan-7 commited on
Commit
d54bf80
·
verified ·
1 Parent(s): 072e615

update code

Browse files
Files changed (1) hide show
  1. response_formatter.py +113 -20
response_formatter.py CHANGED
@@ -34,34 +34,126 @@ def extract_code_block(text: str) -> Optional[str]:
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,
@@ -73,9 +165,10 @@ def build_response(
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:
 
34
  return None
35
 
36
 
37
+ def extract_explanation(text: str, task_type: CodeTaskType) -> Optional[str]:
38
+ if task_type == CodeTaskType.FIX:
39
+ return extract_section(text, "Explanation") or extract_section(text, "Root Cause")
40
+
41
+ if task_type == CodeTaskType.REVIEW:
42
+ review = extract_section(text, "Review")
43
+ suggestions = extract_section(text, "Suggestions")
44
+ if review and suggestions:
45
+ return f"{review}\n\nSuggestions:\n{suggestions}"
46
+ return review or suggestions
47
+
48
+ if task_type == CodeTaskType.REFACTOR:
49
+ return extract_section(text, "Explanation")
50
+
51
+ if task_type == CodeTaskType.EXPLAIN:
52
+ return extract_section(text, "Explanation")
53
+
54
+ if task_type == CodeTaskType.GENERATE:
55
+ return extract_section(text, "Explanation")
56
+
57
+ return (
58
+ extract_section(text, "Explanation")
59
+ or extract_section(text, "Root Cause")
60
+ or extract_section(text, "Review")
61
+ or extract_section(text, "Suggestions")
62
+ )
63
 
64
 
65
+ def strip_structured_sections(text: str) -> str:
66
+ text = re.sub(r"```(?:\w+)?\n.*?```", "", text, flags=re.DOTALL)
67
+ text = re.sub(r"Code:\s*.*", "", text, flags=re.DOTALL)
68
+ text = re.sub(r"Explanation:\s*.*", "", text, flags=re.DOTALL)
69
+ text = re.sub(r"Root Cause:\s*.*", "", text, flags=re.DOTALL)
70
+ text = re.sub(r"Review:\s*.*", "", text, flags=re.DOTALL)
71
+ text = re.sub(r"Suggestions:\s*.*", "", text, flags=re.DOTALL)
72
+ return clean_text(text)
73
 
 
74
 
75
+ def first_sentence(text: str) -> str:
76
+ text = clean_text(text)
77
+ if not text:
78
+ return ""
79
+ match = re.split(r"(?<=[.!?])\s+", text, maxsplit=1)
80
+ return clean_text(match[0]) if match else text
81
+
82
 
83
+ def build_main_answer(
84
+ task_type: CodeTaskType,
85
+ raw_text: str,
86
+ explanation: Optional[str],
87
+ code_output: Optional[str],
88
+ ) -> str:
89
+ cleaned = clean_text(raw_text)
90
+
91
+ if task_type == CodeTaskType.GENERATE:
92
+ if explanation:
93
+ return first_sentence(explanation)
94
+ if code_output:
95
+ return "Code generated successfully."
96
+ return "Generation completed."
97
+
98
+ if task_type == CodeTaskType.EXPLAIN:
99
+ if explanation:
100
+ return first_sentence(explanation)
101
+ stripped = strip_structured_sections(cleaned)
102
+ if stripped:
103
+ return first_sentence(stripped)
104
+ return "Code explanation generated."
105
+
106
+ if task_type == CodeTaskType.FIX:
107
+ root_cause = extract_section(cleaned, "Root Cause")
108
+ if root_cause:
109
+ return first_sentence(root_cause)
110
+ if explanation:
111
+ return first_sentence(explanation)
112
+ if code_output:
113
+ return "Code fix generated successfully."
114
+ return "Fix completed."
115
+
116
+ if task_type == CodeTaskType.REVIEW:
117
+ review = extract_section(cleaned, "Review")
118
+ if review:
119
+ return first_sentence(review)
120
+ if explanation:
121
+ return first_sentence(explanation)
122
+ stripped = strip_structured_sections(cleaned)
123
+ if stripped:
124
+ return first_sentence(stripped)
125
+ return "Code review completed."
126
+
127
+ if task_type == CodeTaskType.REFACTOR:
128
+ if explanation:
129
+ return first_sentence(explanation)
130
+ if code_output:
131
+ return "Code refactored successfully."
132
+ return "Refactor completed."
133
+
134
+ stripped = strip_structured_sections(cleaned)
135
+ if stripped:
136
+ return first_sentence(stripped)
137
+
138
+ if explanation:
139
+ return first_sentence(explanation)
140
 
141
  if code_output:
142
+ return "Request processed successfully."
143
 
144
  return "Request processed successfully."
145
 
146
 
147
+ def should_include_code_output(task_type: CodeTaskType, code_output: Optional[str]) -> Optional[str]:
148
+ if not code_output:
149
+ return None
150
+
151
+ if task_type == CodeTaskType.EXPLAIN:
152
+ return None
153
+
154
+ return code_output
155
+
156
+
157
  def build_response(
158
  task_type: CodeTaskType,
159
  model_output: str,
 
165
  ) -> CodeXResponse:
166
  cleaned_output = clean_text(model_output)
167
 
168
+ raw_code_output = extract_code_block(cleaned_output)
169
+ explanation = extract_explanation(cleaned_output, task_type)
170
+ code_output = should_include_code_output(task_type, raw_code_output)
171
+ answer = build_main_answer(task_type, cleaned_output, explanation, code_output)
172
 
173
  warnings = []
174
  if not cleaned_output: