Back to guides
5
5 min

HR App

Chat Interface & Query Routing

Where Everything Comes Together

Modules 1-4 built the infrastructure: data lake, encoding pipeline, retrieval system, AI gateway. Module 5 is where it becomes a product that employees actually use. The goal is deceptively simple: a chat interface where someone types "What's our parental leave policy in California?" and gets an accurate, cited answer in under 3 seconds.

But "simple" on the surface requires sophisticated orchestration underneath.

The Chat Architecture

Message Flow

Employee types query
    ↓
Frontend sends POST /api/chat
    ↓
API classifies query category
    ↓
Retrieval system searches (filtered by category)
    ↓
AI Gateway processes (guardrails → route → LLM)
    ↓
Response streams back via SSE
    ↓
Frontend renders with citations

Every step is visible in the code. There's no magic — just a pipeline where each stage does one thing well.

Server-Sent Events (SSE)

The chat uses SSE streaming, not WebSockets. Why?

  • Simpler — SSE is a standard HTTP response, no upgrade handshake
  • One-directional — the server pushes tokens as they're generated
  • Reconnectable — built-in reconnection if the connection drops
  • Vercel-compatible — SSE works on serverless; WebSockets don't (without additional infrastructure)
  • Each SSE event carries a JSON payload:

    data: {"content": "Based on our", "category": "Leave & PTO"}
    data: {"content": "Based on our PTO policy", "category": "Leave & PTO"}
    data: {"content": "Based on our PTO policy (POL-001,", "citations": [...], "category": "Leave & PTO"}
    data: [DONE]

    The frontend accumulates content and displays the growing response in real time. Citations arrive with the final chunk.

    Query Routing

    Not all HR questions should search the same data. The routing layer detects what kind of question is being asked and targets the search accordingly:

    Category Detection

    SignalCategorySearch Strategy
    "leave", "PTO", "vacation", "sick"Leave & PTOpolicies + handbook (leave sections)
    "benefit", "401k", "insurance", "dental"Benefitsbenefits guide
    "reports to", "manager", "who is"Organizationorg chart
    "expense", "travel", "reimburse"Financepolicies (expense, travel)
    "remote", "WFH", "work from home"Remote Workhandbook (remote work)
    "harassment", "discrimination"Compliancepolicies + handbook (sensitive flag)

    This isn't just for UI badges — it determines which source_type filter is applied during retrieval. A benefits question searches the benefits guide first, falling back to other sources only if needed.

    Multi-Source Questions

    Some questions span categories: "If I take parental leave, what happens to my health insurance?" This touches both Leave and Benefits. The router detects multi-category queries and searches across both, then merges and reranks the results.

    Citations: The Trust Layer

    Every response from the HR assistant includes citations. This isn't a nice-to-have — it's what makes employees trust the system over Googling or asking a coworker.

    Citation Format

    Source: PTO Policy (POL-001, v4.0, effective 2025-01-01)
    Section: Carryover Rules

    Each citation includes:

  • Document name — which policy, handbook section, or guide
  • Policy ID — for formal policies, the ID for cross-reference
  • Version — which version of the policy is being cited
  • Effective date — when this version took effect
  • Why Effective Dates Matter

    Imagine a policy was updated on March 1, 2025. An employee asks about something that happened in February 2025. The AI should cite the *old* version that was in effect at the time, not the current version. While the basic system always cites the latest version, the citation metadata enables auditing: "This answer is based on v4.0, effective 2025-01-01."

    Employee Context

    In a production deployment, the chat knows who's asking:

  • Department — show relevant policies (Engineering-specific vs Sales-specific)
  • Location/State — automatically filter for state-specific provisions
  • Tenure — "How much PTO do I get?" depends on years of service
  • Role level — managers see team data; ICs see personal data only
  • This context is passed to the retrieval system as filters and to the AI gateway for confidentiality enforcement. The same question from a California engineer and a Texas salesperson can produce different — and correct — answers.

    Quick Actions

    The UI includes pre-built query buttons for the most common HR questions:

  • "What's our parental leave policy?"
  • "How does the 401k match work?"
  • "Who reports to the VP of Engineering?"
  • "How do I file an expense report?"
  • "Can I work remotely from another state?"
  • These aren't just convenience — they're data collection. Tracking which quick actions get clicked tells HR which topics employees ask about most, informing which policies to clarify or simplify.

    Conversation Persistence

    Every conversation is saved:

    {
      conversation_id: "conv_123",
      employee_id: "EMP-008",
      messages: [...],
      topic_tags: ["leave", "california"],
      started_at: "2025-11-15T10:30:00Z",
      satisfaction_rating: 4,  // optional post-chat rating
    }

    This data serves three purposes:

  • Continuity — the employee can return to a previous conversation
  • Analytics — HR sees what questions are being asked (aggregated, anonymized)
  • Improvement — unanswered or low-satisfaction queries identify gaps in the knowledge base
  • What You'll Build

  • Explore the pre-seeded Next.js chat interface
  • Wire the chat to the retrieval system and AI gateway
  • Implement query routing with category detection
  • Add policy citations with version and effective date
  • Add employee context for personalized results
  • Launch the dev server and test end-to-end
  • Glossary

    TermMeaning
    SSEServer-Sent Events — one-way streaming from server to client
    Query routingDirecting queries to the right data sources based on category
    CitationSource attribution linking an answer to its policy document
    Employee contextUser-specific info (dept, location, tenure) used to personalize results
    Quick actionPre-built query button for common HR questions
    Conversation persistenceSaving chat history for continuity and analytics

    This is chapter 5 of AI HR Assistant.

    Get the full hands-on course for $100 and build the complete system. Your projects become your portfolio.

    View course details