feat: Implement text manager API and database connection
- Added `text_manager.py` to handle the creation of text libraries via FastAPI. - Introduced database connection management in `conexion.py` using PostgreSQL credentials from environment variables. - Created abstract base class `EmbedderABC` in `Base_Embedder.py` for embedding models. - Developed `OpenAIEmbedder` class to generate embeddings using OpenAI's API. - Implemented `OpenAIEmbedderModel` and repository pattern for managing OpenAI embedders in `Openai_embedder_mmr.py`. - Established `Biblioteca` class for managing text libraries and their associated notes in `biblioteca.py`. - Created SQLAlchemy models and mappers for `Biblioteca` and `Nota` in `biblioteca_mmr.py` and `notas_biblioteca_mmr.py`. - Added functionality for dynamic table generation for notes associated with libraries. - Included comprehensive methods for adding, retrieving, and managing notes and libraries in their respective repositories.
This commit is contained in:
+51
-12
@@ -5,15 +5,7 @@
|
||||
"execution_count": 1,
|
||||
"id": "26aa8e2b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"✅ Credencial: Credencial_enmanuel\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from src.ApiKeys.openai_apikey import OpenAICredencial\n",
|
||||
"from src.ApiKeys.openai_apikey_mmr import OpenAICredencialRepo # Ajusta si está en otro módulo\n",
|
||||
@@ -24,14 +16,61 @@
|
||||
"conexion_admin = PostgresConexion(db_credencial)\n",
|
||||
"\n",
|
||||
"# 3. Guardar la credencial en la base de datos\n",
|
||||
"repo = OpenAICredencialRepo(conexion_admin)\n",
|
||||
"credencial_openai = repo.get_by_id(1)\n",
|
||||
"repo = OpenAICredencialRepo(conexion_admin)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4c232ecd",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'OPAK20250510-ac2cea8af3110632314'"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# apikey_gpt = OpenAICredencial(titulo=\"Credencial_enmanuel_gpt\")\n",
|
||||
"# repo.add(apikey_gpt)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "32552452",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"✅ Credencial: Credencial_enmanuel_gpt\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"credencial_openai = repo.get_by_id('OPAK20250510-ac2cea8af3110632314')\n",
|
||||
"print(f\"✅ Credencial: {credencial_openai.titulo}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": null,
|
||||
"id": "7464fa65",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "e5b665a6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
|
||||
File diff suppressed because one or more lines are too long
+7
-7
@@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"id": "5206b9c6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -14,7 +14,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 2,
|
||||
"id": "63a0b954",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -25,7 +25,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"id": "0575f424",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -38,17 +38,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"id": "a5266309",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1"
|
||||
"'PGCR20250510-02f3cf9610127084237'"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -72,7 +72,7 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"modelo_cred = repo_cred.get_by_id(1)\n",
|
||||
"modelo_cred = repo_cred.get_by_id(\"PGCR20250510-02f3cf9610127084237\")\n",
|
||||
"\n",
|
||||
"print(modelo_cred.titulo)"
|
||||
]
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from backend.db.conexion import get_conexion
|
||||
from src.TextManager.biblioteca import Biblioteca
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class BibliotecaIn(BaseModel):
|
||||
nombre: str
|
||||
descripcion: Optional[str] = ""
|
||||
vector_dim: Optional[int] = None
|
||||
|
||||
@router.post("/")
|
||||
def crear_biblioteca(biblio: BibliotecaIn):
|
||||
try:
|
||||
biblioteca = Biblioteca(
|
||||
nombre=biblio.nombre,
|
||||
descripcion=biblio.descripcion,
|
||||
vector_dim=biblio.vector_dim
|
||||
)
|
||||
conexion = get_conexion()
|
||||
modelo = biblioteca.generar_modelo_notas(conexion)
|
||||
return {"id": biblioteca.id, "modelo": str(modelo.__name__)}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
@@ -1,7 +1,8 @@
|
||||
# backend/api/router.py
|
||||
|
||||
from fastapi import APIRouter
|
||||
from backend.api.v1.endpoints import ping
|
||||
from backend.api.v1.endpoints import ping, nota, text_manager
|
||||
|
||||
router = APIRouter()
|
||||
router.include_router(ping.router, prefix="/api/v1")
|
||||
router.include_router(ping.router, prefix="/api/v1/ping")
|
||||
router.include_router(text_manager.router, prefix="/api/v1/bibliotecas")
|
||||
@@ -0,0 +1,19 @@
|
||||
from src.ConexionSql.Postgres_conexion import PostgresConexion
|
||||
from src.Credenciales.postgres_credencial import PostgresCredencial
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
from entrypoint import ENV_PATH
|
||||
|
||||
# Cargar .env
|
||||
load_dotenv(ENV_PATH)
|
||||
|
||||
def get_conexion():
|
||||
db_credencial = PostgresCredencial(
|
||||
titulo=os.getenv("DB_TITLE"),
|
||||
user=os.getenv("DB_USER"),
|
||||
password=os.getenv("DB_PASSWORD"),
|
||||
host=os.getenv("DB_HOST"),
|
||||
port=os.getenv("DB_PORT"),
|
||||
dbname=os.getenv("DB_NAME")
|
||||
)
|
||||
return PostgresConexion(db_credencial)
|
||||
+1
-1
@@ -13,7 +13,7 @@ app = FastAPI(
|
||||
# Configuración de CORS
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["http://localhost:5173"], # Solo permite tu frontend local
|
||||
allow_origins=["http://localhost:5173", "http://0.0.0.0:5173"], # Solo permite tu frontend local
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
|
||||
+42
-1
@@ -41,6 +41,7 @@ E:\Fitz_Studio
|
||||
│ │ ├── 0e
|
||||
│ │ ├── 10
|
||||
│ │ ├── 11
|
||||
│ │ ├── 13
|
||||
│ │ ├── 15
|
||||
│ │ ├── 17
|
||||
│ │ ├── 18
|
||||
@@ -55,9 +56,11 @@ E:\Fitz_Studio
|
||||
│ │ ├── 22
|
||||
│ │ ├── 23
|
||||
│ │ ├── 24
|
||||
│ │ ├── 25
|
||||
│ │ ├── 26
|
||||
│ │ ├── 27
|
||||
│ │ ├── 28
|
||||
│ │ ├── 2b
|
||||
│ │ ├── 2c
|
||||
│ │ ├── 2d
|
||||
│ │ ├── 2f
|
||||
@@ -65,11 +68,14 @@ E:\Fitz_Studio
|
||||
│ │ ├── 32
|
||||
│ │ ├── 33
|
||||
│ │ ├── 34
|
||||
│ │ ├── 36
|
||||
│ │ ├── 39
|
||||
│ │ ├── 3a
|
||||
│ │ ├── 3c
|
||||
│ │ ├── 3d
|
||||
│ │ ├── 3e
|
||||
│ │ ├── 3f
|
||||
│ │ ├── 40
|
||||
│ │ ├── 41
|
||||
│ │ ├── 42
|
||||
│ │ ├── 43
|
||||
@@ -83,10 +89,14 @@ E:\Fitz_Studio
|
||||
│ │ ├── 4d
|
||||
│ │ ├── 4e
|
||||
│ │ ├── 4f
|
||||
│ │ ├── 50
|
||||
│ │ ├── 51
|
||||
│ │ ├── 52
|
||||
│ │ ├── 55
|
||||
│ │ ├── 56
|
||||
│ │ ├── 57
|
||||
│ │ ├── 58
|
||||
│ │ ├── 59
|
||||
│ │ ├── 5a
|
||||
│ │ ├── 5b
|
||||
│ │ ├── 5c
|
||||
@@ -100,6 +110,7 @@ E:\Fitz_Studio
|
||||
│ │ ├── 65
|
||||
│ │ ├── 67
|
||||
│ │ ├── 69
|
||||
│ │ ├── 6b
|
||||
│ │ ├── 6c
|
||||
│ │ ├── 6d
|
||||
│ │ ├── 6e
|
||||
@@ -109,27 +120,33 @@ E:\Fitz_Studio
|
||||
│ │ ├── 75
|
||||
│ │ ├── 76
|
||||
│ │ ├── 77
|
||||
│ │ ├── 79
|
||||
│ │ ├── 7b
|
||||
│ │ ├── 7c
|
||||
│ │ ├── 7d
|
||||
│ │ ├── 7f
|
||||
│ │ ├── 80
|
||||
│ │ ├── 81
|
||||
│ │ ├── 82
|
||||
│ │ ├── 83
|
||||
│ │ ├── 84
|
||||
│ │ ├── 85
|
||||
│ │ ├── 86
|
||||
│ │ ├── 87
|
||||
│ │ ├── 89
|
||||
│ │ ├── 8a
|
||||
│ │ ├── 8b
|
||||
│ │ ├── 8c
|
||||
│ │ ├── 8d
|
||||
│ │ ├── 90
|
||||
│ │ ├── 92
|
||||
│ │ ├── 94
|
||||
│ │ ├── 95
|
||||
│ │ ├── 97
|
||||
│ │ ├── 98
|
||||
│ │ ├── 99
|
||||
│ │ ├── 9a
|
||||
│ │ ├── 9b
|
||||
│ │ ├── 9c
|
||||
│ │ ├── 9d
|
||||
│ │ ├── a0
|
||||
@@ -148,7 +165,9 @@ E:\Fitz_Studio
|
||||
│ │ ├── ad
|
||||
│ │ ├── ae
|
||||
│ │ ├── af
|
||||
│ │ ├── b0
|
||||
│ │ ├── b1
|
||||
│ │ ├── b2
|
||||
│ │ ├── b3
|
||||
│ │ ├── b4
|
||||
│ │ ├── b5
|
||||
@@ -158,6 +177,7 @@ E:\Fitz_Studio
|
||||
│ │ ├── ba
|
||||
│ │ ├── bb
|
||||
│ │ ├── bc
|
||||
│ │ ├── bd
|
||||
│ │ ├── bf
|
||||
│ │ ├── c0
|
||||
│ │ ├── c3
|
||||
@@ -171,6 +191,7 @@ E:\Fitz_Studio
|
||||
│ │ ├── ce
|
||||
│ │ ├── cf
|
||||
│ │ ├── d1
|
||||
│ │ ├── d3
|
||||
│ │ ├── d4
|
||||
│ │ ├── d5
|
||||
│ │ ├── d6
|
||||
@@ -184,6 +205,8 @@ E:\Fitz_Studio
|
||||
│ │ ├── de
|
||||
│ │ ├── df
|
||||
│ │ ├── e0
|
||||
│ │ ├── e1
|
||||
│ │ ├── e2
|
||||
│ │ ├── e3
|
||||
│ │ ├── e4
|
||||
│ │ ├── e5
|
||||
@@ -202,6 +225,7 @@ E:\Fitz_Studio
|
||||
│ │ ├── f6
|
||||
│ │ ├── f7
|
||||
│ │ ├── f9
|
||||
│ │ ├── fa
|
||||
│ │ ├── fb
|
||||
│ │ ├── fc
|
||||
│ │ ├── fd
|
||||
@@ -298,6 +322,7 @@ E:\Fitz_Studio
|
||||
│ ├── jupyter
|
||||
│ └── man
|
||||
├── Apikeys.ipynb
|
||||
├── Apikeys_embedding.ipynb
|
||||
├── Credenciales.ipynb
|
||||
├── Encriptacion.ipynb
|
||||
├── README.md
|
||||
@@ -310,6 +335,9 @@ E:\Fitz_Studio
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── __pycache__
|
||||
│ │ └── v1
|
||||
│ ├── db
|
||||
│ │ ├── __init__.py
|
||||
│ │ └── conexion.py
|
||||
│ ├── deps
|
||||
│ │ ├── __init__.py
|
||||
│ │ └── auth.py
|
||||
@@ -721,16 +749,19 @@ E:\Fitz_Studio
|
||||
│ │ ├── queue-microtask
|
||||
│ │ ├── range-parser
|
||||
│ │ ├── raw-body
|
||||
│ │ ├── re-resizable
|
||||
│ │ ├── react
|
||||
│ │ ├── react-docgen
|
||||
│ │ ├── react-docgen-typescript
|
||||
│ │ ├── react-dom
|
||||
│ │ ├── react-draggable
|
||||
│ │ ├── react-is
|
||||
│ │ ├── react-number-format
|
||||
│ │ ├── react-reconciler
|
||||
│ │ ├── react-refresh
|
||||
│ │ ├── react-remove-scroll
|
||||
│ │ ├── react-remove-scroll-bar
|
||||
│ │ ├── react-rnd
|
||||
│ │ ├── react-router
|
||||
│ │ ├── react-router-dom
|
||||
│ │ ├── react-style-singleton
|
||||
@@ -892,9 +923,9 @@ E:\Fitz_Studio
|
||||
│ │ ├── Router.tsx
|
||||
│ │ ├── assets
|
||||
│ │ ├── components
|
||||
│ │ ├── data
|
||||
│ │ ├── main.tsx
|
||||
│ │ ├── pages
|
||||
│ │ ├── public
|
||||
│ │ ├── theme.ts
|
||||
│ │ ├── types
|
||||
│ │ └── vite-env.d.ts
|
||||
@@ -905,6 +936,7 @@ E:\Fitz_Studio
|
||||
│ ├── vite.config.js
|
||||
│ ├── vitest.setup.mjs
|
||||
│ └── yarn.lock
|
||||
├── github_tutorial.ipynb
|
||||
├── main.py
|
||||
├── notebooks
|
||||
│ └── hacer_script_nombres.ipynb
|
||||
@@ -958,6 +990,7 @@ E:\Fitz_Studio
|
||||
│ │ └── postgres_credencial_mmr.py
|
||||
│ ├── Llms
|
||||
│ │ ├── Agente.py
|
||||
│ │ ├── Embedders
|
||||
│ │ ├── MCPs
|
||||
│ │ ├── Memory
|
||||
│ │ ├── Modelos
|
||||
@@ -965,8 +998,16 @@ E:\Fitz_Studio
|
||||
│ │ └── __pycache__
|
||||
│ ├── Security
|
||||
│ │ ├── Encriptar.py
|
||||
│ │ ├── GenerarIDs.py
|
||||
│ │ ├── __init__.py
|
||||
│ │ └── __pycache__
|
||||
│ ├── TextManager
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── __pycache__
|
||||
│ │ ├── biblioteca.py
|
||||
│ │ ├── biblioteca_mmr.py
|
||||
│ │ ├── nota.py
|
||||
│ │ └── notas_biblioteca_mmr.py
|
||||
│ ├── __init__.py
|
||||
│ ├── __pycache__
|
||||
│ │ ├── __init__.cpython-311.pyc
|
||||
|
||||
@@ -7,6 +7,8 @@ from src.Credenciales.postgres_credencial import PostgresCredencial # Asegúrat
|
||||
from src.Credenciales.postgres_credencial_mmr import PostgresCredencialModel
|
||||
from src.ApiKeys.openai_apikey_mmr import OpenAICredencialModel
|
||||
from src.Llms.Modelos.Openai_model_mmr import ModeloOpenAIConfigModel
|
||||
from src.TextManager.biblioteca_mmr import BibliotecaModel
|
||||
from src.Llms.Embedders.Openai_embedder_mmr import OpenAIEmbedderModel
|
||||
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
@@ -28,3 +28,4 @@ def save_tree_to_file(start_path='.', max_depth=2, output_file='tree.txt'):
|
||||
# Ejemplo de uso:
|
||||
# Puedes cambiar estos valores según lo necesites
|
||||
save_tree_to_file(start_path=r'E:\Fitz_Studio', max_depth=3, output_file=r'E:\Fitz_Studio\data\files\txt\tree.txt')
|
||||
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
from src.Security.GenerarIDs import GeneradorIDUnico
|
||||
|
||||
class OpenAICredencial:
|
||||
def __init__(self, titulo: str, api_key: str, organizacion: str = None):
|
||||
def __init__(self, titulo: str, api_key: str, organizacion: str = None, id: str = None):
|
||||
"""
|
||||
:param titulo: Nombre descriptivo para esta credencial.
|
||||
:param api_key: Clave secreta de la API de OpenAI.
|
||||
:param organizacion: (Opcional) ID de la organización asociada a la cuenta de OpenAI.
|
||||
"""
|
||||
self.titulo = titulo
|
||||
self.id = id if id is not None else GeneradorIDUnico("OPAK").generar()
|
||||
self.titulo = titulo
|
||||
self.api_key = api_key
|
||||
self.organizacion = organizacion
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ if pssword is None:
|
||||
class OpenAICredencialModel(Base):
|
||||
__tablename__ = 'openai_credenciales'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
id = Column(String, primary_key=True)
|
||||
titulo = Column(String, nullable=False)
|
||||
api_key = Column(String, nullable=False) # Encriptada como base64 string
|
||||
organizacion = Column(String, nullable=True)
|
||||
@@ -37,6 +37,7 @@ class OpenAICredencialMapper:
|
||||
@staticmethod
|
||||
def to_dict(obj: OpenAICredencial) -> dict:
|
||||
return {
|
||||
"id": obj.id,
|
||||
"titulo": obj.titulo,
|
||||
"api_key": base64.b64encode(
|
||||
Encriptar_fernet.encriptar(obj.api_key, pssword)
|
||||
@@ -47,6 +48,7 @@ class OpenAICredencialMapper:
|
||||
@staticmethod
|
||||
def from_dict(data: dict) -> OpenAICredencial:
|
||||
return OpenAICredencial(
|
||||
id=data["id"],
|
||||
titulo=data["titulo"],
|
||||
api_key=Encriptar_fernet.desencriptar(
|
||||
base64.b64decode(data["api_key"]), pssword
|
||||
@@ -57,6 +59,7 @@ class OpenAICredencialMapper:
|
||||
@staticmethod
|
||||
def from_model(model: OpenAICredencialModel) -> OpenAICredencial:
|
||||
return OpenAICredencial(
|
||||
id=model.id,
|
||||
titulo=model.titulo,
|
||||
api_key=Encriptar_fernet.desencriptar(
|
||||
base64.b64decode(model.api_key), pssword
|
||||
@@ -72,7 +75,7 @@ class OpenAICredencialRepo:
|
||||
def __init__(self, conexion: ConexionBase):
|
||||
self.session = conexion.get_session()
|
||||
|
||||
def add(self, credencial: OpenAICredencial) -> int:
|
||||
def add(self, credencial: OpenAICredencial) -> str:
|
||||
data = OpenAICredencialMapper.to_dict(credencial)
|
||||
model = OpenAICredencialModel(**data)
|
||||
self.session.add(model)
|
||||
@@ -87,6 +90,6 @@ class OpenAICredencialRepo:
|
||||
model = self.session.query(OpenAICredencialModel).filter_by(titulo=titulo).first()
|
||||
return OpenAICredencialMapper.from_model(model) if model else None
|
||||
|
||||
def get_by_id(self, id_: int) -> OpenAICredencial | None:
|
||||
def get_by_id(self, id_: str) -> OpenAICredencial | None:
|
||||
model = self.session.get(OpenAICredencialModel, id_)
|
||||
return OpenAICredencialMapper.from_model(model) if model else None
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.engine import Engine
|
||||
|
||||
class ConexionBase(ABC):
|
||||
@abstractmethod
|
||||
def get_session(self) -> Session:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_engine(self) -> Engine:
|
||||
pass
|
||||
@@ -1,12 +1,13 @@
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.orm import sessionmaker, Session
|
||||
from sqlalchemy.engine import Engine
|
||||
|
||||
|
||||
from src.ConexionSql.Base_conexion import ConexionBase
|
||||
from src.Credenciales.postgres_credencial import PostgresCredencial
|
||||
|
||||
|
||||
class PostgresConexion(ConexionBase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.estado = "pendiente"
|
||||
@@ -18,25 +19,29 @@ class PostgresConexion(ConexionBase):
|
||||
self.port = credencial.port
|
||||
self.dbname = credencial.dbname
|
||||
self.user = credencial.user
|
||||
self.password = credencial.password # se guarda la contraseña
|
||||
self.password = credencial.password
|
||||
uri = credencial.get_uri()
|
||||
else:
|
||||
self.user = kwargs.get("user")
|
||||
self.password = kwargs.get("password") # se guarda la contraseña
|
||||
self.password = kwargs.get("password")
|
||||
self.host = kwargs.get("host")
|
||||
self.port = kwargs.get("port", 5432)
|
||||
self.dbname = kwargs.get("db") or kwargs.get("dbname")
|
||||
uri = f"postgresql://{self.user}:{self.password}@{self.host}:{self.port}/{self.dbname}"
|
||||
|
||||
self.engine = create_engine(uri)
|
||||
self.SessionLocal = sessionmaker(bind=self.engine)
|
||||
self._engine: Engine = create_engine(uri)
|
||||
self._Session = sessionmaker(bind=self._engine)
|
||||
|
||||
def get_session(self):
|
||||
return self.SessionLocal()
|
||||
|
||||
def get_session(self) -> Session:
|
||||
return self._Session()
|
||||
|
||||
def get_engine(self) -> Engine:
|
||||
return self._engine
|
||||
|
||||
def probar_conexion(self) -> bool:
|
||||
try:
|
||||
with self.engine.connect() as connection:
|
||||
with self._engine.connect() as connection:
|
||||
connection.execute(text("SELECT 1"))
|
||||
self.estado = "exito"
|
||||
return True
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
from src.Security.GenerarIDs import GeneradorIDUnico
|
||||
|
||||
class PostgresCredencial:
|
||||
def __init__(self, titulo: str, host: str, port: int, dbname: str, user: str, password: str):
|
||||
def __init__(self, titulo: str, host: str, port: int, dbname: str, user: str, password: str, id: str = None):
|
||||
self.id = id if id is not None else GeneradorIDUnico("PGCR").generar()
|
||||
self.titulo = titulo
|
||||
self.host = host
|
||||
self.port = port
|
||||
@@ -7,6 +10,7 @@ class PostgresCredencial:
|
||||
self.user = user
|
||||
self.password = password
|
||||
|
||||
|
||||
def get_uri(self) -> str:
|
||||
"""
|
||||
Retorna una URI de conexión para PostgreSQL.
|
||||
|
||||
@@ -24,7 +24,7 @@ if pssword is None:
|
||||
class PostgresCredencialModel(Base):
|
||||
__tablename__ = 'postgres_credenciales'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
id = Column(String, primary_key=True)
|
||||
titulo = Column(String, nullable=False)
|
||||
host = Column(String, nullable=False)
|
||||
port = Column(Integer, nullable=False)
|
||||
@@ -42,6 +42,7 @@ class PostgresCredencialMapper:
|
||||
@staticmethod
|
||||
def to_dict(obj: PostgresCredencial) -> dict:
|
||||
return {
|
||||
"id": obj.id,
|
||||
"titulo": obj.titulo,
|
||||
"host": obj.host,
|
||||
"port": obj.port,
|
||||
@@ -55,6 +56,7 @@ class PostgresCredencialMapper:
|
||||
@staticmethod
|
||||
def from_dict(data: dict) -> PostgresCredencial:
|
||||
return PostgresCredencial(
|
||||
id=data["id"],
|
||||
titulo=data["titulo"],
|
||||
host=data["host"],
|
||||
port=data["port"],
|
||||
@@ -68,6 +70,7 @@ class PostgresCredencialMapper:
|
||||
@staticmethod
|
||||
def from_model(model: PostgresCredencialModel) -> PostgresCredencial:
|
||||
return PostgresCredencial(
|
||||
id=model.id,
|
||||
titulo=model.titulo,
|
||||
host=model.host,
|
||||
port=model.port,
|
||||
@@ -86,7 +89,7 @@ class PostgresCredencialRepo:
|
||||
def __init__(self, conexion: ConexionBase):
|
||||
self.session = conexion.get_session()
|
||||
|
||||
def add(self, credencial: PostgresCredencial) -> int:
|
||||
def add(self, credencial: PostgresCredencial) -> str:
|
||||
data = PostgresCredencialMapper.to_dict(credencial)
|
||||
model = PostgresCredencialModel(**data)
|
||||
self.session.add(model)
|
||||
@@ -101,6 +104,6 @@ class PostgresCredencialRepo:
|
||||
model = self.session.query(PostgresCredencialModel).filter_by(titulo=titulo).first()
|
||||
return PostgresCredencialMapper.from_model(model) if model else None
|
||||
|
||||
def get_by_id(self, id_: int) -> PostgresCredencial | None:
|
||||
def get_by_id(self, id_: str) -> PostgresCredencial | None:
|
||||
model = self.session.get(PostgresCredencialModel, id_)
|
||||
return PostgresCredencialMapper.from_model(model) if model else None
|
||||
@@ -0,0 +1,13 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import List
|
||||
|
||||
class EmbedderABC(ABC):
|
||||
@abstractmethod
|
||||
def encoder(self, text: str) -> List[float]:
|
||||
"""Genera los embeddings para un texto dado."""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def dimension_number(self) -> int:
|
||||
"""Devuelve la dimensión del modelo de embedding."""
|
||||
pass
|
||||
@@ -0,0 +1,32 @@
|
||||
from typing import List
|
||||
from src.Llms.Embedders.Base_Embedder import EmbedderABC # Asegúrate de que EmbedderABC esté en este módulo
|
||||
from src.ApiKeys.openai_apikey import OpenAICredencial
|
||||
from src.ConexionApis.OpenAi_conexion import OpenAICliente
|
||||
from src.Security.GenerarIDs import GeneradorIDUnico
|
||||
|
||||
class OpenAIEmbedder(EmbedderABC):
|
||||
def __init__(self, credencial: OpenAICredencial,
|
||||
model: str,
|
||||
id: str = None):
|
||||
self.model = model
|
||||
self.client = OpenAICliente(credencial)
|
||||
self._dimension = None # Lazy loading
|
||||
self.id = id if id is not None else GeneradorIDUnico("OAMB").generar()
|
||||
|
||||
def encoder(self, text: str) -> List[float]:
|
||||
"""
|
||||
Genera los embeddings para un texto dado utilizando el modelo de OpenAI.
|
||||
"""
|
||||
response = self.client.embedding(model=self.model, input=text)
|
||||
embedding = response.data[0].embedding
|
||||
if self._dimension is None:
|
||||
self._dimension = len(embedding)
|
||||
return embedding
|
||||
|
||||
def dimension_number(self) -> int:
|
||||
"""
|
||||
Devuelve la dimensión del modelo de embedding, generando un embedding si no se ha calculado aún.
|
||||
"""
|
||||
if self._dimension is None:
|
||||
_ = self.encoder("dimension_check")
|
||||
return self._dimension
|
||||
@@ -0,0 +1,83 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from sqlalchemy import Column, String
|
||||
from src.ConexionSql.Base_conexion import ConexionBase
|
||||
from src.base import Base
|
||||
from src.Security.GenerarIDs import GeneradorIDUnico
|
||||
from src.Llms.Embedders.Openai_embedder import OpenAIEmbedder
|
||||
from src.ApiKeys.openai_apikey import OpenAICredencial
|
||||
|
||||
# ----------------------
|
||||
# Cargar configuración desde .env si se requiere
|
||||
# ----------------------
|
||||
from entrypoint import ENV_PATH
|
||||
load_dotenv(ENV_PATH)
|
||||
|
||||
# ----------------------
|
||||
# MODELO (SQLAlchemy)
|
||||
# ----------------------
|
||||
|
||||
class OpenAIEmbedderModel(Base):
|
||||
__tablename__ = "openai_embedders"
|
||||
|
||||
id = Column(String, primary_key=True)
|
||||
api_key_id = Column(String, nullable=False) # ID de la credencial asociada (clave foránea lógica)
|
||||
model = Column(String, nullable=False)
|
||||
|
||||
# ----------------------
|
||||
# MAPPER
|
||||
# ----------------------
|
||||
|
||||
class OpenAIEmbedderMapper:
|
||||
@staticmethod
|
||||
def to_dict(obj: OpenAIEmbedder) -> dict:
|
||||
return {
|
||||
"id": obj.id,
|
||||
"api_key_id": obj.client.credencial.id,
|
||||
"model": obj.model
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def from_dict(data: dict, credencial: OpenAICredencial) -> OpenAIEmbedder:
|
||||
return OpenAIEmbedder(
|
||||
id=data["id"],
|
||||
credencial=credencial,
|
||||
model=data["model"]
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def from_model(model: OpenAIEmbedderModel, credencial: OpenAICredencial) -> OpenAIEmbedder:
|
||||
return OpenAIEmbedder(
|
||||
id=model.id,
|
||||
credencial=credencial,
|
||||
model=model.model
|
||||
)
|
||||
|
||||
# ----------------------
|
||||
# REPO
|
||||
# ----------------------
|
||||
|
||||
class OpenAIEmbedderRepo:
|
||||
def __init__(self, conexion: ConexionBase):
|
||||
self.session = conexion.get_session()
|
||||
|
||||
def add(self, embedder: OpenAIEmbedder) -> str:
|
||||
data = OpenAIEmbedderMapper.to_dict(embedder)
|
||||
model = OpenAIEmbedderModel(**data)
|
||||
self.session.add(model)
|
||||
self.session.commit()
|
||||
return model.id
|
||||
|
||||
def get_by_id(self, id_: str, credencial: OpenAICredencial) -> OpenAIEmbedder | None:
|
||||
model = self.session.get(OpenAIEmbedderModel, id_)
|
||||
return OpenAIEmbedderMapper.from_model(model, credencial) if model else None
|
||||
|
||||
def get_all(self, credencial_loader: callable) -> list[OpenAIEmbedder]:
|
||||
"""
|
||||
:param credencial_loader: función que recibe un api_key_id y devuelve una instancia de OpenAICredencial
|
||||
"""
|
||||
models = self.session.query(OpenAIEmbedderModel).all()
|
||||
return [
|
||||
OpenAIEmbedderMapper.from_model(m, credencial_loader(m.api_key_id))
|
||||
for m in models
|
||||
]
|
||||
@@ -1,5 +1,6 @@
|
||||
from src.Llms.Modelos.Base_model import ModeloABC
|
||||
from src.ConexionApis.OpenAi_conexion import OpenAICliente
|
||||
from src.Security.GenerarIDs import GeneradorIDUnico
|
||||
import asyncio
|
||||
from typing import AsyncGenerator, Union
|
||||
|
||||
@@ -8,6 +9,7 @@ class ModeloOpenAI(ModeloABC):
|
||||
self,
|
||||
cliente: OpenAICliente,
|
||||
model: str = "gpt-4o",
|
||||
id: str = None,
|
||||
temperature: float = 0.7,
|
||||
top_p: float = 1.0,
|
||||
top_k: int = None,
|
||||
@@ -15,7 +17,9 @@ class ModeloOpenAI(ModeloABC):
|
||||
num_tokens_maximos: int = 512,
|
||||
use_legacy: bool = False
|
||||
):
|
||||
id = id if id is not None else GeneradorIDUnico("MOPA").generar()
|
||||
super().__init__(
|
||||
id=id,
|
||||
model=model,
|
||||
temperature=temperature,
|
||||
top_p=top_p,
|
||||
|
||||
@@ -22,7 +22,7 @@ if pssword is None:
|
||||
class ModeloOpenAIConfigModel(Base):
|
||||
__tablename__ = 'modelo_openai_configs'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
id = Column(String, primary_key=True)
|
||||
model = Column(String, nullable=False)
|
||||
temperature = Column(Float, default=0.7)
|
||||
top_p = Column(Float, default=1.0)
|
||||
@@ -39,6 +39,7 @@ class ModeloOpenAIConfigMapper:
|
||||
@staticmethod
|
||||
def to_dict(obj: ModeloOpenAI) -> dict:
|
||||
return {
|
||||
"id": obj.id,
|
||||
"model": obj.model,
|
||||
"temperature": obj.temperature,
|
||||
"top_p": obj.top_p,
|
||||
@@ -51,6 +52,7 @@ class ModeloOpenAIConfigMapper:
|
||||
@staticmethod
|
||||
def from_model(model: ModeloOpenAIConfigModel, cliente: object) -> ModeloOpenAI:
|
||||
return ModeloOpenAI(
|
||||
id=model.id,
|
||||
cliente=cliente,
|
||||
model=model.model,
|
||||
temperature=model.temperature,
|
||||
@@ -70,14 +72,14 @@ class ModeloOpenAIConfigRepo:
|
||||
self.session = conexion.get_session()
|
||||
self.cliente = cliente # Necesario para crear ModeloOpenAI
|
||||
|
||||
def add(self, config: ModeloOpenAI) -> int:
|
||||
def add(self, config: ModeloOpenAI) -> str:
|
||||
data = ModeloOpenAIConfigMapper.to_dict(config)
|
||||
model = ModeloOpenAIConfigModel(**data)
|
||||
self.session.add(model)
|
||||
self.session.commit()
|
||||
return model.id
|
||||
|
||||
def get_by_id(self, id_: int) -> ModeloOpenAI | None:
|
||||
def get_by_id(self, id_: str) -> ModeloOpenAI | None:
|
||||
model = self.session.get(ModeloOpenAIConfigModel, id_)
|
||||
return ModeloOpenAIConfigMapper.from_model(model, self.cliente) if model else None
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
from src.Security.GenerarIDs import GeneradorIDUnico
|
||||
from src.Llms.Embedders.Base_Embedder import EmbedderABC # Asegúrate de que esta ruta sea correcta
|
||||
from typing import List, Optional
|
||||
from src.ConexionSql.Base_conexion import ConexionBase
|
||||
from sqlalchemy import MetaData # Asegúrate de importar esto
|
||||
from src.TextManager.notas_biblioteca_mmr import generar_tabla_nota_para_biblioteca # Ajusta si es necesario
|
||||
|
||||
class Biblioteca:
|
||||
def __init__(
|
||||
self,
|
||||
nombre: str,
|
||||
descripcion: str = "",
|
||||
id: Optional[str] = None,
|
||||
embedder: Optional[EmbedderABC] = None,
|
||||
vector_dim: Optional[int] = None
|
||||
):
|
||||
"""
|
||||
Clase que representa una biblioteca de notas de texto.
|
||||
|
||||
:param nombre: Nombre de la biblioteca.
|
||||
:param descripcion: Breve descripción de la biblioteca.
|
||||
:param id: ID único opcional. Si no se proporciona, se genera automáticamente.
|
||||
:param embedder: Objeto que implementa EmbedderABC para generar el vector del nombre.
|
||||
:param vector_dim: Dimensión del vector si no se proporciona un embedder.
|
||||
"""
|
||||
self.id = id if id is not None else GeneradorIDUnico("BBLI").generar()
|
||||
self.nombre = nombre
|
||||
self.descripcion = descripcion
|
||||
self.embedder = embedder
|
||||
|
||||
if self.embedder is not None:
|
||||
self.vector_dim = self.embedder.dimension_number()
|
||||
elif vector_dim is not None:
|
||||
self.vector_dim = vector_dim
|
||||
else:
|
||||
raise ValueError("Debes proporcionar un 'embedder' o un 'vector_dim' explícito.")
|
||||
|
||||
def generar_modelo_notas(self, conexion: ConexionBase):
|
||||
"""
|
||||
Genera dinámicamente un modelo de notas asociado a esta biblioteca y lo crea en la base de datos.
|
||||
|
||||
:param conexion: Objeto de conexión a la base de datos.
|
||||
:return: Clase del modelo SQLAlchemy correspondiente a las notas.
|
||||
"""
|
||||
nombre_tabla = f"biblio_{self.nombre}"
|
||||
|
||||
metadata = MetaData()
|
||||
engine = conexion.get_engine()
|
||||
tabla, NotaModel = generar_tabla_nota_para_biblioteca(nombre_tabla, self.vector_dim, metadata)
|
||||
metadata.create_all(engine)
|
||||
return NotaModel
|
||||
@@ -0,0 +1,96 @@
|
||||
import os
|
||||
import base64
|
||||
from dotenv import load_dotenv
|
||||
from sqlalchemy import Column, String, Integer
|
||||
|
||||
from src.ConexionSql.Base_conexion import ConexionBase
|
||||
from src.base import Base
|
||||
from src.Security.Encriptar import Encriptar_fernet
|
||||
from src.Security.GenerarIDs import GeneradorIDUnico
|
||||
from src.Llms.Embedders.Base_Embedder import EmbedderABC
|
||||
from src.TextManager.biblioteca import Biblioteca # Suponiendo que defines la clase lógica Biblioteca aquí
|
||||
|
||||
# ----------------------
|
||||
# Cargar clave maestra
|
||||
# ----------------------
|
||||
from entrypoint import ENV_PATH
|
||||
load_dotenv(ENV_PATH)
|
||||
pssword = os.getenv('MASTER_PASSWORD')
|
||||
if pssword is None:
|
||||
raise ValueError("MASTER_PASSWORD no está definida en el archivo .env")
|
||||
|
||||
# ----------------------
|
||||
# MODELO (SQLAlchemy)
|
||||
# ----------------------
|
||||
|
||||
class BibliotecaModel(Base):
|
||||
__tablename__ = "bibliotecas"
|
||||
|
||||
id = Column(String, primary_key=True)
|
||||
nombre = Column(String, nullable=False)
|
||||
descripcion = Column(String, default="")
|
||||
vector_dim = Column(Integer, nullable=False)
|
||||
embedder_info = Column(String, nullable=True) # Se puede guardar nombre de clase o config encriptada
|
||||
|
||||
# ----------------------
|
||||
# MAPPER
|
||||
# ----------------------
|
||||
|
||||
class BibliotecaMapper:
|
||||
@staticmethod
|
||||
def to_dict(obj: Biblioteca) -> dict:
|
||||
embedder_info = type(obj.embedder).__name__ if obj.embedder else None
|
||||
return {
|
||||
"id": obj.id,
|
||||
"nombre": obj.nombre,
|
||||
"descripcion": obj.descripcion,
|
||||
"vector_dim": obj.vector_dim,
|
||||
"embedder_info": embedder_info # sin codificar ni encriptar
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def from_dict(data: dict) -> Biblioteca:
|
||||
return Biblioteca(
|
||||
id=data["id"],
|
||||
nombre=data["nombre"],
|
||||
descripcion=data["descripcion"],
|
||||
vector_dim=data["vector_dim"],
|
||||
embedder=None # Mantienes la lógica actual de no restaurarlo automáticamente
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def from_model(model: BibliotecaModel) -> Biblioteca:
|
||||
return Biblioteca(
|
||||
id=model.id,
|
||||
nombre=model.nombre,
|
||||
descripcion=model.descripcion,
|
||||
vector_dim=model.vector_dim,
|
||||
embedder=None # Se puede cargar manualmente si es necesario
|
||||
)
|
||||
|
||||
# ----------------------
|
||||
# REPO
|
||||
# ----------------------
|
||||
|
||||
class BibliotecaRepo:
|
||||
def __init__(self, conexion: ConexionBase):
|
||||
self.session = conexion.get_session()
|
||||
|
||||
def add(self, biblioteca: Biblioteca) -> str:
|
||||
data = BibliotecaMapper.to_dict(biblioteca)
|
||||
model = BibliotecaModel(**data)
|
||||
self.session.add(model)
|
||||
self.session.commit()
|
||||
return model.id
|
||||
|
||||
def get_all(self) -> list[Biblioteca]:
|
||||
models = self.session.query(BibliotecaModel).all()
|
||||
return [BibliotecaMapper.from_model(m) for m in models]
|
||||
|
||||
def get_by_nombre(self, nombre: str) -> Biblioteca | None:
|
||||
model = self.session.query(BibliotecaModel).filter_by(nombre=nombre).first()
|
||||
return BibliotecaMapper.from_model(model) if model else None
|
||||
|
||||
def get_by_id(self, id_: str) -> Biblioteca | None:
|
||||
model = self.session.get(BibliotecaModel, id_)
|
||||
return BibliotecaMapper.from_model(model) if model else None
|
||||
@@ -0,0 +1,41 @@
|
||||
from src.Security.GenerarIDs import GeneradorIDUnico
|
||||
from typing import List
|
||||
|
||||
class Nota:
|
||||
def __init__(
|
||||
self,
|
||||
titulo: str,
|
||||
tags: List[str] = None,
|
||||
conexiones: List[str] = None,
|
||||
texto: str = "",
|
||||
vector: List[float] = None,
|
||||
resumen: str = "",
|
||||
vector_resumen: List[float] = None,
|
||||
id: str = None
|
||||
):
|
||||
"""
|
||||
Clase que representa una nota de texto con estructura semántica.
|
||||
|
||||
:param titulo: Título de la nota.
|
||||
:param tags: Lista de etiquetas asociadas.
|
||||
:param conexiones: Lista de identificadores relacionados.
|
||||
:param vector: Embedding vectorial de la nota.
|
||||
:param resumen: Texto resumen de la nota.
|
||||
:param vector_resumen: Embedding del resumen.
|
||||
:param id: Identificador único (si no se proporciona, se genera automáticamente).
|
||||
"""
|
||||
self.id = id if id is not None else GeneradorIDUnico("NOTA").generar()
|
||||
self.titulo = titulo
|
||||
self.tags = tags if tags is not None else []
|
||||
self.conexiones = conexiones if conexiones is not None else []
|
||||
self.texto = texto
|
||||
self.vector = vector
|
||||
self.resumen = resumen
|
||||
self.vector_resumen = vector_resumen
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"<Nota id={self.id}, titulo='{self.titulo}', tags={len(self.tags)}, "
|
||||
f"conexiones={len(self.conexiones)}, vector_dim={len(self.vector)}, "
|
||||
f"resumen_len={len(self.resumen)}, vector_resumen_dim={len(self.vector_resumen)}>"
|
||||
)
|
||||
@@ -0,0 +1,107 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
from sqlalchemy import Column, String, Table, MetaData
|
||||
from pgvector.sqlalchemy import Vector
|
||||
from sqlalchemy.orm import registry, Session
|
||||
from src.TextManager.nota import Nota
|
||||
from src.ConexionSql.Base_conexion import ConexionBase
|
||||
|
||||
# ----------------------
|
||||
# Cargar .env
|
||||
# ----------------------
|
||||
from entrypoint import ENV_PATH
|
||||
load_dotenv(ENV_PATH)
|
||||
|
||||
# ----------------------
|
||||
# REGISTRO DINÁMICO PARA TABLAS
|
||||
# ----------------------
|
||||
mapper_registry = registry()
|
||||
|
||||
# ----------------------
|
||||
# FUNCIONES AUXILIARES
|
||||
# ----------------------
|
||||
|
||||
def generar_tabla_nota_para_biblioteca(biblioteca_nombre: str, vector_dim: int, metadata: MetaData):
|
||||
nombre_tabla = biblioteca_nombre.lower().replace(" ", "_")
|
||||
|
||||
tabla = Table(
|
||||
nombre_tabla,
|
||||
metadata,
|
||||
Column("id", String, primary_key=True),
|
||||
Column("titulo", String, nullable=False),
|
||||
Column("tags", String),
|
||||
Column("conexiones", String),
|
||||
Column("texto", String),
|
||||
Column("resumen", String),
|
||||
Column("vector", Vector(vector_dim), nullable=False),
|
||||
Column("vector_resumen", Vector(vector_dim), nullable=True), # AHORA ES OPCIONAL
|
||||
)
|
||||
|
||||
class NotaModel:
|
||||
def __init__(self, nota: Nota):
|
||||
self.id = nota.id
|
||||
self.titulo = nota.titulo
|
||||
self.tags = ",".join(nota.tags)
|
||||
self.conexiones = ",".join(nota.conexiones)
|
||||
self.texto = nota.texto
|
||||
self.resumen = nota.resumen
|
||||
self.vector = nota.vector
|
||||
self.vector_resumen = getattr(nota, "vector_resumen", None) # AHORA ES OPCIONAL
|
||||
|
||||
mapper_registry.map_imperatively(NotaModel, tabla)
|
||||
return tabla, NotaModel
|
||||
|
||||
|
||||
# ----------------------
|
||||
# MAPPER
|
||||
# ----------------------
|
||||
|
||||
class NotaMapper:
|
||||
@staticmethod
|
||||
def to_dict(nota: Nota) -> dict:
|
||||
return {
|
||||
"id": nota.id,
|
||||
"titulo": nota.titulo,
|
||||
"tags": ",".join(nota.tags),
|
||||
"conexiones": ",".join(nota.conexiones),
|
||||
"texto": nota.texto,
|
||||
"resumen": nota.resumen,
|
||||
"vector": nota.vector,
|
||||
"vector_resumen": nota.vector_resumen
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def from_model(model) -> Nota:
|
||||
return Nota(
|
||||
id=model.id,
|
||||
titulo=model.titulo,
|
||||
tags=model.tags.split(",") if model.tags else [],
|
||||
conexiones=model.conexiones.split(",") if model.conexiones else [],
|
||||
texto=model.texto,
|
||||
resumen=model.resumen,
|
||||
vector=list(model.vector),
|
||||
vector_resumen=list(model.vector_resumen) if model.vector_resumen is not None else None
|
||||
)
|
||||
|
||||
# ----------------------
|
||||
# REPO
|
||||
# ----------------------
|
||||
|
||||
class NotaRepo:
|
||||
def __init__(self, session: Session, modelo_nota):
|
||||
self.session = session
|
||||
self.ModeloNota = modelo_nota
|
||||
|
||||
def add(self, nota: Nota) -> str:
|
||||
model = self.ModeloNota(nota)
|
||||
self.session.add(model)
|
||||
self.session.commit()
|
||||
return model.id
|
||||
|
||||
def get_by_id(self, id_: str) -> Nota | None:
|
||||
model = self.session.get(self.ModeloNota, id_)
|
||||
return NotaMapper.from_model(model) if model else None
|
||||
|
||||
def get_all(self) -> list[Nota]:
|
||||
models = self.session.query(self.ModeloNota).all()
|
||||
return [NotaMapper.from_model(m) for m in models]
|
||||
Reference in New Issue
Block a user