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:
2025-05-10 17:52:43 +02:00
parent c646bc1fef
commit c47b9474f4
27 changed files with 844 additions and 44 deletions
+5 -2
View File
@@ -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
+6 -3
View File
@@ -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