Update code
Browse files- prompt_builder.py +28 -17
prompt_builder.py
CHANGED
|
@@ -3,6 +3,13 @@ from typing import List, Optional
|
|
| 3 |
from schemas import CodeTaskType, RetrievedEvidence
|
| 4 |
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def format_evidence(evidence_list: Optional[List[RetrievedEvidence]]) -> str:
|
| 7 |
if not evidence_list:
|
| 8 |
return "No external technical evidence provided."
|
|
@@ -32,12 +39,12 @@ def build_shared_context(
|
|
| 32 |
evidence_list: Optional[List[RetrievedEvidence]] = None,
|
| 33 |
) -> str:
|
| 34 |
parts = [
|
| 35 |
-
f"User Request:\n{
|
| 36 |
-
f"Language:\n{
|
| 37 |
-
f"Framework:\n{
|
| 38 |
-
f"Previous Context:\n{
|
| 39 |
-
f"Error Message:\n{
|
| 40 |
-
f"Code:\n{
|
| 41 |
f"Technical Evidence:\n{format_evidence(evidence_list)}",
|
| 42 |
]
|
| 43 |
return "\n\n".join(parts)
|
|
@@ -63,10 +70,11 @@ def build_generate_prompt(
|
|
| 63 |
"1. Generate clean, correct, production-style code.\n"
|
| 64 |
"2. Match the requested language and framework if provided.\n"
|
| 65 |
"3. If the language or framework is missing, infer a reasonable default from the request.\n"
|
| 66 |
-
"4. Keep the explanation concise.\n"
|
| 67 |
"5. Return code that is directly usable.\n"
|
| 68 |
"6. Do not include unnecessary long prose.\n"
|
| 69 |
-
"7. Do not include multiple alternative implementations unless the user asks.\n
|
|
|
|
| 70 |
f"{shared}\n\n"
|
| 71 |
"Output Format:\n"
|
| 72 |
"Explanation:\n"
|
|
@@ -103,10 +111,11 @@ def build_fix_prompt(
|
|
| 103 |
"2. Preserve the user's intended logic where possible.\n"
|
| 104 |
"3. Use the provided technical evidence if relevant.\n"
|
| 105 |
"4. If the error details are incomplete, make the safest reasonable fix and mention assumptions.\n"
|
| 106 |
-
"5.
|
| 107 |
-
"6.
|
| 108 |
-
"7. Do not
|
| 109 |
-
"8.
|
|
|
|
| 110 |
f"{shared}\n\n"
|
| 111 |
"Output Format:\n"
|
| 112 |
"Root Cause:\n"
|
|
@@ -139,9 +148,9 @@ def build_explain_prompt(
|
|
| 139 |
"Follow these rules:\n"
|
| 140 |
"1. Explain in a simple and structured way.\n"
|
| 141 |
"2. Focus on the most important logic.\n"
|
| 142 |
-
"3. If code is provided, explain what it does and
|
| 143 |
"4. Keep the explanation practical and concise.\n"
|
| 144 |
-
"5. Do not output code unless the user explicitly asks for
|
| 145 |
"6. Do not rewrite the code.\n\n"
|
| 146 |
f"{shared}\n\n"
|
| 147 |
"Output Format:\n"
|
|
@@ -175,7 +184,8 @@ def build_refactor_prompt(
|
|
| 175 |
"4. Do not rename variables, parameters, functions, classes, or methods unless absolutely necessary.\n"
|
| 176 |
"5. Improve readability, maintainability, and structure only.\n"
|
| 177 |
"6. If the code is already simple and acceptable, make only minimal improvements.\n"
|
| 178 |
-
"7.
|
|
|
|
| 179 |
f"{shared}\n\n"
|
| 180 |
"Output Format:\n"
|
| 181 |
"Explanation:\n"
|
|
@@ -207,9 +217,10 @@ def build_review_prompt(
|
|
| 207 |
"1. Focus on correctness, readability, maintainability, edge cases, and possible bugs.\n"
|
| 208 |
"2. Be specific and practical.\n"
|
| 209 |
"3. Prefer review comments and suggestions over rewriting the code.\n"
|
| 210 |
-
|
| 211 |
'5. If no rewrite is necessary, do not include a "Code" section.\n'
|
| 212 |
-
"6. Keep the output review-oriented, not refactor-oriented.\n
|
|
|
|
| 213 |
f"{shared}\n\n"
|
| 214 |
"Output Format:\n"
|
| 215 |
"Review:\n"
|
|
|
|
| 3 |
from schemas import CodeTaskType, RetrievedEvidence
|
| 4 |
|
| 5 |
|
| 6 |
+
def clean_text(value: Optional[str], fallback: str = "None") -> str:
|
| 7 |
+
if value is None:
|
| 8 |
+
return fallback
|
| 9 |
+
text = str(value).strip()
|
| 10 |
+
return text if text else fallback
|
| 11 |
+
|
| 12 |
+
|
| 13 |
def format_evidence(evidence_list: Optional[List[RetrievedEvidence]]) -> str:
|
| 14 |
if not evidence_list:
|
| 15 |
return "No external technical evidence provided."
|
|
|
|
| 39 |
evidence_list: Optional[List[RetrievedEvidence]] = None,
|
| 40 |
) -> str:
|
| 41 |
parts = [
|
| 42 |
+
f"User Request:\n{clean_text(message, '')}",
|
| 43 |
+
f"Language:\n{clean_text(language, 'Not specified')}",
|
| 44 |
+
f"Framework:\n{clean_text(framework, 'Not specified')}",
|
| 45 |
+
f"Previous Context:\n{clean_text(previous_context, 'None')}",
|
| 46 |
+
f"Error Message:\n{clean_text(error_message, 'None')}",
|
| 47 |
+
f"Code:\n{clean_text(code, 'No code provided')}",
|
| 48 |
f"Technical Evidence:\n{format_evidence(evidence_list)}",
|
| 49 |
]
|
| 50 |
return "\n\n".join(parts)
|
|
|
|
| 70 |
"1. Generate clean, correct, production-style code.\n"
|
| 71 |
"2. Match the requested language and framework if provided.\n"
|
| 72 |
"3. If the language or framework is missing, infer a reasonable default from the request.\n"
|
| 73 |
+
"4. Keep the explanation concise and practical.\n"
|
| 74 |
"5. Return code that is directly usable.\n"
|
| 75 |
"6. Do not include unnecessary long prose.\n"
|
| 76 |
+
"7. Do not include multiple alternative implementations unless the user asks.\n"
|
| 77 |
+
"8. Do not add unrelated features or speculative improvements.\n\n"
|
| 78 |
f"{shared}\n\n"
|
| 79 |
"Output Format:\n"
|
| 80 |
"Explanation:\n"
|
|
|
|
| 111 |
"2. Preserve the user's intended logic where possible.\n"
|
| 112 |
"3. Use the provided technical evidence if relevant.\n"
|
| 113 |
"4. If the error details are incomplete, make the safest reasonable fix and mention assumptions.\n"
|
| 114 |
+
"5. Make the minimum necessary changes to solve the issue.\n"
|
| 115 |
+
"6. Do not change variable names, function names, class names, signatures, public interfaces, or structure unless absolutely necessary.\n"
|
| 116 |
+
"7. Do not rewrite unrelated parts of the code.\n"
|
| 117 |
+
"8. Return corrected code that is directly usable.\n"
|
| 118 |
+
"9. Keep the explanation concise and practical.\n\n"
|
| 119 |
f"{shared}\n\n"
|
| 120 |
"Output Format:\n"
|
| 121 |
"Root Cause:\n"
|
|
|
|
| 148 |
"Follow these rules:\n"
|
| 149 |
"1. Explain in a simple and structured way.\n"
|
| 150 |
"2. Focus on the most important logic.\n"
|
| 151 |
+
"3. If code is provided, explain what it does, why it works, and any important edge cases.\n"
|
| 152 |
"4. Keep the explanation practical and concise.\n"
|
| 153 |
+
"5. Do not output changed code unless the user explicitly asks for changes.\n"
|
| 154 |
"6. Do not rewrite the code.\n\n"
|
| 155 |
f"{shared}\n\n"
|
| 156 |
"Output Format:\n"
|
|
|
|
| 184 |
"4. Do not rename variables, parameters, functions, classes, or methods unless absolutely necessary.\n"
|
| 185 |
"5. Improve readability, maintainability, and structure only.\n"
|
| 186 |
"6. If the code is already simple and acceptable, make only minimal improvements.\n"
|
| 187 |
+
"7. Do not rewrite unrelated sections.\n"
|
| 188 |
+
"8. Return improved code that remains easy to compare with the original.\n\n"
|
| 189 |
f"{shared}\n\n"
|
| 190 |
"Output Format:\n"
|
| 191 |
"Explanation:\n"
|
|
|
|
| 217 |
"1. Focus on correctness, readability, maintainability, edge cases, and possible bugs.\n"
|
| 218 |
"2. Be specific and practical.\n"
|
| 219 |
"3. Prefer review comments and suggestions over rewriting the code.\n"
|
| 220 |
+
'4. Only provide improved code if a small correction is truly necessary and helpful.\n'
|
| 221 |
'5. If no rewrite is necessary, do not include a "Code" section.\n'
|
| 222 |
+
"6. Keep the output review-oriented, not refactor-oriented.\n"
|
| 223 |
+
"7. Mention important risks and concrete suggestions.\n\n"
|
| 224 |
f"{shared}\n\n"
|
| 225 |
"Output Format:\n"
|
| 226 |
"Review:\n"
|