fix: RAG vector search SQL syntax error breaking assistant chat
- Use CAST(:embedding AS vector) instead of :embedding::vector to avoid SQLAlchemy named param conflict with PostgreSQL :: cast syntax - Add db.rollback() before recording AI usage on failure to prevent InFailedSQLTransactionError cascade Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -155,6 +155,7 @@ async def post_message(
|
|||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception("Assistant chat message failed: %s", e)
|
logger.exception("Assistant chat message failed: %s", e)
|
||||||
|
await db.rollback()
|
||||||
await record_ai_usage(
|
await record_ai_usage(
|
||||||
user_id=current_user.id,
|
user_id=current_user.id,
|
||||||
account_id=current_user.account_id,
|
account_id=current_user.account_id,
|
||||||
|
|||||||
@@ -143,13 +143,13 @@ async def search(
|
|||||||
te.chunk_text,
|
te.chunk_text,
|
||||||
te.chunk_type,
|
te.chunk_type,
|
||||||
te.node_id,
|
te.node_id,
|
||||||
1 - (te.embedding <=> :embedding::vector) as similarity
|
1 - (te.embedding <=> CAST(:embedding AS vector)) as similarity
|
||||||
FROM tree_embeddings te
|
FROM tree_embeddings te
|
||||||
JOIN trees t ON t.id = te.tree_id
|
JOIN trees t ON t.id = te.tree_id
|
||||||
WHERE te.account_id = :account_id
|
WHERE te.account_id = :account_id
|
||||||
AND t.deleted_at IS NULL
|
AND t.deleted_at IS NULL
|
||||||
{exclude_clause}
|
{exclude_clause}
|
||||||
ORDER BY te.embedding <=> :embedding::vector
|
ORDER BY te.embedding <=> CAST(:embedding AS vector)
|
||||||
LIMIT :limit
|
LIMIT :limit
|
||||||
"""),
|
"""),
|
||||||
params,
|
params,
|
||||||
|
|||||||
Reference in New Issue
Block a user