pvanand commited on
Commit
34c01cb
·
verified ·
1 Parent(s): 0f54295

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +60 -0
main.py CHANGED
@@ -230,6 +230,66 @@ async def chat(request: ChatRequest, background_tasks: BackgroundTasks, api_key:
230
  logger.error(f"Error in chat endpoint: {str(e)}")
231
  raise HTTPException(status_code=500, detail=f"Error in chat endpoint: {str(e)}")
232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
 
234
 
235
  @app.on_event("startup")
 
230
  logger.error(f"Error in chat endpoint: {str(e)}")
231
  raise HTTPException(status_code=500, detail=f"Error in chat endpoint: {str(e)}")
232
 
233
+ from typing import Dict, List
234
+ import os
235
+ import json
236
+
237
+ class IndexMetadata(BaseModel):
238
+ index_id: str
239
+ document_count: int
240
+ created_at: str = Field(default_factory=lambda: datetime.now().isoformat())
241
+ size_bytes: int
242
+
243
+ @app.get("/list_indexes/", response_model=Dict[str, List[IndexMetadata]], tags=["Index Operations"])
244
+ async def list_indexes():
245
+ """
246
+ List all available indexes and their metadata.
247
+
248
+ Returns a dictionary containing:
249
+ - List of indexes with their metadata (document count, creation date, size)
250
+ """
251
+ try:
252
+ indexes_path = "/app/indexes"
253
+ if not os.path.exists(indexes_path):
254
+ return {"indexes": []}
255
+
256
+ indexes = []
257
+ for index_id in os.listdir(indexes_path):
258
+ index_path = os.path.join(indexes_path, index_id)
259
+ if os.path.isdir(index_path):
260
+ try:
261
+ # Get document count from document_list.json
262
+ doc_list_path = os.path.join(index_path, "document_list.json")
263
+ with open(doc_list_path, "r") as f:
264
+ documents = json.load(f)
265
+ doc_count = len(documents)
266
+
267
+ # Calculate total size of the index
268
+ total_size = 0
269
+ for root, dirs, files in os.walk(index_path):
270
+ total_size += sum(os.path.getsize(os.path.join(root, file)) for file in files)
271
+
272
+ # Get creation time of the index directory
273
+ created_at = datetime.fromtimestamp(os.path.getctime(index_path)).isoformat()
274
+
275
+ indexes.append(IndexMetadata(
276
+ index_id=index_id,
277
+ document_count=doc_count,
278
+ created_at=created_at,
279
+ size_bytes=total_size
280
+ ))
281
+
282
+ except Exception as e:
283
+ logger.error(f"Error processing index {index_id}: {str(e)}")
284
+ continue
285
+
286
+ logger.info(f"Successfully retrieved metadata for {len(indexes)} indexes")
287
+ return {"indexes": indexes}
288
+
289
+ except Exception as e:
290
+ logger.error(f"Error listing indexes: {str(e)}")
291
+ raise HTTPException(status_code=500, detail=f"Error listing indexes: {str(e)}")
292
+
293
 
294
 
295
  @app.on_event("startup")