Skip to content

Commit

Permalink
Implementación de chatbot LauraV2
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaister9 committed Oct 3, 2024
1 parent b7f46cd commit 1d214af
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 22 deletions.
16 changes: 1 addition & 15 deletions backend/chatbot_laura/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,10 @@ async def receive(self, text_data):

logger.info(f"Mensaje recibido para Laura Chatbot: {mensaje}")

# Usar la función search de chatbot_logic.py
resultados = search(mensaje, df, index)

# Formatear los resultados
formatted_resultados = [
{
'pregunta': res['content'].get('pregunta', res['content'].get('titulo', 'Sin título')),
'respuesta': res['content'].get('respuesta', res['content'].get('descripcion', 'Sin descripción')),
'url': res.get('url', ''),
'type': res.get('type', ''),
'similarity_score': res.get('similarity_score', 0)
}
for res in resultados
]

# Enviar respuesta
await self.send(text_data=json.dumps({
'message': formatted_resultados
'message': resultados
}))
except Exception as e:
logger.error(f"Error en receive para Laura Chatbot: {e}")
Expand Down
2 changes: 1 addition & 1 deletion backend/chatbot_laura/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
from .consumers import LauraChatConsumer

websocket_urlpatterns = [
re_path(r'ws/chat/$', LauraChatConsumer.as_asgi()),
re_path(r'ws/laura_chat/$', LauraChatConsumer.as_asgi()),
]
13 changes: 7 additions & 6 deletions frontend/src/components/features/LauraChatbot/LauraChatbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ const LauraChatbot = () => {
const [inputMessage, setInputMessage] = useState('');
const webSocketServiceRef = useRef(null);
const hasConnectedRef = useRef(false);

useEffect(() => {
if (!hasConnectedRef.current) {
const service = createWebSocketService('wss://' + window.location.host + '/ws/chat/', (mensaje) => {
console.log("Mensaje recibido desde WebSocket:", mensaje);
const service = createWebSocketService('wss://' + window.location.host + '/ws/laura_chat/', (mensaje) => {
console.log("Mensaje recibido desde WebSocket Laura:", mensaje);

if (Array.isArray(mensaje.message)) {
setMensajes((prevMensajes) => [...prevMensajes, { autor: 'Laura (WebSocket)', mensaje: mensaje.message }]);
} else {
setMensajes((prevMensajes) => [...prevMensajes, { autor: 'Desconocido', mensaje: 'Formato inesperado de WebSocket' }]);
setMensajes((prevMensajes) => [...prevMensajes, { autor: 'Laura', mensaje: mensaje.message || 'Respuesta inesperada' }]);
}
});

Expand Down Expand Up @@ -46,12 +46,13 @@ const LauraChatbot = () => {
}

const respuestaAPI = await enviarMensajeLaura(inputMessage);
console.log("Respuesta API Laura:", respuestaAPI);
if (respuestaAPI.results) {
setMensajes((prevMensajes) => [...prevMensajes, { autor: 'Laura (API)', mensaje: respuestaAPI.results }]);
}
} catch (error) {
console.error("Error al enviar mensaje:", error);
setMensajes((prevMensajes) => [...prevMensajes, { autor: 'Sistema', mensaje: 'Error al enviar el mensaje. Por favor, intenta de nuevo.' }]);
console.error("Error al enviar mensaje a Laura:", error);
setMensajes((prevMensajes) => [...prevMensajes, { autor: 'Sistema', mensaje: 'Error al enviar el mensaje a Laura. Por favor, intenta de nuevo.' }]);
}

setInputMessage('');
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export async function enviarMensajeLaura(mensaje) {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
}
Expand Down

0 comments on commit 1d214af

Please sign in to comment.