1313
1414# Upload a text or files
1515@data_app .post (path = "/v1/data" )
16- async def add_data (request : DataIn ):
17- pl = ctx .get_pipeline_mgr ().get_active_pipeline ()
16+ async def add_data (request : DataIn , docs_name : str = None ):
17+ active_pl = ctx .get_pipeline_mgr ().get_active_pipeline ()
1818 docs = []
1919 if request .text is not None :
2020 docs .extend (ctx .get_file_mgr ().add_text (text = request .text ))
2121 if request .local_path is not None :
22- docs .extend (ctx .get_file_mgr ().add_files (docs = request .local_path ))
22+ docs .extend (ctx .get_file_mgr ().add_files (docs = request .local_path , docs_name = docs_name ))
2323
2424 nodelist = ctx .get_pipeline_mgr ().run_data_prepare (docs = docs )
25- if pl .indexer .comp_subtype != "kbadmin_indexer" :
25+ if active_pl .indexer .comp_subtype != "kbadmin_indexer" :
2626 if nodelist is None or len (nodelist ) == 0 :
2727 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "File not found" )
28- ctx .get_node_mgr ().add_nodes (pl .node_parser .idx , nodelist )
28+ ctx .get_node_mgr ().add_nodes (active_pl .node_parser .idx , nodelist )
2929 return "Done"
3030
3131
3232# Reindex all files
3333@data_app .post (path = "/v1/data/reindex" )
3434async def redindex_data ():
3535 pl = ctx .get_pipeline_mgr ().get_active_pipeline ()
36-
36+ kb = ctx .get_knowledge_mgr ().get_active_knowledge_base ()
37+ if kb :
38+ kb_name = kb .name
39+ docs_name = kb_name + pl .name + str (pl .indexer .d )
40+ else :
41+ kb_name = None
42+ docs_name = None
3743 ctx .get_node_mgr ().del_nodes_by_np_idx (pl .node_parser .idx )
38- pl .indexer .reinitialize_indexer ()
44+ pl .indexer .reinitialize_indexer (kb_name )
3945 pl .update_indexer_to_retriever ()
4046
41- all_docs = ctx .get_file_mgr ().get_all_docs ()
47+ all_docs = []
48+ docs_list = ctx .get_file_mgr ().get_kb_files_by_name (docs_name )
49+ for docs_file in docs_list :
50+ all_docs .extend (docs_file .documents )
4251 nodelist = ctx .get_pipeline_mgr ().run_data_prepare (docs = all_docs )
4352 if nodelist is not None and len (nodelist ) > 0 :
4453 ctx .get_node_mgr ().add_nodes (pl .node_parser .idx , nodelist )
45-
4654 return "Done"
4755
4856
4957# Upload files by a list of file_path
5058@data_app .post (path = "/v1/data/files" )
5159async def add_files (request : FilesIn ):
5260 docs = []
61+ pl = ctx .get_pipeline_mgr ().get_active_pipeline ()
62+ kb = ctx .get_knowledge_mgr ().get_active_knowledge_base ()
63+ docs_name = kb .name + pl .name + str (pl .indexer .d )
5364 if request .local_paths is not None :
54- docs .extend (ctx .get_file_mgr ().add_files (docs = request .local_paths ))
65+ docs .extend (ctx .get_file_mgr ().add_files (docs = request .local_path , kb_name = docs_name ))
5566
5667 nodelist = ctx .get_pipeline_mgr ().run_data_prepare (docs = docs )
5768 if nodelist is None or len (nodelist ) == 0 :
5869 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "File not found" )
59- pl = ctx .get_pipeline_mgr ().get_active_pipeline ()
6070 ctx .get_node_mgr ().add_nodes (pl .node_parser .idx , nodelist )
6171 return "Done"
6272
6373
6474# GET files
6575@data_app .get (path = "/v1/data/files" )
6676async def get_files ():
67- return ctx .get_file_mgr ().get_files ()
77+ return ctx .get_file_mgr ().get_all_docs ()
6878
6979
7080# GET a file
7181@data_app .get (path = "/v1/data/files/{name}" )
72- async def get_file_docs (name ):
73- return ctx .get_file_mgr ().get_file_by_name_or_id (name )
82+ async def get_kb_files_by_name (name ):
83+ return ctx .get_file_mgr ().get_kb_files_by_name (name )
7484
7585
7686# DELETE a file
7787@data_app .delete (path = "/v1/data/files/{name}" )
78- async def delete_file (name ):
79- if ctx .get_file_mgr ().del_file ( name ):
80- pl = ctx . get_pipeline_mgr (). get_active_pipeline ( )
81-
88+ async def delete_file (kb_name , file_path ):
89+ pl = ctx .get_pipeline_mgr ().get_active_pipeline ()
90+ docs_name = kb_name + pl . name + str ( pl . indexer . d )
91+ if ctx . get_file_mgr (). del_file ( docs_name , file_path ):
8292 # Current solution: reindexing all docs after deleting one file
8393 # TODO: delete the nodes related to the file
8494 ctx .get_node_mgr ().del_nodes_by_np_idx (pl .node_parser .idx )
85- pl .indexer .reinitialize_indexer ()
95+ pl .indexer .reinitialize_indexer (kb_name )
8696 pl .update_indexer_to_retriever ()
87-
88- all_docs = ctx .get_file_mgr ().get_all_docs ()
97+ all_docs = ctx .get_file_mgr ().get_file_by_name (docs_name )
8998 nodelist = ctx .get_pipeline_mgr ().run_data_prepare (docs = all_docs )
9099 if nodelist is not None and len (nodelist ) > 0 :
91100 ctx .get_node_mgr ().add_nodes (pl .node_parser .idx , nodelist )
92101
102+ return f"File is deleted"
103+ else :
104+ return f"File not found"
105+
106+ # DELETE a file
107+ @data_app .delete (path = "/v1/data/all_files/{name}" )
108+ async def delete_all_file (name ):
109+ if ctx .get_file_mgr ().del_kb_file (name ):
110+ pl = ctx .get_pipeline_mgr ().get_active_pipeline ()
111+
112+ # Current solution: reindexing all docs after deleting one file
113+ # TODO: delete the nodes related to the file
114+ ctx .get_node_mgr ().del_nodes_by_np_idx (pl .node_parser .idx )
115+ pl .indexer .reinitialize_indexer ()
116+ pl .update_indexer_to_retriever ()
93117 return f"File { name } is deleted"
94118 else :
95119 return f"File { name } not found"
96120
97-
98121# Upload & save a file from UI
99122@data_app .post (path = "/v1/data/file/{file_name}" )
100123async def upload_file (file_name : str , file : UploadFile = File (...)):
@@ -122,4 +145,4 @@ async def upload_file(file_name: str, file: UploadFile = File(...)):
122145 except Exception as e :
123146 raise HTTPException (
124147 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = f"Failed to upload file: { str (e )} "
125- )
148+ )
0 commit comments