creator_rag / app /rag /quant.py
xPushkal's picture
feat: implement creator-rag MVP — router-based RAG over two short-form videos
a8b0be6
Raw History Blame Contribute Delete
1.79 kB
"""Quantitative path: numbers from the database, computed in Python.
Never touches the LLM or the vector store. Engagement rate is computed with the
same deterministic helper used at ingest time, so the answer is always
arithmetically correct (NFR-1, NFR-3).
"""
from __future__ import annotations
from sqlalchemy.orm import Session
from app.models.video import Video, compute_engagement_rate
def build_metrics_context(session: Session) -> str:
"""A compact, factual block of both videos' metrics for the synthesizer.
This is a deterministic data dump — the LLM formats it into prose but the
numbers themselves are never generated by the model.
"""
videos = session.query(Video).order_by(Video.video_id).all()
if not videos:
return "No videos have been ingested yet."
lines: list[str] = []
for v in videos:
er = compute_engagement_rate(v.likes, v.comments, v.views)
er_str = f"{er:.4f}%" if er is not None else "n/a (views unknown)"
lines.append(
f"Video {v.video_id} ({v.source}) — creator: {v.creator or 'n/a'}, "
f"followers: {v.follower_count if v.follower_count is not None else 'n/a'}, "
f"views: {v.views if v.views is not None else 'n/a'}, "
f"likes: {v.likes if v.likes is not None else 'n/a'}, "
f"comments: {v.comments if v.comments is not None else 'n/a'}, "
f"engagement_rate: {er_str} "
f"[engagement_rate = (likes + comments) / views * 100], "
f"duration: {v.duration if v.duration is not None else 'n/a'}s, "
f"upload_date: {v.upload_date or 'n/a'}, url: {v.url}"
)
return "METRICS (computed, authoritative — do not alter these numbers):\n" + "\n".join(
lines
)