feat: Implement WebSocket support for chat functionality and refactor chat service
- Added WebSocket endpoint for real-time chat interactions. - Refactored ChatPage component to utilize WebSocket for sending and receiving messages. - Updated chat service to handle streaming responses from the LLM agent. - Introduced error handling for WebSocket connections and message processing. - Modified Editor_Test to include AppShellWithMenu for better layout. - Adjusted file path in generar_tree.py for correct directory structure. - Created llm_chat_endpoint_v1.py and llm_chat_srvc.py for handling chat requests and responses. - Established logging for WebSocket interactions and errors.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
# backend/domains/llm/agent_endpoints.py
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import BaseModel
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
|
||||
from backend.domains.llms.llm_chat_srvc import construir_agente_llm, responder, responder_stream
|
||||
from src.Logger.logger_db import LoggerDB, logger
|
||||
from entrypoint.init_db import db_credencial
|
||||
|
||||
LoggerDB(db_credencial, "logger_llm", created_by="sistema")
|
||||
|
||||
router = APIRouter()
|
||||
agente = construir_agente_llm() # inicializa el agente una vez
|
||||
|
||||
# 📥 Schema para entrada de prompt
|
||||
class ChatInput(BaseModel):
|
||||
prompt: str
|
||||
|
||||
# ✅ Endpoint de respuesta simple
|
||||
@router.post("/chat", summary="Enviar prompt y obtener respuesta completa del agente")
|
||||
async def chat_endpoint(data: ChatInput):
|
||||
try:
|
||||
return await responder(data.prompt, agente)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.exception("[ERROR] Fallo durante respuesta del agente:")
|
||||
raise HTTPException(status_code=500, detail="Error interno al procesar la solicitud.")
|
||||
|
||||
# 🔁 Endpoint de streaming
|
||||
@router.post("/chat-stream", summary="Enviar prompt y recibir respuesta del agente en streaming")
|
||||
async def chat_stream_endpoint(data: ChatInput):
|
||||
try:
|
||||
return StreamingResponse(
|
||||
responder_stream(data.prompt, agente),
|
||||
media_type="text/plain"
|
||||
)
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
logger.exception("[ERROR] Fallo durante respuesta en streaming:")
|
||||
raise HTTPException(status_code=500, detail="Error interno en el agente.")
|
||||
Reference in New Issue
Block a user