fix(chatbot): row_factory=dict_row در sync_chunks و fetch_all_chunks

بدون آن، psycopg3 تاپل برمی‌گرداند و row["model"] خطای
«tuple indices must be integers or slices, not str» می‌داد؛ نتیجه:
«Initial index sync failed» و unavailable ماندن چت با وجود امبد موفق.
This commit is contained in:
aramesh-site deploy
2026-09-27 10:32:48 +00:00
parent ef619c635e
commit 275c3d6bb3
+8 -2
View File
@@ -77,6 +77,7 @@ def sync_chunks(chunks: list[dict], force: bool = False) -> dict:
خروجی: آمار عملیات برای لاگ.
"""
import psycopg
from psycopg.rows import dict_row
from psycopg.types.json import Jsonb
from .embeddings import get_embeddings
@@ -84,7 +85,10 @@ def sync_chunks(chunks: list[dict], force: bool = False) -> dict:
model = settings.embedding_model
wanted = {c["doc_id"]: c for c in chunks}
with psycopg.connect(chatbot_dsn(), autocommit=True, connect_timeout=10) as conn:
# row_factory لازم است: پایین‌تر row["model"]/row["doc_id"] خوانده می‌شود و
# پیش‌فرض psycopg3 تاپل برمی‌گرداند (خطای tuple indices must be integers).
with psycopg.connect(chatbot_dsn(), autocommit=True, connect_timeout=10,
row_factory=dict_row) as conn:
existing: dict[str, str] = {}
if not force:
for row in conn.execute("SELECT doc_id, content_hash, model FROM chatbot_chunks").fetchall():
@@ -136,8 +140,10 @@ def sync_chunks(chunks: list[dict], force: bool = False) -> dict:
def fetch_all_chunks() -> list[dict]:
"""همه قطعات ذخیره‌شده (برای ساخت BM25 در حافظه)."""
import psycopg
from psycopg.rows import dict_row
with psycopg.connect(chatbot_dsn(), autocommit=True, connect_timeout=10) as conn:
with psycopg.connect(chatbot_dsn(), autocommit=True, connect_timeout=10,
row_factory=dict_row) as conn:
rows = conn.execute(
"SELECT doc_id, content_hash, content FROM chatbot_chunks ORDER BY doc_id"
).fetchall()