vfven commited on
Commit
5c2c5e3
·
verified ·
1 Parent(s): a7a1128

Update agents/specialized.py

Browse files
Files changed (1) hide show
  1. agents/specialized.py +13 -17
agents/specialized.py CHANGED
@@ -4,13 +4,17 @@ from utils.prompts import get_today_prefix, get_chat_style
4
  from agents.registry import AGENT_REGISTRY
5
  from core.file_builder import build_docx, build_excel
6
  import re
 
 
 
7
 
8
  AGENT_ROLES = {
9
  "manager": """
10
- Eres el gerente principal. Cuando no haya delegación, responde de forma natural, amigable y útil al usuario.
11
  No necesitas generar JSON aquí, solo contesta directamente.
 
12
  """,
13
-
14
  "image_agent": """
15
  Eres el agente de imágenes.
16
  Cuando se te pida generar o buscar imágenes, responde SOLO con:
@@ -61,13 +65,10 @@ async def generate_image_from_prompt(prompt: str, model: str = "black-forest-lab
61
  image = await asyncio.to_thread(
62
  client.text_to_image,
63
  prompt,
64
- num_inference_steps=20, # ajusta para más calidad (más lento)
65
  guidance_scale=7.5
66
  )
67
 
68
- # Guardar temporalmente
69
- from pathlib import Path
70
- from datetime import datetime
71
  DOCS_DIR = Path("data/docs")
72
  DOCS_DIR.mkdir(exist_ok=True)
73
  ts = datetime.now().strftime("%Y%m%d_%H%M%S")
@@ -91,7 +92,6 @@ async def run_specialized_agent(agent_key: str, task: str, context: dict = None)
91
 
92
  full_prompt = today + role + get_chat_style() + f"\n\nTarea asignada: {task}"
93
 
94
- # Agregar contexto si viene de otros agentes
95
  if context:
96
  full_prompt += "\n\nContexto de otros agentes:\n" + "\n".join([f"[{k.upper()}] {v[:300]}..." for k, v in context.items()])
97
 
@@ -105,7 +105,8 @@ async def run_specialized_agent(agent_key: str, task: str, context: dict = None)
105
  "response": response,
106
  "success": True,
107
  "file_path": None,
108
- "file_type": None
 
109
  }
110
 
111
  # Generar archivo según agente
@@ -120,40 +121,35 @@ async def run_specialized_agent(agent_key: str, task: str, context: dict = None)
120
  result["file_error"] = str(e)
121
 
122
  elif agent_key == "backend_dev":
123
- # Detectar EXCEL_TEMPLATE
124
  match = re.search(r'EXCEL_TEMPLATE:\s*(\{.*\})', response, re.DOTALL | re.IGNORECASE)
125
  if match:
126
  try:
127
- import json
128
  excel_data = json.loads(match.group(1))
129
  file_path = build_excel(task, excel_data)
130
  if file_path:
131
  result["file_path"] = file_path.name
132
  result["file_type"] = "xlsx"
133
- # Limpiar respuesta para no mostrar el template crudo
134
- result["response"] = response.split("EXCEL_TEMPLATE:")[0].strip()
135
  except Exception as e:
136
  result["file_error"] = str(e)
137
 
138
  elif agent_key == "image_agent":
139
- # Generar queries (como antes)
140
  match = re.search(r'\{.*"image_queries".*\}', response, re.DOTALL)
141
  if match:
142
  try:
143
  queries = json.loads(match.group(0))["image_queries"]
144
  result["queries"] = queries
145
 
146
- # Si la tarea pide generar imagen real
147
  if any(word in task.lower() for word in ["genera la imagen", "crea la imagen", "muestra la imagen", "imagen real"]):
148
- # Usa la primera query (o combina todas)
149
  best_prompt = ", ".join(queries) + ", highly detailed, 8k, masterpiece"
150
  img_result = await generate_image_from_prompt(best_prompt)
151
  if img_result["success"]:
152
  result["image_url"] = img_result["image_url"]
153
  else:
154
  result["image_error"] = img_result["error"]
155
- except:
156
- pass
157
 
158
  return result
159
 
 
4
  from agents.registry import AGENT_REGISTRY
5
  from core.file_builder import build_docx, build_excel
6
  import re
7
+ import json
8
+ from pathlib import Path
9
+ from datetime import datetime
10
 
11
  AGENT_ROLES = {
12
  "manager": """
13
+ Eres el gerente principal. Cuando no haya delegación o la tarea sea simple, responde de forma natural, amigable y útil al usuario.
14
  No necesitas generar JSON aquí, solo contesta directamente.
15
+ Mantén un tono profesional pero cercano.
16
  """,
17
+
18
  "image_agent": """
19
  Eres el agente de imágenes.
20
  Cuando se te pida generar o buscar imágenes, responde SOLO con:
 
65
  image = await asyncio.to_thread(
66
  client.text_to_image,
67
  prompt,
68
+ num_inference_steps=20,
69
  guidance_scale=7.5
70
  )
71
 
 
 
 
72
  DOCS_DIR = Path("data/docs")
73
  DOCS_DIR.mkdir(exist_ok=True)
74
  ts = datetime.now().strftime("%Y%m%d_%H%M%S")
 
92
 
93
  full_prompt = today + role + get_chat_style() + f"\n\nTarea asignada: {task}"
94
 
 
95
  if context:
96
  full_prompt += "\n\nContexto de otros agentes:\n" + "\n".join([f"[{k.upper()}] {v[:300]}..." for k, v in context.items()])
97
 
 
105
  "response": response,
106
  "success": True,
107
  "file_path": None,
108
+ "file_type": None,
109
+ "image_url": None
110
  }
111
 
112
  # Generar archivo según agente
 
121
  result["file_error"] = str(e)
122
 
123
  elif agent_key == "backend_dev":
 
124
  match = re.search(r'EXCEL_TEMPLATE:\s*(\{.*\})', response, re.DOTALL | re.IGNORECASE)
125
  if match:
126
  try:
 
127
  excel_data = json.loads(match.group(1))
128
  file_path = build_excel(task, excel_data)
129
  if file_path:
130
  result["file_path"] = file_path.name
131
  result["file_type"] = "xlsx"
132
+ result["response"] = response.split("EXCEL_TEMPLATE:")[0].strip() # limpiar
 
133
  except Exception as e:
134
  result["file_error"] = str(e)
135
 
136
  elif agent_key == "image_agent":
 
137
  match = re.search(r'\{.*"image_queries".*\}', response, re.DOTALL)
138
  if match:
139
  try:
140
  queries = json.loads(match.group(0))["image_queries"]
141
  result["queries"] = queries
142
 
143
+ # Generar imagen real si el usuario lo pide explícitamente
144
  if any(word in task.lower() for word in ["genera la imagen", "crea la imagen", "muestra la imagen", "imagen real"]):
 
145
  best_prompt = ", ".join(queries) + ", highly detailed, 8k, masterpiece"
146
  img_result = await generate_image_from_prompt(best_prompt)
147
  if img_result["success"]:
148
  result["image_url"] = img_result["image_url"]
149
  else:
150
  result["image_error"] = img_result["error"]
151
+ except Exception as e:
152
+ result["image_error"] = str(e)
153
 
154
  return result
155