Spaces:
Running
Running
Update modules/chatbot/chat_process.py
Browse files
modules/chatbot/chat_process.py
CHANGED
|
@@ -6,6 +6,8 @@ from typing import Dict, Generator
|
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
|
|
|
|
|
|
| 9 |
class ChatProcessor:
|
| 10 |
def __init__(self):
|
| 11 |
"""
|
|
@@ -17,7 +19,48 @@ class ChatProcessor:
|
|
| 17 |
|
| 18 |
self.client = anthropic.Anthropic(api_key=api_key)
|
| 19 |
self.conversation_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
| 21 |
def process_chat_input(self, message: str, lang_code: str) -> Generator[str, None, None]:
|
| 22 |
"""
|
| 23 |
Procesa el mensaje y genera una respuesta
|
|
@@ -52,7 +95,9 @@ class ChatProcessor:
|
|
| 52 |
except Exception as e:
|
| 53 |
logger.error(f"Error en process_chat_input: {str(e)}")
|
| 54 |
yield f"Error: {str(e)}"
|
|
|
|
| 55 |
|
|
|
|
| 56 |
def get_conversation_history(self) -> list:
|
| 57 |
"""
|
| 58 |
Retorna el historial de la conversaci贸n
|
|
|
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
+
|
| 10 |
+
####################################################
|
| 11 |
class ChatProcessor:
|
| 12 |
def __init__(self):
|
| 13 |
"""
|
|
|
|
| 19 |
|
| 20 |
self.client = anthropic.Anthropic(api_key=api_key)
|
| 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 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
|
| 62 |
+
################################################################
|
| 63 |
+
'''
|
| 64 |
def process_chat_input(self, message: str, lang_code: str) -> Generator[str, None, None]:
|
| 65 |
"""
|
| 66 |
Procesa el mensaje y genera una respuesta
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
logger.error(f"Error en process_chat_input: {str(e)}")
|
| 97 |
yield f"Error: {str(e)}"
|
| 98 |
+
'''
|
| 99 |
|
| 100 |
+
##########################################
|
| 101 |
def get_conversation_history(self) -> list:
|
| 102 |
"""
|
| 103 |
Retorna el historial de la conversaci贸n
|