Spaces:
Sleeping
Sleeping
Update private_gpt/server/ingest/ingest_router.py
Browse files
private_gpt/server/ingest/ingest_router.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
-
from typing import Literal
|
2 |
|
3 |
from fastapi import APIRouter, Depends, HTTPException, Request, UploadFile
|
4 |
from pydantic import BaseModel
|
5 |
|
6 |
from private_gpt.server.ingest.ingest_service import IngestService
|
7 |
from private_gpt.server.ingest.model import IngestedDoc
|
8 |
-
from private_gpt.server.utils.auth import authenticated
|
9 |
-
|
10 |
-
ingest_router = APIRouter(prefix="/v1", dependencies=[Depends(
|
11 |
|
12 |
|
13 |
class IngestResponse(BaseModel):
|
@@ -52,12 +52,36 @@ def list_ingested(request: Request) -> IngestResponse:
|
|
52 |
return IngestResponse(object="list", model="private-gpt", data=ingested_documents)
|
53 |
|
54 |
|
55 |
-
@ingest_router.delete("/ingest/{
|
56 |
-
def delete_ingested(request: Request,
|
57 |
-
"""Delete the specified
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
"""
|
62 |
service = request.state.injector.get(IngestService)
|
63 |
-
service.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Literal, List
|
2 |
|
3 |
from fastapi import APIRouter, Depends, HTTPException, Request, UploadFile
|
4 |
from pydantic import BaseModel
|
5 |
|
6 |
from private_gpt.server.ingest.ingest_service import IngestService
|
7 |
from private_gpt.server.ingest.model import IngestedDoc
|
8 |
+
#from private_gpt.server.utils.auth import authenticated
|
9 |
+
from private_gpt.server.utils.authentication import get_current_user
|
10 |
+
ingest_router = APIRouter(prefix="/v1", dependencies=[Depends(get_current_user)])
|
11 |
|
12 |
|
13 |
class IngestResponse(BaseModel):
|
|
|
52 |
return IngestResponse(object="list", model="private-gpt", data=ingested_documents)
|
53 |
|
54 |
|
55 |
+
@ingest_router.delete("/ingest/{file_name}", tags=["Ingestion"])
|
56 |
+
def delete_ingested(request: Request, file_name: str) -> None:
|
57 |
+
"""Delete all ingested Documents with the specified file name.
|
58 |
+
|
59 |
+
The `file_name` can be obtained from the `GET /ingest/list` endpoint.
|
60 |
+
All documents with the specified file name will be effectively deleted from your storage context.
|
61 |
+
"""
|
62 |
+
service = request.state.injector.get(IngestService)
|
63 |
+
|
64 |
+
# Find all doc_ids with the specified file_name
|
65 |
+
ingested_documents = service.list_ingested()
|
66 |
+
documents_to_delete = [doc.doc_id for doc in ingested_documents if doc.doc_metadata.get("file_name") == file_name]
|
67 |
+
|
68 |
+
# Delete all documents with the specified file_name
|
69 |
+
for doc_id_to_delete in documents_to_delete:
|
70 |
+
service.delete(doc_id_to_delete)
|
71 |
+
|
72 |
+
|
73 |
+
@ingest_router.get("/ingest/list_filenames", tags=["Ingestion"], response_model=List[str])
|
74 |
+
def list_ingested(request: Request) -> List[str]:
|
75 |
+
"""Lists already ingested Documents including their Document ID and metadata.
|
76 |
|
77 |
+
Those IDs can be used to filter the context used to create responses
|
78 |
+
in `/chat/completions`, `/completions`, and `/chunks` APIs.
|
79 |
"""
|
80 |
service = request.state.injector.get(IngestService)
|
81 |
+
ingested_documents: List[IngestedDoc] = service.list_ingested()
|
82 |
+
|
83 |
+
# Extract unique filenames
|
84 |
+
unique_filenames = set(doc.doc_metadata.get("file_name", "") for doc in ingested_documents)
|
85 |
+
unique_filenames_list = list(unique_filenames)
|
86 |
+
|
87 |
+
return unique_filenames_list
|