Update modules/chatbot.py
Browse files- modules/chatbot.py +21 -29
modules/chatbot.py
CHANGED
@@ -7,14 +7,12 @@ class ClaudeAPIChat:
|
|
7 |
api_key = os.environ.get("ANTHROPIC_API_KEY")
|
8 |
if not api_key:
|
9 |
raise ValueError("No se encontr贸 la clave API de Anthropic. Aseg煤rate de configurarla en las variables de entorno.")
|
10 |
-
|
11 |
self.client = anthropic.Anthropic(api_key=api_key)
|
12 |
self.conversation_history = []
|
13 |
|
14 |
def generate_response(self, prompt, lang_code):
|
15 |
self.conversation_history.append(f"Human: {prompt}")
|
16 |
full_message = "\n".join(self.conversation_history)
|
17 |
-
|
18 |
try:
|
19 |
response = self.client.completions.create(
|
20 |
model="claude-2",
|
@@ -23,41 +21,35 @@ class ClaudeAPIChat:
|
|
23 |
temperature=0.7,
|
24 |
stop_sequences=["Human:"]
|
25 |
)
|
26 |
-
|
27 |
claude_response = response.completion.strip()
|
28 |
self.conversation_history.append(f"Assistant: {claude_response}")
|
29 |
-
|
30 |
if len(self.conversation_history) > 10:
|
31 |
self.conversation_history = self.conversation_history[-10:]
|
32 |
-
|
33 |
return claude_response
|
34 |
-
|
35 |
except anthropic.APIError as e:
|
36 |
st.error(f"Error al llamar a la API de Claude: {str(e)}")
|
37 |
return "Lo siento, hubo un error al procesar tu solicitud."
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
return ClaudeAPIChat()
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
st.session_state.api_calls = 0
|
47 |
-
|
48 |
-
if st.session_state.api_calls >= 50: # L铆mite de 50 llamadas por sesi贸n
|
49 |
-
yield "Lo siento, has alcanzado el l铆mite de consultas para esta sesi贸n."
|
50 |
-
return
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
7 |
api_key = os.environ.get("ANTHROPIC_API_KEY")
|
8 |
if not api_key:
|
9 |
raise ValueError("No se encontr贸 la clave API de Anthropic. Aseg煤rate de configurarla en las variables de entorno.")
|
|
|
10 |
self.client = anthropic.Anthropic(api_key=api_key)
|
11 |
self.conversation_history = []
|
12 |
|
13 |
def generate_response(self, prompt, lang_code):
|
14 |
self.conversation_history.append(f"Human: {prompt}")
|
15 |
full_message = "\n".join(self.conversation_history)
|
|
|
16 |
try:
|
17 |
response = self.client.completions.create(
|
18 |
model="claude-2",
|
|
|
21 |
temperature=0.7,
|
22 |
stop_sequences=["Human:"]
|
23 |
)
|
|
|
24 |
claude_response = response.completion.strip()
|
25 |
self.conversation_history.append(f"Assistant: {claude_response}")
|
|
|
26 |
if len(self.conversation_history) > 10:
|
27 |
self.conversation_history = self.conversation_history[-10:]
|
|
|
28 |
return claude_response
|
|
|
29 |
except anthropic.APIError as e:
|
30 |
st.error(f"Error al llamar a la API de Claude: {str(e)}")
|
31 |
return "Lo siento, hubo un error al procesar tu solicitud."
|
32 |
|
33 |
+
def initialize_chatbot():
|
34 |
+
return ClaudeAPIChat()
|
|
|
35 |
|
36 |
+
def get_chatbot_response(chatbot, prompt, lang_code):
|
37 |
+
if 'api_calls' not in st.session_state:
|
38 |
+
st.session_state.api_calls = 0
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
if st.session_state.api_calls >= 50: # L铆mite de 50 llamadas por sesi贸n
|
41 |
+
yield "Lo siento, has alcanzado el l铆mite de consultas para esta sesi贸n."
|
42 |
+
return
|
43 |
+
|
44 |
+
try:
|
45 |
+
st.session_state.api_calls += 1
|
46 |
+
response = chatbot.generate_response(prompt, lang_code)
|
47 |
+
|
48 |
+
# Dividir la respuesta en palabras
|
49 |
+
words = response.split()
|
50 |
+
|
51 |
+
# Devolver las palabras una por una
|
52 |
+
for word in words:
|
53 |
+
yield word + " "
|
54 |
+
except Exception as e:
|
55 |
+
yield f"Error: {str(e)}"
|