Spaces:
Runtime error
Runtime error
Create promt_generator.py
Browse files- promt_generator.py +73 -0
promt_generator.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
class PromptGenerator:
|
| 4 |
+
@staticmethod
|
| 5 |
+
def detect_intent(prompt: str) -> dict:
|
| 6 |
+
"""Detecta la intenci贸n del usuario basado en el prompt"""
|
| 7 |
+
prompt_lower = prompt.lower().strip()
|
| 8 |
+
|
| 9 |
+
# Detecci贸n de c贸digo
|
| 10 |
+
code_keywords = [
|
| 11 |
+
'c贸digo', 'code', 'programa', 'function', 'def ', 'import ',
|
| 12 |
+
'python', 'javascript', 'java', 'c++', 'html', 'css', 'sql',
|
| 13 |
+
'algoritmo', 'loop', 'for ', 'while ', 'if ', 'else',
|
| 14 |
+
'variable', 'clase', 'class ', 'funci贸n', 'method'
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
is_code = any(keyword in prompt_lower for keyword in code_keywords)
|
| 18 |
+
|
| 19 |
+
# Detecci贸n de tipo de consulta
|
| 20 |
+
if any(word in prompt_lower for word in ['explica', 'explicar', 'qu茅 es', 'qu茅 son', 'defin']):
|
| 21 |
+
intent_type = "explication"
|
| 22 |
+
elif any(word in prompt_lower for word in ['ejemplo', 'ejemplifica', 'muestra']):
|
| 23 |
+
intent_type = "example"
|
| 24 |
+
elif any(word in prompt_lower for word in ['corrige', 'error', 'bug', 'problema']):
|
| 25 |
+
intent_type = "correction"
|
| 26 |
+
else:
|
| 27 |
+
intent_type = "general"
|
| 28 |
+
|
| 29 |
+
return {
|
| 30 |
+
"is_code": is_code,
|
| 31 |
+
"type": intent_type,
|
| 32 |
+
"language": PromptGenerator._detect_language(prompt_lower)
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
@staticmethod
|
| 36 |
+
def _detect_language(prompt: str) -> str:
|
| 37 |
+
"""Detecta el lenguaje de programaci贸n mencionado"""
|
| 38 |
+
languages = {
|
| 39 |
+
'python': ['python', 'py'],
|
| 40 |
+
'javascript': ['javascript', 'js', 'node'],
|
| 41 |
+
'java': ['java'],
|
| 42 |
+
'html': ['html'],
|
| 43 |
+
'css': ['css'],
|
| 44 |
+
'sql': ['sql', 'mysql', 'postgresql'],
|
| 45 |
+
'c++': ['c++', 'cpp'],
|
| 46 |
+
'c#': ['c#', 'csharp']
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
for lang, keywords in languages.items():
|
| 50 |
+
if any(keyword in prompt for keyword in keywords):
|
| 51 |
+
return lang
|
| 52 |
+
return "unknown"
|
| 53 |
+
|
| 54 |
+
@staticmethod
|
| 55 |
+
def enhance_prompt(original_prompt: str, intent: dict) -> str:
|
| 56 |
+
"""Mejora el prompt basado en la intenci贸n detectada"""
|
| 57 |
+
if intent["is_code"]:
|
| 58 |
+
if intent["type"] == "explication":
|
| 59 |
+
return f"Explica detalladamente este c贸digo: {original_prompt}"
|
| 60 |
+
elif intent["type"] == "example":
|
| 61 |
+
lang = intent["language"] if intent["language"] != "unknown" else "Python"
|
| 62 |
+
return f"Da un ejemplo en {lang}: {original_prompt}"
|
| 63 |
+
elif intent["type"] == "correction":
|
| 64 |
+
return f"Corrige este c贸digo: {original_prompt}"
|
| 65 |
+
else:
|
| 66 |
+
return f"Responde sobre programaci贸n: {original_prompt}"
|
| 67 |
+
else:
|
| 68 |
+
if intent["type"] == "explication":
|
| 69 |
+
return f"Explica claramente: {original_prompt}"
|
| 70 |
+
elif intent["type"] == "example":
|
| 71 |
+
return f"Proporciona ejemplos: {original_prompt}"
|
| 72 |
+
else:
|
| 73 |
+
return original_prompt
|