Update modules/chatbot/chat_process.py
Browse files
modules/chatbot/chat_process.py
CHANGED
@@ -6,21 +6,16 @@ from typing import Dict, Generator
|
|
6 |
|
7 |
logger = logging.getLogger(__name__)
|
8 |
|
9 |
-
|
10 |
####################################################
|
11 |
class ChatProcessor:
|
12 |
def __init__(self):
|
13 |
-
"""
|
14 |
-
Inicializa el procesador de chat con la API de Claude
|
15 |
-
"""
|
16 |
api_key = os.environ.get("ANTHROPIC_API_KEY")
|
17 |
if not api_key:
|
18 |
raise ValueError("No se encontr贸 la clave API de Anthropic. Aseg煤rate de configurarla en las variables de entorno.")
|
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:
|
@@ -38,7 +33,7 @@ class ChatProcessor:
|
|
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 |
|
@@ -50,53 +45,11 @@ class ChatProcessor:
|
|
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 |
-
def process_chat_input(self, message: str, lang_code: str) -> Generator[str, None, None]:
|
56 |
-
"""
|
57 |
-
Procesa el mensaje y genera una respuesta
|
58 |
-
"""
|
59 |
-
try:
|
60 |
-
# Agregar mensaje a la historia
|
61 |
-
self.conversation_history.append(f"Human: {message}")
|
62 |
-
full_message = "\n".join(self.conversation_history)
|
63 |
-
|
64 |
-
# Generar respuesta usando la API de Claude
|
65 |
-
response = self.client.completions.create(
|
66 |
-
model="claude-2",
|
67 |
-
prompt=f"{full_message}\n\nAssistant:",
|
68 |
-
max_tokens_to_sample=300,
|
69 |
-
temperature=0.7,
|
70 |
-
stop_sequences=["Human:"]
|
71 |
-
)
|
72 |
-
|
73 |
-
# Procesar la respuesta
|
74 |
-
claude_response = response.completion.strip()
|
75 |
-
self.conversation_history.append(f"Assistant: {claude_response}")
|
76 |
-
|
77 |
-
# Mantener un historial limitado
|
78 |
-
if len(self.conversation_history) > 10:
|
79 |
-
self.conversation_history = self.conversation_history[-10:]
|
80 |
-
|
81 |
-
# Dividir la respuesta en palabras para streaming
|
82 |
-
words = claude_response.split()
|
83 |
-
for word in words:
|
84 |
-
yield word + " "
|
85 |
-
|
86 |
-
except Exception as e:
|
87 |
-
logger.error(f"Error en process_chat_input: {str(e)}")
|
88 |
-
yield f"Error: {str(e)}"
|
89 |
-
'''
|
90 |
|
91 |
-
##########################################
|
92 |
def get_conversation_history(self) -> list:
|
93 |
-
"""
|
94 |
-
Retorna el historial de la conversaci贸n
|
95 |
-
"""
|
96 |
return self.conversation_history
|
97 |
-
|
98 |
def clear_history(self):
|
99 |
-
"""
|
100 |
-
Limpia el historial de la conversaci贸n
|
101 |
-
"""
|
102 |
self.conversation_history = []
|
|
|
6 |
|
7 |
logger = logging.getLogger(__name__)
|
8 |
|
|
|
9 |
####################################################
|
10 |
class ChatProcessor:
|
11 |
def __init__(self):
|
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. Aseg煤rate de configurarla en las variables de entorno.")
|
|
|
16 |
self.client = anthropic.Anthropic(api_key=api_key)
|
17 |
self.conversation_history = []
|
18 |
+
|
|
|
19 |
def process_chat_input(self, message: str, lang_code: str) -> Generator[str, None, None]:
|
20 |
"""Procesa el mensaje y genera una respuesta"""
|
21 |
try:
|
|
|
33 |
claude_response = response.content[0].text
|
34 |
self.conversation_history.append({"role": "assistant", "content": claude_response})
|
35 |
|
36 |
+
# Mantener un historial limitado
|
37 |
if len(self.conversation_history) > 10:
|
38 |
self.conversation_history = self.conversation_history[-10:]
|
39 |
|
|
|
45 |
except Exception as e:
|
46 |
logger.error(f"Error en process_chat_input: {str(e)}")
|
47 |
yield f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
|
|
49 |
def get_conversation_history(self) -> list:
|
50 |
+
"""Retorna el historial de la conversaci贸n"""
|
|
|
|
|
51 |
return self.conversation_history
|
52 |
+
|
53 |
def clear_history(self):
|
54 |
+
"""Limpia el historial de la conversaci贸n"""
|
|
|
|
|
55 |
self.conversation_history = []
|