aef8791151
- Added Appshell component with responsive navbar and main content area - Integrated ColorSchemeToggle for light/dark mode switching - Created Welcome component with styled title and introductory text - Developed ChatPage for LLM interaction with WebSocket support - Implemented Biblioteca for managing notes with rich text editor - Added LoginPage for user authentication with error handling - Introduced MessageList and MessageBubble components for chat messages - Styled components with CSS modules for consistent design
85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
# src/services/agent_service.py
|
|
|
|
from domains.ApiKeys.openai_apikey_mmr import OpenAICredencialRepo
|
|
from domains.ConexionSql.Postgres_conexion import PostgresConexion
|
|
from domains.ConexionApis.OpenAi_conexion import OpenAICliente
|
|
from domains.Llms.Modelos.Openai_model import ModeloOpenAI
|
|
from domains.Llms.Agente import AgenteAI
|
|
from domains.Llms.Memory.postgres_MemoryConv import MemoryConvPostgres
|
|
from domains.Llms.MCPs.McpClient import MCPClient
|
|
from domains.Llms.MCPs.McpClient_Registry import ClientRegistry
|
|
from entrypoint.init_db import db_credencial
|
|
|
|
from domains.Logger.logger_db import LoggerDB, logger
|
|
LoggerDB(db_credencial, "logger_llm", created_by="sistema")
|
|
|
|
from typing import AsyncGenerator
|
|
|
|
# 🔧 Inicialización única del agente
|
|
def construir_agente_llm() -> AgenteAI:
|
|
logger.info("[INICIO] Inicializando agente LLM...")
|
|
|
|
conexion = PostgresConexion(db_credencial)
|
|
|
|
# Paso 1: Obtener credencial
|
|
repo = OpenAICredencialRepo(conexion)
|
|
credencial = repo.get_by_id("OPAK20250513-61b29978b7604031014")
|
|
if not credencial:
|
|
raise ValueError("No se encontró la credencial OpenAI")
|
|
|
|
logger.debug(f"[OK] Credencial OpenAI cargada: {credencial.titulo}")
|
|
|
|
# Paso 2: Crear cliente
|
|
cliente = OpenAICliente(credencial)
|
|
|
|
# Paso 3: Instanciar modelo
|
|
modelo = ModeloOpenAI(
|
|
cliente=cliente,
|
|
model="gpt-4o",
|
|
temperature=1
|
|
)
|
|
|
|
# Paso 4: Memoria en PostgreSQL
|
|
memoria = MemoryConvPostgres(
|
|
credencial=db_credencial,
|
|
nombre_tabla="memoria_conversacion_pruebas",
|
|
k=10
|
|
)
|
|
|
|
# Paso 5: Herramientas MCP (ej. archivos)
|
|
archivos = MCPClient.from_http(
|
|
name="files",
|
|
url="http://127.0.0.1:4201/fs"
|
|
)
|
|
registry = ClientRegistry()
|
|
registry.add("files", archivos)
|
|
|
|
# Paso 6: Agente
|
|
agente = AgenteAI(
|
|
modelo=modelo,
|
|
nombre="Asistente Inteligente",
|
|
descripcion="",
|
|
system_prompt="",
|
|
rol="asistente",
|
|
objetivos=[],
|
|
max_iterations=0,
|
|
memoria=memoria,
|
|
mcp=registry
|
|
)
|
|
|
|
logger.success("[OK] Agente LLM listo.")
|
|
return agente
|
|
|
|
# ⚡ Función simple
|
|
async def responder(prompt: str, agente: AgenteAI) -> str:
|
|
logger.info(f"[Petición] Prompt recibido: {prompt[:50]}...")
|
|
respuesta = await agente.interactuar_en_bucle(prompt=prompt, stream=False)
|
|
logger.debug(f"[Respuesta] {respuesta[:100]}...")
|
|
return respuesta
|
|
|
|
# 🔁 Función en streaming
|
|
async def responder_stream(prompt: str, agente: AgenteAI) -> AsyncGenerator[str, None]:
|
|
logger.info(f"[Streaming] Prompt recibido: {prompt[:50]}...")
|
|
async for token in agente.interactuar_en_bucle(prompt=prompt, stream=True):
|
|
yield token
|