fix(knowledge): asyncpg 不支援 :param::type,改用 CAST(:param AS vector)
Some checks failed
CD Pipeline / build-and-deploy (push) Has been cancelled
Some checks failed
CD Pipeline / build-and-deploy (push) Has been cancelled
asyncpg 使用 $1 位置參數,:emb::vector 語法導致 PostgresSyntaxError。 save_embedding 和 semantic_search 均改用 CAST(:emb AS vector) 語法。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -196,12 +196,14 @@ class KnowledgeDBRepository:
|
||||
return [(row.id, row.title, row.content) for row in result.fetchall()]
|
||||
|
||||
async def save_embedding(self, entry_id: str, embedding: list[float]) -> bool:
|
||||
"""儲存向量 embedding (768 維)"""
|
||||
# 直接用 raw SQL 寫入 pgvector 欄位
|
||||
"""儲存向量 embedding (768 維)
|
||||
|
||||
注意: asyncpg 不支援 :param::type 語法,必須用 CAST(:param AS vector)
|
||||
"""
|
||||
from sqlalchemy import text as sa_text
|
||||
result = await self.db.execute(
|
||||
sa_text(
|
||||
"UPDATE knowledge_entries SET embedding = :emb::vector WHERE id = :id"
|
||||
"UPDATE knowledge_entries SET embedding = CAST(:emb AS vector) WHERE id = :id"
|
||||
),
|
||||
{"emb": str(embedding), "id": entry_id},
|
||||
)
|
||||
@@ -221,12 +223,12 @@ class KnowledgeDBRepository:
|
||||
"""
|
||||
from sqlalchemy import text as sa_text
|
||||
sql = sa_text("""
|
||||
SELECT id, 1 - (embedding <=> :emb::vector) AS score
|
||||
SELECT id, 1 - (embedding <=> CAST(:emb AS vector)) AS score
|
||||
FROM knowledge_entries
|
||||
WHERE status != 'ARCHIVED'
|
||||
AND embedding IS NOT NULL
|
||||
AND 1 - (embedding <=> :emb::vector) >= :threshold
|
||||
ORDER BY embedding <=> :emb::vector
|
||||
AND 1 - (embedding <=> CAST(:emb AS vector)) >= :threshold
|
||||
ORDER BY embedding <=> CAST(:emb AS vector)
|
||||
LIMIT :limit
|
||||
""")
|
||||
rows = await self.db.execute(
|
||||
|
||||
Reference in New Issue
Block a user