fix(db): release connection before embedding in memory /query (#20579)
Remove Depends(get_session) from POST /query endpoint to prevent database connections from being held during embedding API calls (1-5+ seconds). The Memories.get_memories_by_user_id() function manages its own short-lived session internally, releasing the connection before the slow EMBEDDING_FUNCTION() call begins.
This commit is contained in:
@@ -122,8 +122,11 @@ async def query_memory(
|
|||||||
request: Request,
|
request: Request,
|
||||||
form_data: QueryMemoryForm,
|
form_data: QueryMemoryForm,
|
||||||
user=Depends(get_verified_user),
|
user=Depends(get_verified_user),
|
||||||
db: Session = Depends(get_session),
|
|
||||||
):
|
):
|
||||||
|
# NOTE: We intentionally do NOT use Depends(get_session) here.
|
||||||
|
# Database operations (get_memories_by_user_id) manage their own short-lived sessions.
|
||||||
|
# This prevents holding a connection during EMBEDDING_FUNCTION()
|
||||||
|
# which makes external embedding API calls (1-5+ seconds).
|
||||||
if not request.app.state.config.ENABLE_MEMORIES:
|
if not request.app.state.config.ENABLE_MEMORIES:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
@@ -138,7 +141,7 @@ async def query_memory(
|
|||||||
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
||||||
)
|
)
|
||||||
|
|
||||||
memories = Memories.get_memories_by_user_id(user.id, db=db)
|
memories = Memories.get_memories_by_user_id(user.id)
|
||||||
if not memories:
|
if not memories:
|
||||||
raise HTTPException(status_code=404, detail="No memories found for user")
|
raise HTTPException(status_code=404, detail="No memories found for user")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user