keep ingested chunks findable and structured
Two bugs made self-ingested content useless, both silently: - _payload wrote `chapter_number` (int) while math retrieval filters on `chapter` (the full "فصل ۲: کسر" string), so anything we ingest could never match a search — no error, just permanently invisible content. - ingest_pdf flattened the vision extraction into one text blob and threw away type/keywords/generalSolution/problem/solution. LessonChunk is the source of truth, so rebuilding meant paying for vision again — and the problem/solution pairs are the input for building lesson questions. LessonChunk now stores those fields and _payload emits them under the same keys the other team's collections use, so ours and theirs stay one shape. Also fixes a regression from the whitenoise commit: the Manifest storage variant raises when staticfiles.json is absent, which broke all 23 admin rendering tests (Django forces DEBUG=False in tests, so USE_FINDERS is off) and would have taken the admin panel down entirely the day DEBUG=0 ships without a collectstatic run. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,7 @@ from ..models import LessonChunk
|
||||
from .chunking import chunk_and_summarize
|
||||
from .config import build_collection_name, resolve_grade_title
|
||||
from .embedding import embed_text
|
||||
from .known_collections import is_math_subject, resolve_math_chapter
|
||||
from . import pdf as pdf_service
|
||||
from .qdrant_store import ensure_collection, upsert_points
|
||||
|
||||
@@ -22,7 +23,16 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _payload(chunk: LessonChunk) -> dict:
|
||||
return {
|
||||
"""
|
||||
payloadِ نقطهی Qdrant.
|
||||
|
||||
⚠️ کلیدِ `chapter` (رشتهی کاملِ فصل) حتماً باید نوشته بشه: جستجوی ریاضی در
|
||||
retrieval.py دقیقاً روی همین کلید فیلتر میکنه (نه `chapter_number`)، پس
|
||||
بدونش هر چیزی که ingest کنیم برای همیشه نامرئی میمونه — بدونِ هیچ خطایی.
|
||||
بقیهی کلیدهای ساختاری هم عمداً همنامِ قراردادِ تیمِ دیگهان تا collectionِ
|
||||
ما و اونا یک شکل بمونن.
|
||||
"""
|
||||
payload = {
|
||||
'lesson_id': chunk.lesson_id,
|
||||
'lesson_number': chunk.lesson_number,
|
||||
'chapter_number': chunk.chapter,
|
||||
@@ -30,6 +40,18 @@ def _payload(chunk: LessonChunk) -> dict:
|
||||
'lesson_title': chunk.title,
|
||||
'summary': chunk.summary,
|
||||
}
|
||||
for key, value in (
|
||||
('chapter', chunk.chapter_title),
|
||||
('grade', chunk.grade_title),
|
||||
('type', chunk.problem_type),
|
||||
('keywords', chunk.keywords),
|
||||
('generalSolution', chunk.general_solution),
|
||||
('problem', chunk.problem),
|
||||
('solution', chunk.solution),
|
||||
):
|
||||
if value:
|
||||
payload[key] = value
|
||||
return payload
|
||||
|
||||
|
||||
def _persist_and_upsert(rows: list[dict], *, lesson_id, grade_id, subject,
|
||||
@@ -53,6 +75,9 @@ def _persist_and_upsert(rows: list[dict], *, lesson_id, grade_id, subject,
|
||||
summary=row['summary'],
|
||||
embedding_vector=vector,
|
||||
collection=collection,
|
||||
# فیلدهای ساختاری فقط از مسیرِ PDF/vision میآن؛ ingest_text
|
||||
# اونها رو نداره و خالی میمونن (payload هم اونوقت نمینویسدشون).
|
||||
**row.get('structured', {}),
|
||||
)
|
||||
created.append(chunk)
|
||||
points.append({'id': chunk.qdrant_point_id, 'vector': vector, 'payload': _payload(chunk)})
|
||||
@@ -71,6 +96,15 @@ def ingest_text(*, lesson_id: int, grade_id, subject: str, lesson_number: int,
|
||||
if not rows:
|
||||
return {'status': 'empty', 'chunks_created': 0, 'collection': collection}
|
||||
|
||||
# برای ریاضی، فیلترِ جستجو رشتهی کاملِ فصله؛ از شمارهی فصل درش میآریم تا
|
||||
# محتوایی که اینطوری ingest میشه هم قابلِ پیدا شدن باشه. اگه فصل ناشناخته
|
||||
# باشه (پایهای که هنوز در known_collections نیست) خالی میمونه.
|
||||
structured = {'grade_title': grade_title}
|
||||
if is_math_subject(subject) and chapter is not None:
|
||||
structured['chapter_title'] = resolve_math_chapter(subject, grade_title, chapter) or ''
|
||||
for row in rows:
|
||||
row.setdefault('structured', structured)
|
||||
|
||||
created = _persist_and_upsert(
|
||||
rows, lesson_id=lesson_id, grade_id=grade_id, subject=subject,
|
||||
lesson_number=lesson_number, chapter=chapter, title=title, collection=collection,
|
||||
@@ -111,12 +145,31 @@ def ingest_pdf(*, lesson_id: int, grade_id, subject: str, lesson_number: int,
|
||||
except Exception:
|
||||
logger.exception('rag.ingest_pdf: تحلیلِ یک صفحه شکست خورد؛ رد شد')
|
||||
continue
|
||||
page_chapter = (parsed.get('chapter') or '').strip() or chapter_name
|
||||
page_grade = (parsed.get('grade') or '').strip() or (detected_grade or grade_title)
|
||||
for problem_type in parsed.get('problemTypes', []) or []:
|
||||
text = pdf_service.problem_type_to_text(problem_type)
|
||||
if not text:
|
||||
continue
|
||||
order += 1
|
||||
rows.append({'order': order, 'original_text': text, 'summary': text})
|
||||
example = problem_type.get('exampleFromImage', {}) or {}
|
||||
rows.append({
|
||||
'order': order,
|
||||
'original_text': text,
|
||||
'summary': text,
|
||||
# ساختار رو نگه میداریم، نه فقط متنِ صافشدهاش را: هم
|
||||
# `chapter` برای فیلترِ جستجو لازمه، هم problem/solution
|
||||
# بعداً ورودیِ ساختِ سؤالهای چهارگزینهای میشن.
|
||||
'structured': {
|
||||
'chapter_title': page_chapter,
|
||||
'grade_title': page_grade,
|
||||
'problem_type': (problem_type.get('type') or '').strip(),
|
||||
'keywords': problem_type.get('keywords') or [],
|
||||
'general_solution': (problem_type.get('generalSolution') or '').strip(),
|
||||
'problem': (example.get('problem') or '').strip(),
|
||||
'solution': (example.get('solution') or '').strip(),
|
||||
},
|
||||
})
|
||||
finally:
|
||||
try:
|
||||
os.remove(tmp_path)
|
||||
|
||||
Reference in New Issue
Block a user