Spaces:
Sleeping
Sleeping
Ilyas KHIAT
commited on
Commit
·
7495086
1
Parent(s):
3186d0c
delete docs
Browse files
main.py
CHANGED
|
@@ -91,6 +91,7 @@ async def upload_file(file: UploadFile, enterprise_data: Json[EnterpriseData]):
|
|
| 91 |
"file_name":file.filename,
|
| 92 |
"enterprise_id": enterprise_data.id,
|
| 93 |
"number_of_chunks": len(text_chunks),
|
|
|
|
| 94 |
}
|
| 95 |
else:
|
| 96 |
raise HTTPException(status_code=500, detail="Could not create vector store")
|
|
@@ -114,6 +115,15 @@ def get_documents(enterprise_id: str):
|
|
| 114 |
except Exception as e:
|
| 115 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
@app.delete("/documents/all/{enterprise_id}")
|
| 118 |
def delete_all_documents(enterprise_id: str):
|
| 119 |
try:
|
|
|
|
| 91 |
"file_name":file.filename,
|
| 92 |
"enterprise_id": enterprise_data.id,
|
| 93 |
"number_of_chunks": len(text_chunks),
|
| 94 |
+
"filename_id":vector_store["filename_id"]
|
| 95 |
}
|
| 96 |
else:
|
| 97 |
raise HTTPException(status_code=500, detail="Could not create vector store")
|
|
|
|
| 115 |
except Exception as e:
|
| 116 |
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 117 |
|
| 118 |
+
@app.delete("/documents/{enterprise_id}/{filename_id}")
|
| 119 |
+
def delete_document(enterprise_id: str, filename_id: str):
|
| 120 |
+
try:
|
| 121 |
+
for ids in index.list(prefix=f"{filename_id}_", namespace=enterprise_id):
|
| 122 |
+
index.delete(ids=ids, namespace=enterprise_id)
|
| 123 |
+
return {"message": "Document deleted", "chunks_deleted": ids}
|
| 124 |
+
except Exception as e:
|
| 125 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
| 126 |
+
|
| 127 |
@app.delete("/documents/all/{enterprise_id}")
|
| 128 |
def delete_all_documents(enterprise_id: str):
|
| 129 |
try:
|
rag.py
CHANGED
|
@@ -7,6 +7,7 @@ from langchain_core.documents import Document
|
|
| 7 |
from langchain_openai import ChatOpenAI
|
| 8 |
from langchain_core.output_parsers import StrOutputParser
|
| 9 |
from langchain_core.prompts import PromptTemplate
|
|
|
|
| 10 |
|
| 11 |
import unicodedata
|
| 12 |
|
|
@@ -47,18 +48,20 @@ def get_vectorstore(text_chunks,filename, file_type,namespace,index):
|
|
| 47 |
uuids = []
|
| 48 |
|
| 49 |
for i, chunk in enumerate(text_chunks):
|
|
|
|
|
|
|
| 50 |
document = Document(
|
| 51 |
page_content=chunk,
|
| 52 |
-
metadata={"filename":filename,"file_type":file_type},
|
| 53 |
)
|
| 54 |
-
|
| 55 |
uuid = f"{clean_filename}_{i}"
|
| 56 |
uuids.append(uuid)
|
| 57 |
documents.append(document)
|
| 58 |
|
| 59 |
vector_store.add_documents(documents=documents, ids=uuids)
|
| 60 |
|
| 61 |
-
return
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
print(e)
|
|
|
|
| 7 |
from langchain_openai import ChatOpenAI
|
| 8 |
from langchain_core.output_parsers import StrOutputParser
|
| 9 |
from langchain_core.prompts import PromptTemplate
|
| 10 |
+
from uuid import uuid4
|
| 11 |
|
| 12 |
import unicodedata
|
| 13 |
|
|
|
|
| 48 |
uuids = []
|
| 49 |
|
| 50 |
for i, chunk in enumerate(text_chunks):
|
| 51 |
+
clean_filename = remove_non_standard_ascii(file_name)
|
| 52 |
+
|
| 53 |
document = Document(
|
| 54 |
page_content=chunk,
|
| 55 |
+
metadata={"filename":filename,"file_type":file_type, "filename_id":clean_filename},
|
| 56 |
)
|
| 57 |
+
|
| 58 |
uuid = f"{clean_filename}_{i}"
|
| 59 |
uuids.append(uuid)
|
| 60 |
documents.append(document)
|
| 61 |
|
| 62 |
vector_store.add_documents(documents=documents, ids=uuids)
|
| 63 |
|
| 64 |
+
return {"filename_id":clean_filename}
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
print(e)
|