232 lines
12 KiB
Plaintext
232 lines
12 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"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": 9,
|
|
"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": 11,
|
|
"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: sajdhasjdhasjdh\n",
|
|
"[DEBUG] Nota creada: titulo='sajdhasjdhasjdh', 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-04dbb203a9126228444\n",
|
|
"[SUCCESS] Nota 'sajdhasjdhasjdh' agregada con éxito a la biblioteca 'biblio_Pruebas_1'.\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'mensaje': \"Nota 'sajdhasjdhasjdh' agregada con éxito a la biblioteca 'biblio_Pruebas_1'.\",\n",
|
|
" 'nota_id': 'NOTA20250511-04dbb203a9126228444'}"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"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=\"sajdhasjdhasjdh\"\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"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": 14,
|
|
"id": "ae4f2994",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"E:\\Fitz_Studio\\src\\TextManager\\notas_biblioteca_mmr.py:51: SAWarning: This declarative base already contains a class with the same class name and module name as src.TextManager.notas_biblioteca_mmr.NotaModel, and will be replaced in the string-lookup table.\n",
|
|
" mapper_registry.map_imperatively(NotaModel, tabla)\n"
|
|
]
|
|
},
|
|
{
|
|
"ename": "TypeError",
|
|
"evalue": "'NoneType' object is not iterable",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
|
"\u001b[31mTypeError\u001b[39m Traceback (most recent call last)",
|
|
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[14]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mlistar_notas_de_biblioteca\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 2\u001b[39m \u001b[43m \u001b[49m\u001b[43mconexion\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconexion_admin\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 3\u001b[39m \u001b[43m \u001b[49m\u001b[43mbiblioteca_id\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mBBLI20250511-a91dbb2168172979414\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\n\u001b[32m 4\u001b[39m \u001b[43m)\u001b[49m\n",
|
|
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[13]\u001b[39m\u001b[32m, line 12\u001b[39m, in \u001b[36mlistar_notas_de_biblioteca\u001b[39m\u001b[34m(conexion, biblioteca_id)\u001b[39m\n\u001b[32m 9\u001b[39m metadata.create_all(conexion.get_engine())\n\u001b[32m 11\u001b[39m repo_nota = NotaRepo(conexion.get_session(), ModeloNota)\n\u001b[32m---> \u001b[39m\u001b[32m12\u001b[39m notas = \u001b[43mrepo_nota\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget_all\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 13\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m [\n\u001b[32m 14\u001b[39m {\n\u001b[32m 15\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mid\u001b[39m\u001b[33m\"\u001b[39m: n.id,\n\u001b[32m (...)\u001b[39m\u001b[32m 22\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m n \u001b[38;5;129;01min\u001b[39;00m notas\n\u001b[32m 23\u001b[39m ]\n",
|
|
"\u001b[36mFile \u001b[39m\u001b[32mE:\\Fitz_Studio\\src\\TextManager\\notas_biblioteca_mmr.py:109\u001b[39m, in \u001b[36mget_all\u001b[39m\u001b[34m(self)\u001b[39m\n\u001b[32m 0\u001b[39m <Error retrieving source code with stack_data see ipython/ipython#13598>\n",
|
|
"\u001b[36mFile \u001b[39m\u001b[32mE:\\Fitz_Studio\\src\\TextManager\\notas_biblioteca_mmr.py:109\u001b[39m, in \u001b[36m<listcomp>\u001b[39m\u001b[34m(.0)\u001b[39m\n\u001b[32m 0\u001b[39m <Error retrieving source code with stack_data see ipython/ipython#13598>\n",
|
|
"\u001b[36mFile \u001b[39m\u001b[32mE:\\Fitz_Studio\\src\\TextManager\\notas_biblioteca_mmr.py:82\u001b[39m, in \u001b[36mfrom_model\u001b[39m\u001b[34m(model)\u001b[39m\n\u001b[32m 76\u001b[39m \u001b[38;5;129m@staticmethod\u001b[39m\n\u001b[32m 77\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mfrom_model\u001b[39m(model) -> Nota:\n\u001b[32m 78\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m Nota(\n\u001b[32m 79\u001b[39m \u001b[38;5;28mid\u001b[39m=model.id,\n\u001b[32m 80\u001b[39m titulo=model.titulo,\n\u001b[32m 81\u001b[39m tags=model.tags.split(\u001b[33m\"\u001b[39m\u001b[33m,\u001b[39m\u001b[33m\"\u001b[39m) \u001b[38;5;28;01mif\u001b[39;00m model.tags \u001b[38;5;28;01melse\u001b[39;00m [],\n\u001b[32m---> \u001b[39m\u001b[32m82\u001b[39m conexiones=model.conexiones.split(\u001b[33m\"\u001b[39m\u001b[33m,\u001b[39m\u001b[33m\"\u001b[39m) \u001b[38;5;28;01mif\u001b[39;00m model.conexiones \u001b[38;5;28;01melse\u001b[39;00m [],\n\u001b[32m 83\u001b[39m texto=model.texto,\n\u001b[32m 84\u001b[39m resumen=model.resumen,\n\u001b[32m 85\u001b[39m vector=\u001b[38;5;28mlist\u001b[39m(model.vector),\n\u001b[32m 86\u001b[39m vector_resumen=\u001b[38;5;28mlist\u001b[39m(model.vector_resumen) \u001b[38;5;28;01mif\u001b[39;00m model.vector_resumen \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m 87\u001b[39m )\n",
|
|
"\u001b[31mTypeError\u001b[39m: 'NoneType' object is not iterable"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|