Spaces:
Sleeping
Sleeping
Update modules/chatbot/chat_process.py
Browse files
modules/chatbot/chat_process.py
CHANGED
|
@@ -12,10 +12,11 @@ class ChatProcessor:
|
|
| 12 |
"""Inicializa el procesador de chat con la API de Claude"""
|
| 13 |
api_key = os.environ.get("ANTHROPIC_API_KEY")
|
| 14 |
if not api_key:
|
| 15 |
-
raise ValueError("No se encontró la clave API de Anthropic.
|
| 16 |
self.client = anthropic.Anthropic(api_key=api_key)
|
| 17 |
self.conversation_history = []
|
| 18 |
-
|
|
|
|
| 19 |
def set_semantic_context(self, text, metrics, graph_data):
|
| 20 |
"""Configura el contexto semántico para conversaciones especializadas"""
|
| 21 |
self.semantic_context = {
|
|
@@ -24,6 +25,10 @@ class ChatProcessor:
|
|
| 24 |
'concept_centrality': metrics.get('concept_centrality', {}),
|
| 25 |
'graph_description': "Available" if graph_data else "Not available"
|
| 26 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def process_chat_input(self, message: str, lang_code: str) -> Generator[str, None, None]:
|
| 29 |
"""Procesa el mensaje con contexto semántico si está disponible"""
|
|
@@ -31,15 +36,15 @@ class ChatProcessor:
|
|
| 31 |
# Preparar mensaje con contexto si existe
|
| 32 |
if self.semantic_context:
|
| 33 |
system_prompt = f"""
|
| 34 |
-
Eres un asistente especializado en análisis semántico.
|
| 35 |
- Conceptos clave: {', '.join([c[0] for c in self.semantic_context['key_concepts'][:5]])}...
|
| 36 |
-
- Centralidad: {len(self.semantic_context['concept_centrality'])} conceptos
|
| 37 |
-
- Grafo
|
| 38 |
|
| 39 |
-
Responde preguntas
|
| 40 |
"""
|
| 41 |
else:
|
| 42 |
-
system_prompt = "Eres un asistente útil. Responde preguntas generales
|
| 43 |
|
| 44 |
# Agregar mensaje a la historia
|
| 45 |
self.conversation_history.append({"role": "user", "content": message})
|
|
|
|
| 12 |
"""Inicializa el procesador de chat con la API de Claude"""
|
| 13 |
api_key = os.environ.get("ANTHROPIC_API_KEY")
|
| 14 |
if not api_key:
|
| 15 |
+
raise ValueError("No se encontró la clave API de Anthropic.")
|
| 16 |
self.client = anthropic.Anthropic(api_key=api_key)
|
| 17 |
self.conversation_history = []
|
| 18 |
+
self.semantic_context = None # <-- Añade esta línea para inicializar el atributo
|
| 19 |
+
|
| 20 |
def set_semantic_context(self, text, metrics, graph_data):
|
| 21 |
"""Configura el contexto semántico para conversaciones especializadas"""
|
| 22 |
self.semantic_context = {
|
|
|
|
| 25 |
'concept_centrality': metrics.get('concept_centrality', {}),
|
| 26 |
'graph_description': "Available" if graph_data else "Not available"
|
| 27 |
}
|
| 28 |
+
|
| 29 |
+
def clear_semantic_context(self):
|
| 30 |
+
"""Limpia el contexto semántico"""
|
| 31 |
+
self.semantic_context = None
|
| 32 |
|
| 33 |
def process_chat_input(self, message: str, lang_code: str) -> Generator[str, None, None]:
|
| 34 |
"""Procesa el mensaje con contexto semántico si está disponible"""
|
|
|
|
| 36 |
# Preparar mensaje con contexto si existe
|
| 37 |
if self.semantic_context:
|
| 38 |
system_prompt = f"""
|
| 39 |
+
Eres un asistente especializado en análisis semántico. Datos del análisis actual:
|
| 40 |
- Conceptos clave: {', '.join([c[0] for c in self.semantic_context['key_concepts'][:5]])}...
|
| 41 |
+
- Centralidad: {len(self.semantic_context['concept_centrality'])} conceptos
|
| 42 |
+
- Grafo: {self.semantic_context['graph_description']}
|
| 43 |
|
| 44 |
+
Responde preguntas sobre este análisis específico.
|
| 45 |
"""
|
| 46 |
else:
|
| 47 |
+
system_prompt = "Eres un asistente útil. Responde preguntas generales."
|
| 48 |
|
| 49 |
# Agregar mensaje a la historia
|
| 50 |
self.conversation_history.append({"role": "user", "content": message})
|