fix(db): release connection before embedding in memory /add (#20578)
Remove Depends(get_session) from POST /add endpoint to prevent database connections from being held during embedding API calls (1-5+ seconds). The Memories.insert_new_memory() function manages its own short-lived session internally, releasing the connection before the slow EMBEDDING_FUNCTION() call begins.
This commit is contained in:
@@ -69,8 +69,11 @@ async def add_memory(
|
|||||||
request: Request,
|
request: Request,
|
||||||
form_data: AddMemoryForm,
|
form_data: AddMemoryForm,
|
||||||
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 (insert_new_memory) 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,
|
||||||
@@ -85,7 +88,7 @@ async def add_memory(
|
|||||||
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
||||||
)
|
)
|
||||||
|
|
||||||
memory = Memories.insert_new_memory(user.id, form_data.content, db=db)
|
memory = Memories.insert_new_memory(user.id, form_data.content)
|
||||||
|
|
||||||
vector = await request.app.state.EMBEDDING_FUNCTION(memory.content, user=user)
|
vector = await request.app.state.EMBEDDING_FUNCTION(memory.content, user=user)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user