{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "255345d5", "metadata": {}, "outputs": [], "source": [ "from src.TextManager.biblioteca import Biblioteca\n", "from src.TextManager.biblioteca_mmr import BibliotecaRepo\n", "from src.Llms.Embedders.Openai_embedder import OpenAIEmbedder\n", "from src.ApiKeys.openai_apikey_mmr import OpenAICredencialRepo\n", "from src.ConexionSql.Postgres_conexion import PostgresConexion\n", "from src.TextManager.nota import Nota\n", "from src.TextManager.notas_biblioteca_mmr import generar_tabla_nota_para_biblioteca, NotaRepo\n", "from sqlalchemy import MetaData\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "b414a66c", "metadata": {}, "outputs": [], "source": [ "def agregar_nota_a_biblioteca(\n", " conexion: PostgresConexion,\n", " biblioteca_id: str,\n", " titulo: str,\n", " texto: str = \"\",\n", " tags: list[str] = None,\n", " conexiones: list[str] = None,\n", " resumen: str = \"\"\n", "):\n", " print(\"[INFO] Iniciando el proceso de agregar nota a la biblioteca...\")\n", "\n", " # Obtener la biblioteca\n", " print(f\"[INFO] Buscando biblioteca con ID: {biblioteca_id}\")\n", " repo_biblioteca = BibliotecaRepo(conexion)\n", " biblioteca = repo_biblioteca.get_by_id(biblioteca_id)\n", " if biblioteca is None:\n", " print(f\"[ERROR] No se encontró la biblioteca con ID {biblioteca_id}\")\n", " raise ValueError(f\"No se encontró la biblioteca con ID {biblioteca_id}\")\n", " print(f\"[INFO] Biblioteca encontrada: {biblioteca.nombre} (vector_dim={biblioteca.vector_dim})\")\n", "\n", " # Crear objeto Nota\n", " print(f\"[INFO] Creando objeto Nota con título: {titulo}\")\n", " nota = Nota(\n", " titulo=titulo,\n", " texto=texto,\n", " tags=tags or [],\n", " conexiones=conexiones or [],\n", " resumen=resumen or \"\",\n", " # vector=biblioteca.embedder.embed_text(texto),\n", " # vector_resumen=biblioteca.embedder.embed_text(resumen) if resumen else None\n", " )\n", " # Mostrar atributos seguros\n", " print(\n", " f\"[DEBUG] Nota creada: titulo='{nota.titulo}', \"\n", " f\"texto_len={len(nota.texto)}, \"\n", " f\"tags={len(nota.tags)}, \"\n", " f\"conexiones={len(nota.conexiones)}, \"\n", " f\"resumen_len={len(nota.resumen)}\"\n", " )\n", "\n", " # Preparar tabla y modelo de nota\n", " print(f\"[INFO] Generando tabla y modelo de Nota para la biblioteca: {biblioteca.nombre}\")\n", " metadata = MetaData()\n", " tabla, ModeloNota = generar_tabla_nota_para_biblioteca(\n", " biblioteca.nombre,\n", " biblioteca.vector_dim,\n", " metadata\n", " )\n", " print(f\"[INFO] Creando tabla en la base de datos si no existe...\")\n", " metadata.create_all(conexion.get_engine())\n", " print(f\"[INFO] Tabla '{tabla.name}' verificada/creada.\")\n", "\n", " # Guardar la nota\n", " print(f\"[INFO] Guardando nota en la base de datos...\")\n", " repo_nota = NotaRepo(conexion.get_session(), ModeloNota)\n", " nota_id = repo_nota.add(nota)\n", " print(f\"[INFO] Nota guardada con ID: {nota_id}\")\n", "\n", " resultado = {\n", " \"mensaje\": f\"Nota '{titulo}' agregada con éxito a la biblioteca '{biblioteca.nombre}'.\",\n", " \"nota_id\": nota_id\n", " }\n", "\n", " print(f\"[SUCCESS] {resultado['mensaje']}\")\n", " return resultado\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "8e57e511", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[INFO] Iniciando el proceso de agregar nota a la biblioteca...\n", "[INFO] Buscando biblioteca con ID: BBLI20250511-a91dbb2168172979414\n", "[INFO] Biblioteca encontrada: biblio_Pruebas_1 (vector_dim=3072)\n", "[INFO] Creando objeto Nota con título: fdsfdsfsdfewww\n", "[DEBUG] Nota creada: titulo='fdsfdsfsdfewww', texto_len=0, tags=0, conexiones=0, resumen_len=0\n", "[INFO] Generando tabla y modelo de Nota para la biblioteca: biblio_Pruebas_1\n", "[INFO] Creando tabla en la base de datos si no existe...\n", "[INFO] Tabla 'biblio_pruebas_1' verificada/creada.\n", "[INFO] Guardando nota en la base de datos...\n", "[INFO] Nota guardada con ID: NOTA20250511-0cf0187e58905045667\n", "[SUCCESS] Nota 'fdsfdsfsdfewww' agregada con éxito a la biblioteca 'biblio_Pruebas_1'.\n" ] }, { "data": { "text/plain": [ "{'mensaje': \"Nota 'fdsfdsfsdfewww' agregada con éxito a la biblioteca 'biblio_Pruebas_1'.\",\n", " 'nota_id': 'NOTA20250511-0cf0187e58905045667'}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from entrypoint.init_db import db_credencial\n", "conexion_admin = PostgresConexion(db_credencial)\n", "\n", "agregar_nota_a_biblioteca(\n", " conexion=conexion_admin,\n", " biblioteca_id=\"BBLI20250511-a91dbb2168172979414\",\n", " titulo=\"fdsfdsfsdfewww\"\n", ")" ] }, { "cell_type": "code", "execution_count": 12, "id": "431f24f1", "metadata": {}, "outputs": [], "source": [ "def listar_notas_de_biblioteca(conexion: PostgresConexion, biblioteca_id: str) -> list[dict]:\n", " repo_biblioteca = BibliotecaRepo(conexion)\n", " biblioteca = repo_biblioteca.get_by_id(biblioteca_id)\n", " if not biblioteca:\n", " raise ValueError(f\"No se encontró la biblioteca con ID: {biblioteca_id}\")\n", "\n", " metadata = MetaData()\n", " tabla, ModeloNota = generar_tabla_nota_para_biblioteca(biblioteca.nombre, biblioteca.vector_dim, metadata)\n", " metadata.create_all(conexion.get_engine())\n", "\n", " repo_nota = NotaRepo(conexion.get_session(), ModeloNota)\n", " notas = repo_nota.get_all()\n", " return [\n", " {\n", " \"id\": n.id,\n", " \"titulo\": n.titulo,\n", " \"tags\": n.tags,\n", " \"texto\": n.texto,\n", " \"resumen\": n.resumen,\n", " \"conexiones\": n.conexiones\n", " }\n", " for n in notas\n", " ]" ] }, { "cell_type": "code", "execution_count": 13, "id": "ae4f2994", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'id': 'NOTA20250511-6cf9b177a2249549641',\n", " 'titulo': 'Hola que tal',\n", " 'tags': [],\n", " 'texto': '',\n", " 'resumen': '',\n", " 'conexiones': []},\n", " {'id': 'NOTA20250511-664831e1bd118315114',\n", " 'titulo': 'Holoooo',\n", " 'tags': [],\n", " 'texto': '',\n", " 'resumen': '',\n", " 'conexiones': []},\n", " {'id': 'NOTA20250511-04dbb203a9126228444',\n", " 'titulo': 'sajdhasjdhasjdh',\n", " 'tags': [],\n", " 'texto': '',\n", " 'resumen': '',\n", " 'conexiones': []},\n", " {'id': 'NOTA20250511-668f68e425271569668',\n", " 'titulo': 'fsdfdf',\n", " 'tags': [],\n", " 'texto': '',\n", " 'resumen': '',\n", " 'conexiones': []},\n", " {'id': 'NOTA20250511-a6ad7166d5660286632',\n", " 'titulo': 'fsdfdf',\n", " 'tags': [],\n", " 'texto': '',\n", " 'resumen': '',\n", " 'conexiones': []},\n", " {'id': 'NOTA20250511-639fa883c2266940759',\n", " 'titulo': 'fsdfdf',\n", " 'tags': [],\n", " 'texto': '',\n", " 'resumen': '',\n", " 'conexiones': []},\n", " {'id': 'NOTA20250511-27e86a8da6529002860',\n", " 'titulo': 'fsdfdf',\n", " 'tags': [],\n", " 'texto': '',\n", " 'resumen': '',\n", " 'conexiones': []},\n", " {'id': 'NOTA20250511-06479aca23973772356',\n", " 'titulo': 'fsdfdf',\n", " 'tags': [],\n", " 'texto': '',\n", " 'resumen': '',\n", " 'conexiones': []},\n", " {'id': 'NOTA20250511-0d577e3336174429305',\n", " 'titulo': 'fdsfdsfsdfewww',\n", " 'tags': [],\n", " 'texto': '',\n", " 'resumen': '',\n", " 'conexiones': []},\n", " {'id': 'NOTA20250511-0cf0187e58905045667',\n", " 'titulo': 'fdsfdsfsdfewww',\n", " 'tags': [],\n", " 'texto': '',\n", " 'resumen': '',\n", " 'conexiones': []}]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "listar_notas_de_biblioteca(\n", " conexion=conexion_admin,\n", " biblioteca_id=\"BBLI20250511-a91dbb2168172979414\"\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.11" } }, "nbformat": 4, "nbformat_minor": 5 }