endpoint_biblioteca_funcionando
This commit is contained in:
@@ -4,6 +4,6 @@ from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/ping")
|
||||
@router.get("/")
|
||||
async def ping():
|
||||
return {"message": "pong"}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
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))
|
||||
@@ -0,0 +1,21 @@
|
||||
# backend/api/v1/biblioteca.py
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel
|
||||
from backend.db.conexion import get_conexion
|
||||
from backend.services.text_manager import crear_biblioteca
|
||||
from src.ConexionSql.Postgres_conexion import PostgresConexion
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class BibliotecaInput(BaseModel):
|
||||
nombre_biblioteca: str
|
||||
descripcion: str
|
||||
|
||||
@router.post("/")
|
||||
def crear_biblioteca_endpoint(
|
||||
data: BibliotecaInput,
|
||||
conexion: PostgresConexion = Depends(get_conexion)
|
||||
):
|
||||
return crear_biblioteca(nombre_biblioteca=data.nombre_biblioteca,
|
||||
descripcion=data.descripcion,
|
||||
conexion=conexion)
|
||||
@@ -1,8 +1,8 @@
|
||||
# backend/api/router.py
|
||||
|
||||
from fastapi import APIRouter
|
||||
from backend.api.v1.endpoints import ping, nota, text_manager
|
||||
from backend.api.v1.endpoints import ping, text_manager_endpoint
|
||||
|
||||
router = APIRouter()
|
||||
router.include_router(ping.router, prefix="/api/v1/ping")
|
||||
router.include_router(text_manager.router, prefix="/api/v1/bibliotecas")
|
||||
router.include_router(text_manager_endpoint.router, prefix="/api/v1/text_manager")
|
||||
|
||||
+7
-16
@@ -1,19 +1,10 @@
|
||||
# backend/db/conexion.py
|
||||
from entrypoint.init_db import db_credencial
|
||||
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)
|
||||
conexion = PostgresConexion(db_credencial)
|
||||
try:
|
||||
yield conexion
|
||||
finally:
|
||||
conexion.close()
|
||||
@@ -0,0 +1,30 @@
|
||||
from src.TextManager.biblioteca import Biblioteca
|
||||
from src.TextManager.biblioteca_mmr import BibliotecaRepo
|
||||
from src.Llms.Embedders.Openai_embedder import OpenAIEmbedder
|
||||
from src.ApiKeys.openai_apikey_mmr import OpenAICredencialRepo
|
||||
from src.ConexionSql.Postgres_conexion import PostgresConexion
|
||||
from backend.db.conexion import get_conexion
|
||||
|
||||
|
||||
def crear_biblioteca(nombre_biblioteca: str, conexion: PostgresConexion, descripcion: str):
|
||||
cred_repo = OpenAICredencialRepo(conexion)
|
||||
credencial = cred_repo.get_by_id("OPAK20250510-ac2cea8af3110632314")
|
||||
|
||||
embedder = OpenAIEmbedder(credencial, model="text-embedding-3-large")
|
||||
biblioteca = Biblioteca(nombre=nombre_biblioteca,
|
||||
embedder=embedder,
|
||||
descripcion=descripcion)
|
||||
|
||||
repo = BibliotecaRepo(conexion)
|
||||
repo.add(biblioteca=biblioteca)
|
||||
|
||||
biblioteca.generar_modelo_notas(conexion)
|
||||
|
||||
return {
|
||||
"mensaje": f"Biblioteca '{nombre_biblioteca}' creada con éxito.",
|
||||
"id": biblioteca.id
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
crear_biblioteca("hola_intento5")
|
||||
Reference in New Issue
Block a user