1- from fastapi import FastAPI , HTTPException ,status
2- from edgecraftrag .context import ctx
1+ # Copyright (C) 2025 Intel Corporation
2+ # SPDX-License-Identifier: Apache-2.0
3+
4+ import os
5+
36from edgecraftrag .api .v1 .data import add_data
47from edgecraftrag .api_schema import DataIn , KnowledgeBaseCreateIn
5- import os
8+ from edgecraftrag .context import ctx
9+ from fastapi import FastAPI , HTTPException , status
610
711kb_app = FastAPI ()
812
@@ -23,11 +27,11 @@ async def get_knowledge_base(knowledge_name: str):
2327 return kb
2428
2529
26- # Create a new knowledge base
30+ # Create a new knowledge base
2731@kb_app .post (path = "/v1/knowledge" )
2832async def create_knowledge_base (knowledge : KnowledgeBaseCreateIn ):
2933 try :
30- kb = ctx .knowledgemgr .create_knowledge_base (knowledge )
34+ kb = ctx .knowledgemgr .create_knowledge_base (knowledge )
3135 if kb .active :
3236 await update_knowledge_base_handler (kb .get_file_paths ())
3337 return "Create knowledge base successfully"
@@ -60,48 +64,48 @@ async def update_knowledge_base(knowledge: KnowledgeBaseCreateIn):
6064
6165# Add a files to the knowledge base
6266@kb_app .post (path = "/v1/knowledge/{knowledge_name}/files" )
63- async def add_file_to_knowledge_base (knowledge_name , file_path : DataIn ):
67+ async def add_file_to_knowledge_base (knowledge_name , file_path : DataIn ):
6468 try :
6569 kb = ctx .knowledgemgr .get_knowledge_base_by_name_or_id (knowledge_name )
66- if os .path .isdir (file_path .local_path ) :
70+ if os .path .isdir (file_path .local_path ):
6771 for root , _ , files in os .walk (file_path .local_path ):
6872 for file in files :
6973 file_full_path = os .path .join (root , file )
7074 if file_full_path not in kb .get_file_paths ():
7175 kb .add_file_path (file_full_path )
7276 else :
7377 raise HTTPException (status_code = status .HTTP_409_CONFLICT , detail = "File upload failed" )
74- elif os .path .isfile (file_path .local_path ) and file_path .local_path not in kb .get_file_paths ():
78+ elif os .path .isfile (file_path .local_path ) and file_path .local_path not in kb .get_file_paths ():
7579 kb .add_file_path (file_path .local_path )
7680 else :
7781 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "File upload failed" )
78-
82+
7983 active_kb = ctx .knowledgemgr .get_active_knowledge_base ()
8084 if active_kb :
81- if active_kb .name == knowledge_name or active_kb .idx == knowledge_name :
85+ if active_kb .name == knowledge_name or active_kb .idx == knowledge_name :
8286 await update_knowledge_base_handler (file_path , add_file = True )
8387
84- return "File upload successfully"
88+ return "File upload successfully"
8589 except ValueError as e :
8690 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = str (e ))
8791
8892
8993# Remove a file from the knowledge base
9094@kb_app .delete (path = "/v1/knowledge/{knowledge_name}/files" )
91- async def remove_file_from_knowledge_base (knowledge_name , file_path : DataIn ):
95+ async def remove_file_from_knowledge_base (knowledge_name , file_path : DataIn ):
9296 try :
9397 kb = ctx .knowledgemgr .get_knowledge_base_by_name_or_id (knowledge_name )
94- if file_path .local_path in kb .get_file_paths ():
98+ if file_path .local_path in kb .get_file_paths ():
9599 kb .remove_file_path (file_path .local_path )
96100 else :
97101 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "File remove failure" )
98102
99103 file_path = kb .get_file_paths ()
100104 active_kb = ctx .knowledgemgr .get_active_knowledge_base ()
101105 if active_kb :
102- if active_kb .name == knowledge_name or active_kb .idx == knowledge_name :
106+ if active_kb .name == knowledge_name or active_kb .idx == knowledge_name :
103107 await update_knowledge_base_handler (file_path )
104- return f "File deleted successfully"
108+ return "File deleted successfully"
105109 except ValueError as e :
106110 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = str (e ))
107111
@@ -110,10 +114,10 @@ async def remove_file_from_knowledge_base(knowledge_name ,file_path: DataIn):
110114async def update_knowledge_base_handler (file_path = None , add_file : bool = False ):
111115 if ctx .get_pipeline_mgr ().get_active_pipeline () is None :
112116 raise HTTPException (status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "Please activate pipeline" )
113-
117+
114118 if add_file and file_path :
115119 return await add_data (file_path )
116-
120+
117121 elif file_path :
118122 pl = ctx .get_pipeline_mgr ().get_active_pipeline ()
119123 ctx .get_node_mgr ().del_nodes_by_np_idx (pl .node_parser .idx )
@@ -123,10 +127,10 @@ async def update_knowledge_base_handler(file_path=None, add_file: bool = False):
123127 request = DataIn (local_path = file )
124128 await add_data (request )
125129 return "Done"
126-
130+
127131 else :
128132 pl = ctx .get_pipeline_mgr ().get_active_pipeline ()
129133 ctx .get_node_mgr ().del_nodes_by_np_idx (pl .node_parser .idx )
130134 pl .indexer .reinitialize_indexer ()
131135 pl .update_indexer_to_retriever ()
132- return "Done"
136+ return "Done"
0 commit comments