Download prompt_builder.py from hashan-7/Chat7-CodeX-Backend: direct link, hf CLI and curl.
- Browser
- Download file 10.3 kB
-
https://huggingface.co/spaces/hashan-7/Chat7-CodeX-Backend/resolve/main/prompt_builder.py
- Command line
-
hf download hf://spaces/hashan-7/Chat7-CodeX-Backend/prompt_builder.py
-
curl -L -o prompt_builder.py https://huggingface.co/spaces/hashan-7/Chat7-CodeX-Backend/resolve/main/prompt_builder.py
10.3 kB
| from typing import List, Optional | |
| from schemas import CodeTaskType, RetrievedEvidence | |
| def clean_text(value: Optional[str], fallback: str = "None") -> str: | |
| if value is None: | |
| return fallback | |
| text = str(value).strip() | |
| return text if text else fallback | |
| def format_evidence(evidence_list: Optional[List[RetrievedEvidence]]) -> str: | |
| if not evidence_list: | |
| return "No external technical evidence provided." | |
| lines = [] | |
| for index, item in enumerate(evidence_list, start=1): | |
| lines.append(f"Source {index}") | |
| lines.append(f"Type: {item.source_type}") | |
| lines.append(f"Title: {item.title}") | |
| lines.append(f"Snippet: {item.snippet}") | |
| if item.url: | |
| lines.append(f"URL: {item.url}") | |
| if item.score is not None: | |
| lines.append(f"Score: {item.score}") | |
| lines.append("") | |
| return "\n".join(lines).strip() | |
| def build_shared_context( | |
| message: str, | |
| language: Optional[str] = None, | |
| framework: Optional[str] = None, | |
| code: Optional[str] = None, | |
| error_message: Optional[str] = None, | |
| previous_context: Optional[str] = None, | |
| evidence_list: Optional[List[RetrievedEvidence]] = None, | |
| ) -> str: | |
| parts = [ | |
| f"User Request:\n{clean_text(message, '')}", | |
| f"Language:\n{clean_text(language, 'Not specified')}", | |
| f"Framework:\n{clean_text(framework, 'Not specified')}", | |
| f"Previous Context:\n{clean_text(previous_context, 'None')}", | |
| f"Error Message:\n{clean_text(error_message, 'None')}", | |
| f"Code:\n{clean_text(code, 'No code provided')}", | |
| f"Technical Evidence:\n{format_evidence(evidence_list)}", | |
| ] | |
| return "\n\n".join(parts) | |
| def build_generate_prompt( | |
| message: str, | |
| language: Optional[str] = None, | |
| framework: Optional[str] = None, | |
| previous_context: Optional[str] = None, | |
| ) -> str: | |
| shared = build_shared_context( | |
| message=message, | |
| language=language, | |
| framework=framework, | |
| previous_context=previous_context, | |
| ) | |
| return ( | |
| "You are a senior software engineer.\n" | |
| "Your task is to generate code based on the user's request.\n" | |
| "Follow these rules:\n" | |
| "1. Generate clean, correct, production-style code.\n" | |
| "2. Match the requested language and framework if provided.\n" | |
| "3. If the language or framework is missing, infer a reasonable default from the request.\n" | |
| "4. Keep the explanation concise and practical.\n" | |
| "5. Return code that is directly usable.\n" | |
| "6. Do not include unnecessary long prose.\n" | |
| "7. Do not include multiple alternative implementations unless the user asks.\n" | |
| "8. Do not add unrelated features or speculative improvements.\n\n" | |
| f"{shared}\n\n" | |
| "Output Format:\n" | |
| "Explanation:\n" | |
| "<one short explanation>\n\n" | |
| "Code:\n" | |
| "<generated code>" | |
| ) | |
| def build_fix_prompt( | |
| message: str, | |
| code: Optional[str] = None, | |
| error_message: Optional[str] = None, | |
| language: Optional[str] = None, | |
| framework: Optional[str] = None, | |
| previous_context: Optional[str] = None, | |
| evidence_list: Optional[List[RetrievedEvidence]] = None, | |
| ) -> str: | |
| shared = build_shared_context( | |
| message=message, | |
| language=language, | |
| framework=framework, | |
| code=code, | |
| error_message=error_message, | |
| previous_context=previous_context, | |
| evidence_list=evidence_list, | |
| ) | |
| return ( | |
| "You are a senior software engineer and debugging expert.\n" | |
| "Your task is to fix the user's code.\n" | |
| "Follow these rules:\n" | |
| "1. Identify the most likely root cause.\n" | |
| "2. Preserve the user's intended logic where possible.\n" | |
| "3. Use the provided technical evidence if relevant.\n" | |
| "4. If the error details are incomplete, make the safest reasonable fix and mention assumptions.\n" | |
| "5. Make the minimum necessary changes to solve the issue.\n" | |
| "6. Do not change variable names, function names, class names, signatures, public interfaces, or structure unless absolutely necessary.\n" | |
| "7. Do not rewrite unrelated parts of the code.\n" | |
| "8. Return corrected code that is directly usable.\n" | |
| "9. Keep the explanation concise and practical.\n\n" | |
| f"{shared}\n\n" | |
| "Output Format:\n" | |
| "Root Cause:\n" | |
| "<one short root cause>\n\n" | |
| "Explanation:\n" | |
| "<clear but concise explanation>\n\n" | |
| "Code:\n" | |
| "<fixed code>" | |
| ) | |
| def build_explain_prompt( | |
| message: str, | |
| code: Optional[str] = None, | |
| language: Optional[str] = None, | |
| framework: Optional[str] = None, | |
| previous_context: Optional[str] = None, | |
| ) -> str: | |
| shared = build_shared_context( | |
| message=message, | |
| language=language, | |
| framework=framework, | |
| code=code, | |
| previous_context=previous_context, | |
| ) | |
| return ( | |
| "You are a senior software engineer.\n" | |
| "Your task is to explain the user's code or technical question clearly.\n" | |
| "Follow these rules:\n" | |
| "1. Explain in a simple and structured way.\n" | |
| "2. Focus on the most important logic.\n" | |
| "3. If code is provided, explain what it does, why it works, and any important edge cases.\n" | |
| "4. Keep the explanation practical and concise.\n" | |
| "5. Do not output changed code unless the user explicitly asks for changes.\n" | |
| "6. Do not rewrite the code.\n\n" | |
| f"{shared}\n\n" | |
| "Output Format:\n" | |
| "Explanation:\n" | |
| "<clear structured explanation>" | |
| ) | |
| def build_refactor_prompt( | |
| message: str, | |
| code: Optional[str] = None, | |
| language: Optional[str] = None, | |
| framework: Optional[str] = None, | |
| previous_context: Optional[str] = None, | |
| ) -> str: | |
| shared = build_shared_context( | |
| message=message, | |
| language=language, | |
| framework=framework, | |
| code=code, | |
| previous_context=previous_context, | |
| ) | |
| return ( | |
| "You are a senior software engineer.\n" | |
| "Your task is to refactor the user's code.\n" | |
| "Follow these rules:\n" | |
| "1. Preserve the original behavior unless the user explicitly asked for functional changes.\n" | |
| "2. Preserve all existing function names, class names, method names, parameters, argument order, return behavior, and public interfaces.\n" | |
| "3. You must keep the original callable names exactly the same.\n" | |
| "4. Do not rename variables, parameters, functions, classes, or methods unless absolutely necessary.\n" | |
| "5. Improve readability, maintainability, and structure only.\n" | |
| "6. If the code is already simple and acceptable, make only minimal improvements.\n" | |
| "7. Do not rewrite unrelated sections.\n" | |
| "8. Return improved code that remains easy to compare with the original.\n\n" | |
| f"{shared}\n\n" | |
| "Output Format:\n" | |
| "Explanation:\n" | |
| "<short explanation>\n\n" | |
| "Code:\n" | |
| "<refactored code>" | |
| ) | |
| def build_review_prompt( | |
| message: str, | |
| code: Optional[str] = None, | |
| language: Optional[str] = None, | |
| framework: Optional[str] = None, | |
| previous_context: Optional[str] = None, | |
| ) -> str: | |
| shared = build_shared_context( | |
| message=message, | |
| language=language, | |
| framework=framework, | |
| code=code, | |
| previous_context=previous_context, | |
| ) | |
| return ( | |
| "You are a senior software engineer performing a code review.\n" | |
| "Your task is to review the user's code and identify issues or improvements.\n" | |
| "Follow these rules:\n" | |
| "1. Focus on correctness, readability, maintainability, edge cases, and possible bugs.\n" | |
| "2. Be specific and practical.\n" | |
| "3. Prefer review comments and suggestions over rewriting the code.\n" | |
| '4. Only provide improved code if a small correction is truly necessary and helpful.\n' | |
| '5. If no rewrite is necessary, do not include a "Code" section.\n' | |
| "6. Keep the output review-oriented, not refactor-oriented.\n" | |
| "7. Mention important risks and concrete suggestions.\n\n" | |
| f"{shared}\n\n" | |
| "Output Format:\n" | |
| "Review:\n" | |
| "<review points>\n\n" | |
| "Suggestions:\n" | |
| "<practical suggestions>\n\n" | |
| "Code:\n" | |
| "<optional improved code only if needed>" | |
| ) | |
| def build_prompt( | |
| task_type: CodeTaskType, | |
| message: str, | |
| code: Optional[str] = None, | |
| error_message: Optional[str] = None, | |
| language: Optional[str] = None, | |
| framework: Optional[str] = None, | |
| previous_context: Optional[str] = None, | |
| evidence_list: Optional[List[RetrievedEvidence]] = None, | |
| ) -> str: | |
| if task_type == CodeTaskType.GENERATE: | |
| return build_generate_prompt( | |
| message=message, | |
| language=language, | |
| framework=framework, | |
| previous_context=previous_context, | |
| ) | |
| if task_type == CodeTaskType.FIX: | |
| return build_fix_prompt( | |
| message=message, | |
| code=code, | |
| error_message=error_message, | |
| language=language, | |
| framework=framework, | |
| previous_context=previous_context, | |
| evidence_list=evidence_list, | |
| ) | |
| if task_type == CodeTaskType.EXPLAIN: | |
| return build_explain_prompt( | |
| message=message, | |
| code=code, | |
| language=language, | |
| framework=framework, | |
| previous_context=previous_context, | |
| ) | |
| if task_type == CodeTaskType.REFACTOR: | |
| return build_refactor_prompt( | |
| message=message, | |
| code=code, | |
| language=language, | |
| framework=framework, | |
| previous_context=previous_context, | |
| ) | |
| if task_type == CodeTaskType.REVIEW: | |
| return build_review_prompt( | |
| message=message, | |
| code=code, | |
| language=language, | |
| framework=framework, | |
| previous_context=previous_context, | |
| ) | |
| return build_explain_prompt( | |
| message=message, | |
| code=code, | |
| language=language, | |
| framework=framework, | |
| previous_context=previous_context, | |
| ) |