cd /news/large-language-models/candidate-compliance-agent-building-… · home topics large-language-models article
[ARTICLE · art-48768] src=dev.to ↗ pub= topic=large-language-models verified=true sentiment=↑ positive

Candidate Compliance Agent: Building a Multilingual RAG System for Tamil Nadu Election Affidavits

A developer built Candidate Compliance Agent, a multilingual RAG system that lets users query Tamil Nadu election candidate affidavits in English or Tamil. The system extracts text from scanned PDFs using Tesseract OCR, creates semantic documents per candidate topic, and uses a multilingual sentence transformer for embeddings. It also features intent routing and a persistent memory service for context across sessions.

read4 min views1 publishedJul 7, 2026

Every election cycle in India, candidates are not easily accessible to many so they don't the many information about candidate's they are voting for.

Candidate Compliance Agent is an AI agent that lets anyone query Tamil Nadu election candidate affidavits in plain English or Tamil. Ask it about assets, criminal records, income declarations, education — it answers from the actual documents.

Live demo: https://affidavit-rag-frontend.vercel.app

This wasn't a clean dataset project. The raw materials were:

Scanned PDFs (~6MB each)

Mixed Tamil and English text

Stamp paper backgrounds with notary seals overlaid on text

Inconsistent formatting across candidates

React Frontend (Vercel)

Flask Backend (Railway)

Hindsight Memory (recall past context)

Intent Router

Structured Query OR Semantic RAG

Groq LLM (Llama 3.3 70B)

Hindsight Memory (retain new context)

Response + Follow-up Questions

Technical Feature Implemnentaion Walkthrough

The first challenge was extracting text from scanned PDFs. I used Tesseract with Tamil + English language models:

python

text = pytesseract.image_to_string(
    image,
    lang="eng+tam",
    config="--psm 6"
)

--psm 6 treats each page as a uniform text block — critical for structured affidavit forms.

Each PDF was converted to images at 300 DPI using pdf2image, then OCR'd page by page. Output was stored as JSON with metadata:

{
  "text": "...",
  "metadata": {
    "candidate": "MKStalin",
    "party": "DMK",
    "constituency": "KOLATHUR",
    "page": 3
  }
}

The biggest architectural decision was how to chunk the data.

Naive approach: Split each page into 500-token chunks with overlap. This gives you 600+ noisy chunks where assets, criminal records, and education are all mixed together.

Better approach: Build one semantic document per topic per candidate.

Each candidate gets 6 documents:

Identity and contact

Education

Criminal cases (structured)

Criminal detail (raw OCR text)

Income tax

Assets and liabilities

def build_docs(candidate, raw_pages):
    docs = []
    docs.append({"section": "criminal_cases", "text": f"""
Candidate: {name}
Party: {party}
Section: Criminal Cases

Pending Cases: {pending}
Convicted Cases: {convicted}
Summary: {"No criminal record" if pending == 0 else f"{pending} pending"}
"""})
    return docs

35 candidates × 6 sections = 210 clean semantic documents instead of 600+ noisy chunks.

Result: When someone asks "does certain candidate have criminal cases?" the retrieval hits the criminal_cases document directly instead of a random page slice.

I used paraphrase-multilingual-MiniLM-L12-v2 for embeddings — a sentence transformer model that handles Tamil and English in the same vector space.

model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2")

embeddings = model.encode(texts, batch_size=32).tolist()

collection.add(
    documents=texts,
    embeddings=embeddings,
    metadatas=[{
        "candidate": d["candidate"],
        "party": d["party"],
        "section": d["section"],
    } for d in all_docs],
    ids=[f"{d['candidate']}_{d['section']}" for d in all_docs]
)

there are 5 intent category based on that the query is passed through

def detect_intent(query):
    q = query.lower()
    for party in PARTIES:
        if party in q:
            return "party"
    for pattern in EDUCATION_PATTERNS:
        if re.search(pattern, q):
            return "education"
    for word in SEMANTIC_KEYWORDS:
        if word in q:
            return "semantic"
    return "unknown"

Hindsight is a persistent memory service that stores and recalls context across sessions. When a user comes back the next day and asks a follow-up question, the agent remembers what they asked before.

from hindsight_client import Hindsight

client = Hindsight(
    base_url="https://api.hindsight.vectorize.io",
    api_key=os.getenv("HINDSIGHT_API_KEY")
)

def recall_memory(session_id, query):
    result = client.recall(
        bank_id="candidate-rag",
        query=f"Session: {session_id}\nQuestion: {query}"
    )
    return "\n".join([m.text for m in result.results])

def retain_memory(session_id, memory):
    client.retain(
        bank_id="candidate-rag",
        content=memory,
        metadata={"session_id": session_id}
    )

past_memory = recall_memory(session_id, query)

prompt = f"""

Previous context: {past_memory}

Context from affidavits: {retrieved_context}

Question: {query}

"""

answer = ask_groq(prompt)

retain_memory(session_id, f"User asked: {query}. Answer: {answer[:200]}")

Why this matters: A voter researching a candidate can ask multiple questions across multiple sessions and get progressively better, context-aware answers.

After every answer, the LLM generates 4 follow-up questions based on the query, retrieved context, and memory:

ANSWER:

Poornima has no pending criminal cases.

FOLLOW_UP_QUESTIONS:

These appear as clickable buttons — clicking fills the input box directly.

Backend: Flask + gunicorn on Railway

Frontend: React on Vercel

Vector DB: ChromaDB (persistent, committed to GitHub)

LLM: Groq API (Llama 3.3 70B Versatile)

Memory: Hindsight

OCR quality determines everything downstream. Bad OCR produces bad embeddings. The --psm 6 config change improved Tamil extraction significantly.

Semantic chunking beats token chunking. 210 topic-focused documents retrieve better than 600+ random page slices.

Persistent memory changes the interaction model. Without Hindsight, every conversation starts cold. With it, the agent builds understanding over time.

Real-world data is always messier than tutorials suggest. Stamp paper backgrounds, notary seal overlays, mixed scripts — none of this appears in RAG tutorials.

What's Next

Scale to all 234 Tamil Nadu constituencies (~4500 PDFs)

Add proactive alerts when new affidavits are filed

Support voice queries in Tamil

── more in #large-language-models 4 stories · sorted by recency
── more on @tesseract 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/candidate-compliance…] indexed:0 read:4min 2026-07-07 ·