|
5 | 5 | from fastapi import APIRouter, Body, Depends, Header, HTTPException, Path, Query |
6 | 6 | from fastapi.responses import JSONResponse |
7 | 7 |
|
8 | | -from consts.model import IndexingResponse |
| 8 | +from consts.model import ChunkCreateRequest, ChunkUpdateRequest, HybridSearchRequest, IndexingResponse |
9 | 9 | from nexent.vector_database.base import VectorDatabaseCore |
10 | 10 | from services.vectordatabase_service import ( |
11 | 11 | ElasticSearchService, |
@@ -226,3 +226,125 @@ def get_index_chunks( |
226 | 226 | f"Error getting chunks for index '{index_name}': {error_msg}") |
227 | 227 | raise HTTPException( |
228 | 228 | status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=f"Error getting chunks: {error_msg}") |
| 229 | + |
| 230 | + |
| 231 | +@router.post("/{index_name}/chunk") |
| 232 | +def create_chunk( |
| 233 | + index_name: str = Path(..., description="Name of the index"), |
| 234 | + payload: ChunkCreateRequest = Body(..., description="Chunk data"), |
| 235 | + vdb_core: VectorDatabaseCore = Depends(get_vector_db_core), |
| 236 | + authorization: Optional[str] = Header(None), |
| 237 | +): |
| 238 | + """Create a manual chunk.""" |
| 239 | + try: |
| 240 | + user_id, _ = get_current_user_id(authorization) |
| 241 | + result = ElasticSearchService.create_chunk( |
| 242 | + index_name=index_name, |
| 243 | + chunk_request=payload, |
| 244 | + vdb_core=vdb_core, |
| 245 | + user_id=user_id, |
| 246 | + ) |
| 247 | + return JSONResponse(status_code=HTTPStatus.OK, content=result) |
| 248 | + except Exception as exc: |
| 249 | + logger.error( |
| 250 | + "Error creating chunk for index %s: %s", index_name, exc, exc_info=True |
| 251 | + ) |
| 252 | + raise HTTPException( |
| 253 | + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc) |
| 254 | + ) |
| 255 | + |
| 256 | + |
| 257 | +@router.put("/{index_name}/chunk/{chunk_id}") |
| 258 | +def update_chunk( |
| 259 | + index_name: str = Path(..., description="Name of the index"), |
| 260 | + chunk_id: str = Path(..., description="Chunk identifier"), |
| 261 | + payload: ChunkUpdateRequest = Body(..., |
| 262 | + description="Chunk update payload"), |
| 263 | + vdb_core: VectorDatabaseCore = Depends(get_vector_db_core), |
| 264 | + authorization: Optional[str] = Header(None), |
| 265 | +): |
| 266 | + """Update an existing chunk.""" |
| 267 | + try: |
| 268 | + user_id, _ = get_current_user_id(authorization) |
| 269 | + result = ElasticSearchService.update_chunk( |
| 270 | + index_name=index_name, |
| 271 | + chunk_id=chunk_id, |
| 272 | + chunk_request=payload, |
| 273 | + vdb_core=vdb_core, |
| 274 | + user_id=user_id, |
| 275 | + ) |
| 276 | + return JSONResponse(status_code=HTTPStatus.OK, content=result) |
| 277 | + except ValueError as exc: |
| 278 | + raise HTTPException( |
| 279 | + status_code=HTTPStatus.BAD_REQUEST, detail=str(exc)) |
| 280 | + except Exception as exc: |
| 281 | + logger.error( |
| 282 | + "Error updating chunk %s for index %s: %s", |
| 283 | + chunk_id, |
| 284 | + index_name, |
| 285 | + exc, |
| 286 | + exc_info=True, |
| 287 | + ) |
| 288 | + raise HTTPException( |
| 289 | + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc) |
| 290 | + ) |
| 291 | + |
| 292 | + |
| 293 | +@router.delete("/{index_name}/chunk/{chunk_id}") |
| 294 | +def delete_chunk( |
| 295 | + index_name: str = Path(..., description="Name of the index"), |
| 296 | + chunk_id: str = Path(..., description="Chunk identifier"), |
| 297 | + vdb_core: VectorDatabaseCore = Depends(get_vector_db_core), |
| 298 | + authorization: Optional[str] = Header(None), |
| 299 | +): |
| 300 | + """Delete a chunk.""" |
| 301 | + try: |
| 302 | + get_current_user_id(authorization) |
| 303 | + result = ElasticSearchService.delete_chunk( |
| 304 | + index_name=index_name, |
| 305 | + chunk_id=chunk_id, |
| 306 | + vdb_core=vdb_core, |
| 307 | + ) |
| 308 | + return JSONResponse(status_code=HTTPStatus.OK, content=result) |
| 309 | + except ValueError as exc: |
| 310 | + raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail=str(exc)) |
| 311 | + except Exception as exc: |
| 312 | + logger.error( |
| 313 | + "Error deleting chunk %s for index %s: %s", |
| 314 | + chunk_id, |
| 315 | + index_name, |
| 316 | + exc, |
| 317 | + exc_info=True, |
| 318 | + ) |
| 319 | + raise HTTPException( |
| 320 | + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc) |
| 321 | + ) |
| 322 | + |
| 323 | + |
| 324 | +@router.post("/search/hybrid") |
| 325 | +async def hybrid_search( |
| 326 | + payload: HybridSearchRequest, |
| 327 | + vdb_core: VectorDatabaseCore = Depends(get_vector_db_core), |
| 328 | + authorization: Optional[str] = Header(None), |
| 329 | +): |
| 330 | + """Run a hybrid (accurate + semantic) search across indices.""" |
| 331 | + try: |
| 332 | + _, tenant_id = get_current_user_id(authorization) |
| 333 | + result = ElasticSearchService.search_hybrid( |
| 334 | + index_names=payload.index_names, |
| 335 | + query=payload.query, |
| 336 | + tenant_id=tenant_id, |
| 337 | + top_k=payload.top_k, |
| 338 | + weight_accurate=payload.weight_accurate, |
| 339 | + vdb_core=vdb_core, |
| 340 | + ) |
| 341 | + return JSONResponse(status_code=HTTPStatus.OK, content=result) |
| 342 | + except ValueError as exc: |
| 343 | + raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, |
| 344 | + detail=str(exc)) |
| 345 | + except Exception as exc: |
| 346 | + logger.error(f"Hybrid search failed: {exc}", exc_info=True) |
| 347 | + raise HTTPException( |
| 348 | + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 349 | + detail=f"Error executing hybrid search: {str(exc)}", |
| 350 | + ) |
0 commit comments