added RAG, Multiuser, TG bot
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.auth.deps import get_current_user
|
||||
from app.db.base import get_db
|
||||
from app.db.models import User
|
||||
from app.db.models import Document
|
||||
from app.rag.ingest import ingest_document_file
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/documents")
|
||||
def list_documents(db: Session = Depends(get_db), user: User = Depends(get_current_user)) -> list[dict[str, Any]]:
|
||||
docs = db.scalars(select(Document).where(Document.user_id == user.id).order_by(Document.created_at.desc())).all()
|
||||
return [
|
||||
{
|
||||
"id": d.id,
|
||||
"title": d.title,
|
||||
"filename": d.filename,
|
||||
"size_bytes": d.size_bytes,
|
||||
"created_at": d.created_at.isoformat() if d.created_at else None,
|
||||
}
|
||||
for d in docs
|
||||
]
|
||||
|
||||
|
||||
@router.post("/documents/upload")
|
||||
async def upload_document(
|
||||
file: UploadFile = File(...),
|
||||
title: str = Form(""),
|
||||
db: Session = Depends(get_db),
|
||||
user: User = Depends(get_current_user),
|
||||
) -> dict[str, Any]:
|
||||
raw = await file.read()
|
||||
if not raw:
|
||||
raise HTTPException(status_code=400, detail="Empty file")
|
||||
try:
|
||||
doc = await ingest_document_file(
|
||||
db,
|
||||
user_id=user.id,
|
||||
title=title.strip() or (file.filename or "document"),
|
||||
filename=file.filename or "upload.txt",
|
||||
raw_bytes=raw,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
return {"ok": True, "document": doc}
|
||||
Reference in New Issue
Block a user