Spaces:
Running
Running
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# agent.py — AGENTE SEMÁNTICO CON SÍNTESIS INTELIGENTE
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
import logging
|
|
@@ -100,30 +100,32 @@ class ImprovedSemanticAgent:
|
|
| 100 |
query_embedding = self.embedding_model.encode([user_prompt], convert_to_numpy=True, normalize_embeddings=True)[0]
|
| 101 |
query_embedding = query_embedding.astype('float32').reshape(1, -1)
|
| 102 |
distances, indices = self.index.search(query_embedding, 5)
|
| 103 |
-
|
| 104 |
candidates = []
|
| 105 |
for idx in indices[0]:
|
| 106 |
if idx < len(self.indexed_examples):
|
| 107 |
candidates.append(self.indexed_examples[idx]['caption'])
|
|
|
|
| 108 |
if not candidates:
|
| 109 |
return self._structural_fallback(user_prompt, category), "🔧 Fallback estructural"
|
| 110 |
-
|
| 111 |
user_words = set(user_prompt.lower().split())
|
| 112 |
all_parts = []
|
|
|
|
| 113 |
for caption in candidates:
|
| 114 |
parts = [p.strip() for p in caption.split(',') if 8 <= len(p) <= 120]
|
| 115 |
for part in parts:
|
| 116 |
part_lower = part.lower()
|
| 117 |
if len(set(part_lower.split()) - user_words) >= 2:
|
| 118 |
all_parts.append(part)
|
| 119 |
-
|
| 120 |
seen = set()
|
| 121 |
unique_parts = []
|
| 122 |
for p in all_parts:
|
| 123 |
if p not in seen:
|
| 124 |
unique_parts.append(p)
|
| 125 |
seen.add(p)
|
| 126 |
-
|
| 127 |
selected = unique_parts[:6]
|
| 128 |
if selected:
|
| 129 |
additions = ", ".join(selected)
|
|
@@ -131,7 +133,7 @@ class ImprovedSemanticAgent:
|
|
| 131 |
return enhanced, f"✨ Prompt sintetizado con {len(candidates)} ejemplos"
|
| 132 |
else:
|
| 133 |
return self._structural_fallback(user_prompt, category), "🔧 Fallback estructural (sin frases útiles)"
|
| 134 |
-
|
| 135 |
except Exception as e:
|
| 136 |
logger.error(f"❌ Error en _do_enhancement: {e}")
|
| 137 |
return user_prompt, f"❌ Error: {str(e)}"
|
|
@@ -167,6 +169,7 @@ class ImprovedSemanticAgent:
|
|
| 167 |
self._used_indices.add(idx)
|
| 168 |
return self.indexed_examples[idx]['caption']
|
| 169 |
return "🔍 No encontrado"
|
|
|
|
| 170 |
except Exception as e:
|
| 171 |
return f"❌ Error: {str(e)}"
|
| 172 |
|
|
|
|
| 1 |
+
# agent.py — AGENTE SEMÁNTICO CON SÍNTESIS INTELIGENTE v1.0
|
| 2 |
import os
|
| 3 |
import time
|
| 4 |
import logging
|
|
|
|
| 100 |
query_embedding = self.embedding_model.encode([user_prompt], convert_to_numpy=True, normalize_embeddings=True)[0]
|
| 101 |
query_embedding = query_embedding.astype('float32').reshape(1, -1)
|
| 102 |
distances, indices = self.index.search(query_embedding, 5)
|
| 103 |
+
|
| 104 |
candidates = []
|
| 105 |
for idx in indices[0]:
|
| 106 |
if idx < len(self.indexed_examples):
|
| 107 |
candidates.append(self.indexed_examples[idx]['caption'])
|
| 108 |
+
|
| 109 |
if not candidates:
|
| 110 |
return self._structural_fallback(user_prompt, category), "🔧 Fallback estructural"
|
| 111 |
+
|
| 112 |
user_words = set(user_prompt.lower().split())
|
| 113 |
all_parts = []
|
| 114 |
+
|
| 115 |
for caption in candidates:
|
| 116 |
parts = [p.strip() for p in caption.split(',') if 8 <= len(p) <= 120]
|
| 117 |
for part in parts:
|
| 118 |
part_lower = part.lower()
|
| 119 |
if len(set(part_lower.split()) - user_words) >= 2:
|
| 120 |
all_parts.append(part)
|
| 121 |
+
|
| 122 |
seen = set()
|
| 123 |
unique_parts = []
|
| 124 |
for p in all_parts:
|
| 125 |
if p not in seen:
|
| 126 |
unique_parts.append(p)
|
| 127 |
seen.add(p)
|
| 128 |
+
|
| 129 |
selected = unique_parts[:6]
|
| 130 |
if selected:
|
| 131 |
additions = ", ".join(selected)
|
|
|
|
| 133 |
return enhanced, f"✨ Prompt sintetizado con {len(candidates)} ejemplos"
|
| 134 |
else:
|
| 135 |
return self._structural_fallback(user_prompt, category), "🔧 Fallback estructural (sin frases útiles)"
|
| 136 |
+
|
| 137 |
except Exception as e:
|
| 138 |
logger.error(f"❌ Error en _do_enhancement: {e}")
|
| 139 |
return user_prompt, f"❌ Error: {str(e)}"
|
|
|
|
| 169 |
self._used_indices.add(idx)
|
| 170 |
return self.indexed_examples[idx]['caption']
|
| 171 |
return "🔍 No encontrado"
|
| 172 |
+
|
| 173 |
except Exception as e:
|
| 174 |
return f"❌ Error: {str(e)}"
|
| 175 |
|