118 lines
3.3 KiB
Python
118 lines
3.3 KiB
Python
import asyncio
|
|
from src.ApiKeys.openai_apikey import OpenAICredencial
|
|
from src.ApiKeys.openai_apikey_mmr import OpenAICredencialRepo
|
|
from src.ConexionSql.Postgres_conexion import PostgresConexion
|
|
from entrypoint.init_db import db_credencial
|
|
from src.ConexionApis.OpenAi_conexion import OpenAICliente
|
|
from src.Llms.Modelos.Openai_model import ModeloOpenAI
|
|
from src.Llms.Agente import AgenteAI
|
|
from src.Llms.Memory.postgres_MemoryConv import MemoryConvPostgres
|
|
from fastmcp.client.transports import StreamableHttpTransport
|
|
from fastmcp.client import Client
|
|
from src.Llms.MCPs.McpClient import MCPClient # ya tienes esta clase
|
|
from src.Llms.MCPs.McpClient_Registry import ClientRegistry # o ajusta según tu estructura
|
|
|
|
import asyncio
|
|
|
|
|
|
async def main():
|
|
|
|
# Usar Credencial openai
|
|
|
|
conexion_admin = PostgresConexion(db_credencial)
|
|
repo = OpenAICredencialRepo(conexion_admin)
|
|
credencial_openai = repo.get_by_id("OPAK20250513-61b29978b7604031014")
|
|
cliente = OpenAICliente(credencial_openai)
|
|
|
|
|
|
|
|
# crea el modelo (openai)
|
|
|
|
modelo = ModeloOpenAI(
|
|
cliente=cliente,
|
|
model="gpt-4o",
|
|
temperature=1,
|
|
top_p=1.0
|
|
)
|
|
|
|
# Le otorga memoria
|
|
|
|
memoria = MemoryConvPostgres(
|
|
credencial=db_credencial,
|
|
nombre_tabla="memoria_conversacion_pruebas",
|
|
k=10
|
|
)
|
|
|
|
|
|
# Cargamos las herramientas
|
|
|
|
herramientas = MCPClient.from_http(
|
|
name="tools",
|
|
url="http://127.0.0.1:4300/tools/"
|
|
)
|
|
|
|
math = MCPClient.from_http(
|
|
name="math",
|
|
url="http://127.0.0.1:4200/math/"
|
|
)
|
|
|
|
# Las añadimos al registro de herramientas
|
|
|
|
registry = ClientRegistry()
|
|
|
|
|
|
registry.add("tools", herramientas)
|
|
registry.add("math", math)
|
|
|
|
|
|
|
|
# --- INICIALIZACIÓN DEL AGENTE ---
|
|
agente2 = AgenteAI(
|
|
modelo=modelo,
|
|
nombre="Asistente Inteligente",
|
|
descripcion="Un asistente conversacional versátil, capaz de resolver problemas, acceder a herramientas y proporcionar respuestas útiles.",
|
|
system_prompt=(
|
|
"Eres un asistente inteligente que ayuda al usuario a resolver tareas, responder preguntas y usar herramientas disponibles si es necesario. "
|
|
"Debes razonar paso a paso, y si se detecta que una herramienta MCP es útil, actúa generando el bloque MCP apropiado sin dar más explicaciones. "
|
|
"Siempre estructura tus respuestas con claridad, y termina con <END> cuando creas haber completado la tarea."
|
|
),
|
|
rol="asistente",
|
|
objetivos=[
|
|
"Resolver tareas del usuario",
|
|
"Usar herramientas MCP si es útil",
|
|
"Responder de forma clara y útil"
|
|
],
|
|
|
|
# max_iterations=3,
|
|
# memoria=memoria,
|
|
|
|
mcp=registry # ← ✅ Integración del cliente MCP
|
|
)
|
|
|
|
|
|
# --- FUNCIÓN DE EJECUCIÓN ---
|
|
async def probar_interaccion_stream():
|
|
# # 🔌 Conectar a los servidores MCP registrados
|
|
# await mcp_client.connect_all()
|
|
|
|
print("Respuesta en streaming:\n")
|
|
respuesta_gen = await agente2.interactuar_en_bucle(
|
|
"¿Cuál es mi nombre de usuario en este sistema?",
|
|
stream=True
|
|
)
|
|
|
|
async for token in respuesta_gen:
|
|
print(token, end="", flush=True)
|
|
|
|
|
|
await probar_interaccion_stream()
|
|
|
|
|
|
# Ejecutar
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|
|
|
|
|
|
|
|
|