This commit is contained in:
@@ -62,6 +62,41 @@ def enqueue_insight_embedding(insight_id: int, insight_type: str, content: str,
|
||||
return _enqueue_embedding("ai_insights", int(insight_id), embed_target_text)
|
||||
|
||||
|
||||
def enqueue_missing_insight_embeddings(limit: int = 200) -> dict:
|
||||
"""Backfill existing ai_insights that have not yet entered the embedding queue."""
|
||||
session = get_session()
|
||||
try:
|
||||
rows = session.execute(
|
||||
text("""
|
||||
SELECT id, insight_type, period, content
|
||||
FROM ai_insights i
|
||||
WHERE i.embedding IS NULL
|
||||
AND COALESCE(i.status, 'approved') NOT IN ('archived', 'rejected')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM embedding_retry_queue q
|
||||
WHERE q.target_table = 'ai_insights'
|
||||
AND q.target_id = i.id
|
||||
AND q.status IN ('pending', 'processing', 'done')
|
||||
)
|
||||
ORDER BY i.created_at DESC
|
||||
LIMIT :lim
|
||||
"""),
|
||||
{"lim": limit},
|
||||
).fetchall()
|
||||
except Exception as exc:
|
||||
sys_log.warning(f"[OCLearn] embedding backfill 略過 (schema/pgvector 未就緒?): {exc}")
|
||||
return {"scanned": 0, "enqueued": 0, "status": "skipped", "error": str(exc)[:200]}
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
enqueued = 0
|
||||
for row in rows:
|
||||
if enqueue_insight_embedding(row.id, row.insight_type, row.content, row.period):
|
||||
enqueued += 1
|
||||
return {"scanned": len(rows), "enqueued": enqueued, "status": "ok"}
|
||||
|
||||
|
||||
def _process_one_embedding(row_id: int, target_table: str, target_id: int,
|
||||
text_content: str, model: str) -> bool:
|
||||
"""處理單筆 embedding,成功寫回目標表,失敗累加 attempts"""
|
||||
|
||||
Reference in New Issue
Block a user