diff --git a/chatbot/vectorstore.py b/chatbot/vectorstore.py index 79b933c..5094f79 100644 --- a/chatbot/vectorstore.py +++ b/chatbot/vectorstore.py @@ -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()