Fix vector search parameter ordering for pgvector
%s placeholders in SQL are positional — SELECT score param must come before WHERE conditions in the params list, not after. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -442,17 +442,23 @@ def search(
|
|||||||
vec_params2.append(min_importance)
|
vec_params2.append(min_importance)
|
||||||
|
|
||||||
vec_where = " AND ".join(vec_conditions)
|
vec_where = " AND ".join(vec_conditions)
|
||||||
|
qv = np.array(query_emb)
|
||||||
|
# Pořadí %s musí odpovídat pořadí v SQL:
|
||||||
|
# 1. WHERE podmínky (vec_params2)
|
||||||
|
# 2. SELECT score: embedding <=> %s
|
||||||
|
# 3. ORDER BY: embedding <=> %s
|
||||||
|
# 4. LIMIT %s
|
||||||
vec_rows = conn.execute(
|
vec_rows = conn.execute(
|
||||||
f"""
|
f"""
|
||||||
SELECT id, mem_type, title, content, summary, tags,
|
SELECT id, mem_type, title, content, summary, tags,
|
||||||
project, source, session_id, importance, created_at,
|
project, source, session_id, importance, created_at,
|
||||||
1 - (embedding <=> %s::vector) AS score
|
1 - (embedding <=> %s) AS score
|
||||||
FROM kb_memories
|
FROM kb_memories
|
||||||
WHERE {vec_where}
|
WHERE {vec_where}
|
||||||
ORDER BY embedding <=> %s::vector
|
ORDER BY embedding <=> %s
|
||||||
LIMIT %s
|
LIMIT %s
|
||||||
""",
|
""",
|
||||||
[np.array(query_emb), np.array(query_emb)] + vec_params2 + [limit],
|
[qv] + vec_params2 + [qv, limit],
|
||||||
).fetchall()
|
).fetchall()
|
||||||
|
|
||||||
for r in vec_rows:
|
for r in vec_rows:
|
||||||
|
|||||||
Reference in New Issue
Block a user