Spaces:
Sleeping
Sleeping
Update modules/chatbot/chat_process.py
Browse files- modules/chatbot/chat_process.py +28 -28
modules/chatbot/chat_process.py
CHANGED
|
@@ -21,35 +21,35 @@ class ChatProcessor:
|
|
| 21 |
self.conversation_history = []
|
| 22 |
|
| 23 |
####################################################
|
| 24 |
-
def process_chat_input(self, message: str, lang_code: str) -> Generator[str, None, None]:
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
# Generar respuesta usando la API de Claude
|
| 31 |
-
response = self.client.messages.create(
|
| 32 |
-
model="claude-3.5-sonnet-20241022",
|
| 33 |
-
messages=self.conversation_history,
|
| 34 |
-
temperature=0.7,
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
# Procesar la respuesta
|
| 38 |
-
claude_response = response.content[0].text
|
| 39 |
-
self.conversation_history.append({"role": "assistant", "content": claude_response})
|
| 40 |
-
|
| 41 |
-
# Mantener un historial limitado
|
| 42 |
-
if len(self.conversation_history) > 10:
|
| 43 |
-
self.conversation_history = self.conversation_history[-10:]
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
|
|
@@ -103,7 +103,7 @@ def process_chat_input(self, message: str, lang_code: str) -> Generator[str, Non
|
|
| 103 |
Retorna el historial de la conversaci贸n
|
| 104 |
"""
|
| 105 |
return self.conversation_history
|
| 106 |
-
|
| 107 |
def clear_history(self):
|
| 108 |
"""
|
| 109 |
Limpia el historial de la conversaci贸n
|
|
|
|
| 21 |
self.conversation_history = []
|
| 22 |
|
| 23 |
####################################################
|
| 24 |
+
def process_chat_input(self, message: str, lang_code: str) -> Generator[str, None, None]:
|
| 25 |
+
"""Procesa el mensaje y genera una respuesta"""
|
| 26 |
+
try:
|
| 27 |
+
# Agregar mensaje a la historia
|
| 28 |
+
self.conversation_history.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Generar respuesta usando la API de Claude
|
| 31 |
+
response = self.client.messages.create(
|
| 32 |
+
model="claude-3.5-sonnet-20241022",
|
| 33 |
+
messages=self.conversation_history,
|
| 34 |
+
temperature=0.7,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Procesar la respuesta
|
| 38 |
+
claude_response = response.content[0].text
|
| 39 |
+
self.conversation_history.append({"role": "assistant", "content": claude_response})
|
| 40 |
|
| 41 |
+
# Mantener un historial limitado
|
| 42 |
+
if len(self.conversation_history) > 10:
|
| 43 |
+
self.conversation_history = self.conversation_history[-10:]
|
| 44 |
+
|
| 45 |
+
# Dividir la respuesta en palabras para streaming
|
| 46 |
+
words = claude_response.split()
|
| 47 |
+
for word in words:
|
| 48 |
+
yield word + " "
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logger.error(f"Error en process_chat_input: {str(e)}")
|
| 52 |
+
yield f"Error: {str(e)}"
|
| 53 |
|
| 54 |
|
| 55 |
|
|
|
|
| 103 |
Retorna el historial de la conversaci贸n
|
| 104 |
"""
|
| 105 |
return self.conversation_history
|
| 106 |
+
|
| 107 |
def clear_history(self):
|
| 108 |
"""
|
| 109 |
Limpia el historial de la conversaci贸n
|