yangdx
commited on
Commit
·
ba7b5d4
1
Parent(s):
2549a80
Add clear cache API endpoint
Browse files
lightrag/api/routers/document_routes.py
CHANGED
@@ -66,6 +66,18 @@ class ClearDocumentsResponse(BaseModel):
|
|
66 |
message: str = Field(description="Message describing the operation result")
|
67 |
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
class DocStatusResponse(BaseModel):
|
70 |
@staticmethod
|
71 |
def format_datetime(dt: Any) -> Optional[str]:
|
@@ -1062,4 +1074,57 @@ def create_document_routes(
|
|
1062 |
logger.error(traceback.format_exc())
|
1063 |
raise HTTPException(status_code=500, detail=str(e))
|
1064 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1065 |
return router
|
|
|
66 |
message: str = Field(description="Message describing the operation result")
|
67 |
|
68 |
|
69 |
+
class ClearCacheRequest(BaseModel):
|
70 |
+
modes: Optional[List[str]] = Field(
|
71 |
+
default=None,
|
72 |
+
description="Modes of cache to clear. Options: ['default', 'naive', 'local', 'global', 'hybrid', 'mix']. If None, clears all cache.",
|
73 |
+
)
|
74 |
+
|
75 |
+
|
76 |
+
class ClearCacheResponse(BaseModel):
|
77 |
+
status: str = Field(description="Status of the clear operation: success/fail")
|
78 |
+
message: str = Field(description="Message describing the operation result")
|
79 |
+
|
80 |
+
|
81 |
class DocStatusResponse(BaseModel):
|
82 |
@staticmethod
|
83 |
def format_datetime(dt: Any) -> Optional[str]:
|
|
|
1074 |
logger.error(traceback.format_exc())
|
1075 |
raise HTTPException(status_code=500, detail=str(e))
|
1076 |
|
1077 |
+
@router.post(
|
1078 |
+
"/cache_clear",
|
1079 |
+
response_model=ClearCacheResponse,
|
1080 |
+
dependencies=[Depends(combined_auth)],
|
1081 |
+
)
|
1082 |
+
async def clear_cache(request: ClearCacheRequest):
|
1083 |
+
"""
|
1084 |
+
Clear cache data from the LLM response cache storage.
|
1085 |
+
|
1086 |
+
This endpoint allows clearing specific modes of cache or all cache if no modes are specified.
|
1087 |
+
Valid modes include: "default", "naive", "local", "global", "hybrid", "mix".
|
1088 |
+
- "default" represents extraction cache.
|
1089 |
+
- Other modes correspond to different query modes.
|
1090 |
+
|
1091 |
+
Args:
|
1092 |
+
request (ClearCacheRequest): The request body containing optional modes to clear.
|
1093 |
+
|
1094 |
+
Returns:
|
1095 |
+
ClearCacheResponse: A response object containing the status and message.
|
1096 |
+
|
1097 |
+
Raises:
|
1098 |
+
HTTPException: If an error occurs during cache clearing (400 for invalid modes, 500 for other errors).
|
1099 |
+
"""
|
1100 |
+
try:
|
1101 |
+
# Validate modes if provided
|
1102 |
+
valid_modes = ["default", "naive", "local", "global", "hybrid", "mix"]
|
1103 |
+
if request.modes and not all(mode in valid_modes for mode in request.modes):
|
1104 |
+
invalid_modes = [
|
1105 |
+
mode for mode in request.modes if mode not in valid_modes
|
1106 |
+
]
|
1107 |
+
raise HTTPException(
|
1108 |
+
status_code=400,
|
1109 |
+
detail=f"Invalid mode(s): {invalid_modes}. Valid modes are: {valid_modes}",
|
1110 |
+
)
|
1111 |
+
|
1112 |
+
# Call the aclear_cache method
|
1113 |
+
await rag.aclear_cache(request.modes)
|
1114 |
+
|
1115 |
+
# Prepare success message
|
1116 |
+
if request.modes:
|
1117 |
+
message = f"Successfully cleared cache for modes: {request.modes}"
|
1118 |
+
else:
|
1119 |
+
message = "Successfully cleared all cache"
|
1120 |
+
|
1121 |
+
return ClearCacheResponse(status="success", message=message)
|
1122 |
+
except HTTPException:
|
1123 |
+
# Re-raise HTTP exceptions
|
1124 |
+
raise
|
1125 |
+
except Exception as e:
|
1126 |
+
logger.error(f"Error clearing cache: {str(e)}")
|
1127 |
+
logger.error(traceback.format_exc())
|
1128 |
+
raise HTTPException(status_code=500, detail=str(e))
|
1129 |
+
|
1130 |
return router
|