Backend:
- Add fetch_bill_actions task with pagination and idempotent upsert
- Add fetch_actions_for_active_bills nightly batch (4 AM UTC beat schedule)
- Wire fetch_bill_actions into new-bill creation and _update_bill_if_changed
- Add backfill_brief_citations task: detects pre-citation briefs by JSONB
type check, deletes them, re-queues LLM processing against stored text
(LLM calls only — zero Congress.gov or GovInfo calls)
- Add admin endpoints: POST /bills/{id}/reprocess, /backfill-citations,
/trigger-fetch-actions; add uncited_briefs count to /stats
Frontend:
- New BriefPanel component: wraps AIBriefCard, adds amber "What Changed"
badge for amendment briefs and collapsible version history with
inline brief expansion
- Swap AIBriefCard for BriefPanel on bill detail page
- Admin panel: Backfill Citations + Fetch Bill Actions buttons; amber
warning in stats when uncited briefs remain
- Add feature roadmap document with phased plan through Phase 5
Co-Authored-By: Jack Levy
164 lines
6.2 KiB
Python
164 lines
6.2 KiB
Python
"""
|
|
LLM processor — generates AI briefs for fetched bill documents.
|
|
Triggered by document_fetcher after successful text retrieval.
|
|
"""
|
|
import logging
|
|
import time
|
|
|
|
from sqlalchemy import text
|
|
|
|
from app.database import get_sync_db
|
|
from app.models import Bill, BillBrief, BillDocument, Member
|
|
from app.services.llm_service import get_llm_provider
|
|
from app.workers.celery_app import celery_app
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@celery_app.task(
|
|
bind=True,
|
|
max_retries=2,
|
|
rate_limit="10/m", # Respect LLM provider rate limits
|
|
name="app.workers.llm_processor.process_document_with_llm",
|
|
)
|
|
def process_document_with_llm(self, document_id: int):
|
|
"""Generate an AI brief for a bill document. Full brief for first version, amendment brief for subsequent versions."""
|
|
db = get_sync_db()
|
|
try:
|
|
# Idempotency: skip if brief already exists for this document
|
|
existing = db.query(BillBrief).filter_by(document_id=document_id).first()
|
|
if existing:
|
|
return {"status": "already_processed", "brief_id": existing.id}
|
|
|
|
doc = db.get(BillDocument, document_id)
|
|
if not doc or not doc.raw_text:
|
|
logger.warning(f"Document {document_id} not found or has no text")
|
|
return {"status": "no_document"}
|
|
|
|
bill = db.get(Bill, doc.bill_id)
|
|
if not bill:
|
|
return {"status": "no_bill"}
|
|
|
|
sponsor = db.get(Member, bill.sponsor_id) if bill.sponsor_id else None
|
|
|
|
bill_metadata = {
|
|
"title": bill.title or "Unknown Title",
|
|
"sponsor_name": sponsor.name if sponsor else "Unknown",
|
|
"party": sponsor.party if sponsor else "Unknown",
|
|
"state": sponsor.state if sponsor else "Unknown",
|
|
"chamber": bill.chamber or "Unknown",
|
|
"introduced_date": str(bill.introduced_date) if bill.introduced_date else "Unknown",
|
|
"latest_action_text": bill.latest_action_text or "None",
|
|
"latest_action_date": str(bill.latest_action_date) if bill.latest_action_date else "Unknown",
|
|
}
|
|
|
|
# Check if a full brief already exists for this bill (from an earlier document version)
|
|
previous_full_brief = (
|
|
db.query(BillBrief)
|
|
.filter_by(bill_id=doc.bill_id, brief_type="full")
|
|
.order_by(BillBrief.created_at.desc())
|
|
.first()
|
|
)
|
|
|
|
provider = get_llm_provider()
|
|
|
|
if previous_full_brief and previous_full_brief.document_id:
|
|
# New version of a bill we've already analyzed — generate amendment brief
|
|
previous_doc = db.get(BillDocument, previous_full_brief.document_id)
|
|
if previous_doc and previous_doc.raw_text:
|
|
logger.info(f"Generating amendment brief for document {document_id} (bill {doc.bill_id})")
|
|
brief = provider.generate_amendment_brief(doc.raw_text, previous_doc.raw_text, bill_metadata)
|
|
brief_type = "amendment"
|
|
else:
|
|
logger.info(f"Previous document unavailable, generating full brief for document {document_id}")
|
|
brief = provider.generate_brief(doc.raw_text, bill_metadata)
|
|
brief_type = "full"
|
|
else:
|
|
logger.info(f"Generating full brief for document {document_id} (bill {doc.bill_id})")
|
|
brief = provider.generate_brief(doc.raw_text, bill_metadata)
|
|
brief_type = "full"
|
|
|
|
db_brief = BillBrief(
|
|
bill_id=doc.bill_id,
|
|
document_id=document_id,
|
|
brief_type=brief_type,
|
|
summary=brief.summary,
|
|
key_points=brief.key_points,
|
|
risks=brief.risks,
|
|
deadlines=brief.deadlines,
|
|
topic_tags=brief.topic_tags,
|
|
llm_provider=brief.llm_provider,
|
|
llm_model=brief.llm_model,
|
|
govinfo_url=doc.govinfo_url,
|
|
)
|
|
db.add(db_brief)
|
|
db.commit()
|
|
db.refresh(db_brief)
|
|
|
|
logger.info(f"{brief_type.capitalize()} brief {db_brief.id} created for bill {doc.bill_id} using {brief.llm_provider}/{brief.llm_model}")
|
|
|
|
# Trigger news fetch now that we have topic tags
|
|
from app.workers.news_fetcher import fetch_news_for_bill
|
|
fetch_news_for_bill.delay(doc.bill_id)
|
|
|
|
return {"status": "ok", "brief_id": db_brief.id, "brief_type": brief_type}
|
|
|
|
except Exception as exc:
|
|
db.rollback()
|
|
logger.error(f"LLM processing failed for document {document_id}: {exc}")
|
|
raise self.retry(exc=exc, countdown=300) # 5 min backoff for LLM failures
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@celery_app.task(bind=True, name="app.workers.llm_processor.backfill_brief_citations")
|
|
def backfill_brief_citations(self):
|
|
"""
|
|
Find briefs generated before citation support was added (key_points contains plain
|
|
strings instead of {text, citation, quote} objects), delete them, and re-queue
|
|
LLM processing against the already-stored document text.
|
|
|
|
No Congress.gov or GovInfo calls — only LLM calls.
|
|
"""
|
|
db = get_sync_db()
|
|
try:
|
|
uncited = db.execute(text("""
|
|
SELECT id, document_id, bill_id
|
|
FROM bill_briefs
|
|
WHERE key_points IS NOT NULL
|
|
AND jsonb_array_length(key_points) > 0
|
|
AND jsonb_typeof(key_points->0) = 'string'
|
|
""")).fetchall()
|
|
|
|
total = len(uncited)
|
|
queued = 0
|
|
skipped = 0
|
|
|
|
for row in uncited:
|
|
if not row.document_id:
|
|
skipped += 1
|
|
continue
|
|
|
|
# Confirm the document still has text before deleting the brief
|
|
doc = db.get(BillDocument, row.document_id)
|
|
if not doc or not doc.raw_text:
|
|
skipped += 1
|
|
continue
|
|
|
|
brief = db.get(BillBrief, row.id)
|
|
if brief:
|
|
db.delete(brief)
|
|
db.commit()
|
|
|
|
process_document_with_llm.delay(row.document_id)
|
|
queued += 1
|
|
time.sleep(0.1) # Avoid burst-queuing all LLM tasks at once
|
|
|
|
logger.info(
|
|
f"backfill_brief_citations: {total} uncited briefs found, "
|
|
f"{queued} re-queued, {skipped} skipped (no document text)"
|
|
)
|
|
return {"total": total, "queued": queued, "skipped": skipped}
|
|
finally:
|
|
db.close()
|