@@ -139,6 +139,15 @@ class FAISSManager:
139139 def __init__ (self , index_path : str , embeddings ):
140140 self .index_path = index_path
141141 self .embeddings = embeddings
142+ self ._ensure_index_directory_exists ()
143+
144+ def _ensure_index_directory_exists (self ):
145+ """Ensure the directory for the FAISS index exists."""
146+ if not os .path .exists (self .index_path ):
147+ os .makedirs (self .index_path )
148+ logger .info (f"Created directory for FAISS index at { self .index_path } ." )
149+ else :
150+ logger .info (f"FAISS index directory already exists at { self .index_path } ." )
142151
143152 def create_and_save_vector_store (self , chunked_documents : List [str ]):
144153 try :
@@ -159,7 +168,7 @@ def load_vector_store(self):
159168 except Exception as e :
160169 logger .error (f"Error loading FAISS vector store: { e } " , exc_info = True )
161170 raise HTTPException (status_code = 500 , detail = "Error loading FAISS vector store" )
162-
171+
163172# LLM Service
164173class LLMService :
165174 def __init__ (self , model_id : str , client ):
@@ -218,6 +227,33 @@ async def general_exception_handler(request: Request, exc: Exception):
218227 logger .error (f"An unexpected error occurred: { exc } " , exc_info = True )
219228 return JSONResponse (status_code = 500 , content = {"detail" : "An unexpected error occurred" })
220229
230+ # Endpoint to create FAISS index from PDF documents
231+ @app .post ("/create_index" )
232+ async def create_index ():
233+ try :
234+ logger .info ("Creating FAISS index..." )
235+
236+ # Load and chunk PDF documents
237+ processor = PDFDocumentProcessor (data_directory = DATA_DIRECTORY )
238+ chunked_documents = processor .load_and_chunk_documents ()
239+
240+ # Initialize AWS client
241+ bedrock_client = boto3 .client (
242+ service_name = "bedrock-runtime" ,
243+ aws_access_key_id = os .getenv ("AWS_ACCESS_KEY_ID" ),
244+ aws_secret_access_key = os .getenv ("AWS_SECRET_ACCESS_KEY" ),
245+ region_name = os .getenv ("AWS_DEFAULT_REGION" ),
246+ )
247+
248+ # Create and save FAISS index
249+ faiss_manager = FAISSManager (FAISS_INDEX_PATH , BedrockEmbeddings (model_id = TITAN_MODEL_ID , client = bedrock_client ))
250+ faiss_manager .create_and_save_vector_store (chunked_documents )
251+
252+ return {"detail" : "FAISS index created successfully" }
253+ except Exception as e :
254+ logger .error (f"Error in /create_index endpoint: { e } " , exc_info = True )
255+ raise HTTPException (status_code = 500 , detail = "Error creating FAISS index" )
256+
221257# Endpoint to ask a question
222258@app .post ("/ask" )
223259async def ask_question (request : QuestionRequest ):
0 commit comments