mcp-client / Projects /LeaseGuard /Bridgeprompt.md
Raj718's picture
feat: Complete Task 1 - Environment Setup & Security Hardening
4e39be7

A newer version of the Gradio SDK is available: 6.14.0

Upgrade

🏠 LeaseGuard Enhanced PromptBridge Blueprint Redis-Powered AI Assistant for Tenant Rights & Lease Enforcement Final Implementation-Ready Version | Cursor Agent Compatible

⚑️ Section 1: Application Foundation πŸ“Œ Core Problem & Vision Problem: Renters often sign leases without fully understanding their rights or the enforceability of clauses. When disputes arise (e.g. eviction threats, repair neglect, illegal rent hikes), they are overwhelmed, under-informed, and unsupported in real time. Vision: Build LeaseGuard β€” a real-time, AI-powered assistant that reads lease documents, flags illegal or unenforceable clauses, and gives tenants tailored advice based on local housing law. Powered by Redis 8 for lightning-fast vector search, document chunking, semantic caching, and event-driven alerts, LeaseGuard is a legal exosuit for renters. 🎯 Target Audience & Context Primary Users: NYC tenants and voucher holders (Beginner–Intermediate tech literacy). Secondary Users: Tenant advocacy orgs, legal aid workers (Intermediate–Advanced). Usage Context: Mobile-first for on-the-go assistance during disputes; desktop for deep lease analysis and document uploads. βœ… Success Definition for MVP Users upload a lease (PDF or image-to-text). System extracts clauses, runs local housing law checks, and flags violations. User can ask follow-up questions in natural language. MVP supports English and Spanish leases. Logs session ID to track multi-query journeys. Redis 8 powers all real-time logic, search, and caching. πŸ“± Platform Priority βœ… [x] Web App (Browser-based) βœ… [x] Mobile App (iOS/Android β€” responsive web) β˜‘οΈ [ ] API-First Service β˜‘οΈ [ ] Multi-platform (web priority)

🧱 Section 2: Feature Architecture & UX Flow 🧩 Core Features Matrix Priority Feature User Story Complexity UX Law Applied πŸ”Ή Core Lease Upload (PDF/OCR) "As a tenant, I want to upload my lease so I can understand my rights." Medium Fitts's Law (large touch targets) πŸ”Ή Core Clause Extraction & Parsing "As a tenant, I want to see each clause broken down clearly." High Von Restorff Effect (highlight issues) πŸ”Ή Core Clause Legality Flagging "As a tenant, I want to know which clauses might be illegal." High Jakob's Law (familiar colors/icons) πŸ”Ή Core Q&A with AI Assistant "I want to ask questions about my lease in plain English." Medium Krug's Law (don't make me think) πŸ”Ή Core Vector Search & Contextual Recall "I want my follow-up questions to remember my lease context." High Doherty Threshold (fast response = delight) πŸ”Έ Nice Alert System for Deadlines "Notify me before my rent is due or my landlord violates terms." Medium Aesthetic-Usability Effect πŸ”Έ Nice Session ID + Timeline View "I want to review my past questions and answers." Low Zeigarnik Effect (unfinished tasks) πŸ”Έ Nice Share Session With Lawyer "I want to give my legal aid access to my flagged clauses." Medium Miller's Law (limited memory slots)

πŸ‘£ User Journey Mapping (Krug's Principles) Entry Point: Social referral, QR code on flyers, housing advocate links β†’ opens LeaseGuard app. Onboarding: Upload lease β†’ guided walkthrough showing progress bar, mobile-friendly UI. Core Workflow: Upload lease β†’ OCR + vector embedding β†’ clause extraction. AI assistant highlights problematic clauses with legal explanations. User types or speaks questions (voice-to-text optional). Context-aware follow-up via Redis semantic caching + session tracking. Edge Cases: Offline lease upload queued for processing. Unreadable scans prompt re-upload. "Help me talk to a lawyer" trigger opens human handoff (email/chat). Exit/Completion: User gets lease summary, saved session link, next steps (e.g., "file 311 complaint"). Feature: Lease Document Upload and Analysis As a NYC tenant I want to upload my lease document and get instant analysis So that I can understand my rights and identify potentially illegal clauses

Background: Given the LeaseGuard application is running And Redis Cloud is connected and operational And the housing law database is populated with NYC regulations And Gemini Flash 1.5 LLM is available

@core @mobile-first Scenario: Successful lease upload and clause extraction Given I am on the LeaseGuard home page When I click the "Upload Your Lease" button And I select a PDF file from my device And the file is under 10MB in size Then I should see a progress bar indicating "Analyzing your lease..." And the system should extract text using PDF.js And the document should be chunked into clauses And I should see "Analysis Complete" within 5 seconds And I should be redirected to the lease analysis dashboard

@core @accessibility Scenario: Upload with scanned lease document (OCR required) Given I am on the upload page When I select an image file (JPG/PNG) of my lease And the image contains scanned text Then I should see "Processing scanned document..." message And Tesseract.js should begin OCR extraction And I should see estimated processing time When OCR processing completes Then I should see the extracted text for review And I should be able to confirm or edit the extracted content And I should proceed to clause analysis

@core @error-handling Scenario: Upload fails due to unreadable document Given I am on the upload page When I select a corrupted or heavily distorted image And OCR processing cannot extract readable text Then I should see "Unable to read document" error message And I should see "Try uploading a clearer image" suggestion And I should see a "Manual Text Entry" fallback option And I should be able to type my lease clauses manually

@core @multilingual Scenario: Spanish language lease processing Given I have set my language preference to Spanish When I upload a lease document in Spanish Then the system should detect the Spanish language And clause extraction should process Spanish legal terms And housing law comparisons should use Spanish regulation database And all UI messages should display in Spanish And flagged violations should show Spanish legal explanations

Feature: Clause Analysis and Violation Detection As a tenant I want to see which clauses in my lease might be illegal So that I can take appropriate action to protect my rights

@core @redis-vector-search Scenario: Illegal clause detection with high confidence Given my lease has been uploaded and processed And clauses have been stored in Redis with vector embeddings When the system performs similarity matching against housing law database And a clause about "security deposit of 3 months rent" is found Then the clause should be flagged as "CRITICAL" violation And I should see the clause highlighted in red And I should see the explanation "NYC limits security deposits to 1 month's rent maximum" And I should see the legal reference "NYC Housing Maintenance Code Β§27-2056" And I should see a "Contact Legal Aid" button

@core @redis-caching Scenario: Medium severity clause flagging Given my lease contains a clause about "tenant responsible for all repairs" When the system checks this against warranty of habitability laws Then the clause should be flagged as "HIGH" severity And I should see it highlighted in orange And I should see "Landlords cannot waive repair responsibilities" explanation And the result should be cached in Redis for similar future queries

@nice-to-have @trending Scenario: Clause with no violations found Given my lease contains standard rent payment terms When the system analyzes the clause for violations And no similar patterns exist in the illegal clause database Then the clause should be marked as "COMPLIANT" And I should see it with a green checkmark And I should see "This clause appears standard and legal"

Feature: AI-Powered Q&A with Contextual Memory As a tenant I want to ask follow-up questions about my lease So that I can get personalized advice based on my specific situation

@core @conversational-ai Scenario: First question about flagged clause Given I have a lease with flagged violations And I am viewing the analysis dashboard When I type "What should I do about the security deposit issue?" Then Gemini Flash 1.5 should process my question And the system should retrieve relevant clause context from Redis And I should receive a response within 2.5 seconds And the response should reference my specific security deposit clause And I should see actionable next steps like "Contact your landlord" or "File a complaint"

@core @session-memory Scenario: Follow-up question with context retention Given I previously asked about security deposit violations And my session context is stored in Redis When I ask "How do I get my extra deposit money back?" Then the system should remember our previous conversation And the response should build on the security deposit context And I should not need to re-explain my situation And the conversation history should be maintained in my session

@core @legal-disclaimer Scenario: Complex legal question requiring professional help Given I ask "Should I break my lease and stop paying rent?" When Gemini processes this high-stakes legal question Then the response should include appropriate legal disclaimers And I should see "This is not legal advice" warning And I should be offered connection to legal aid services And the system should not provide definitive legal recommendations

Feature: Session Management and Timeline View As a tenant I want to review my previous questions and lease analysis So that I can track my progress and share information with advocates

@nice-to-have @session-tracking Scenario: Viewing conversation timeline Given I have had multiple conversations about my lease And my session data is stored in Redis and Supabase When I click "View My Timeline" Then I should see a chronological list of my questions and answers And I should see the dates and times of each interaction And I should be able to expand each conversation thread And I should see which clauses were discussed in each conversation

@nice-to-have @sharing Scenario: Sharing session with legal aid Given I have completed my lease analysis And I have flagged violations that need professional help When I click "Share with Legal Aid" Then I should see a unique shareable link generated And the link should provide access to my lease analysis And the link should include my conversation history And the legal aid worker should be able to view flagged clauses And personal identifying information should be redacted from the shared view βš™οΈ System Logic & Data Flow Triggers: New file uploaded β†’ begin clause extraction pipeline. User submits a question β†’ fetch vector-matched clause chunks from Redis. Flagged clause β†’ trigger notification or escalation flow. Business Rules: Clause flagged if similarity β‰₯ 0.85 threshold to illegal clause DB. Session stored for 7 days unless user opts to save permanently. User's follow-up questions always scoped to their upload session. Data Transformations: PDF/Image β†’ text via OCR β†’ chunked with metadata β†’ embedded and stored in Redis vector DB. Clauses matched against local housing rights database (stored in RedisJSON). Integration Points: Redis Streams β†’ trigger clause checking + LLM calls. Redis Vector + Full-Text Search for real-time clause matching. Supabase for structured analytics logging (optional). Feature: Document Processing Pipeline As the LeaseGuard system I need to efficiently process uploaded documents So that users receive fast and accurate lease analysis

Background: Given Redis Cloud is configured with vector search capabilities And the system has access to Gemini Flash 1.5 API And PDF.js and Tesseract.js libraries are loaded

@system @redis-streams Scenario: Document upload triggers processing pipeline Given a user uploads a PDF lease document When the file upload completes successfully Then a "document_uploaded" event should be published to Redis Stream "lease_processing" And the event should contain user_session_id, document_id, and file_metadata And a background worker should consume the event within 100ms And the document should be queued for text extraction

@system @ocr-processing Scenario: PDF text extraction and chunking Given a PDF document is queued for processing When the text extraction worker processes the document Then PDF.js should extract text content And the text should be split into logical clauses based on line breaks and legal formatting And each clause should be assigned a unique clause_id And clauses should be stored as separate entries with metadata And a "clauses_extracted" event should be published to Redis Stream

@system @vector-embeddings Scenario: Clause vectorization and storage Given clauses have been extracted from a lease document When the vectorization worker processes the clauses Then each clause should be sent to Gemini Flash 1.5 for embedding generation And the 768-dimensional vectors should be stored in Redis vector index And clause metadata should be stored in RedisJSON format And the vector index should support cosine similarity search And a "vectorization_complete" event should be published

@system @performance Scenario: Concurrent document processing Given multiple users upload documents simultaneously When the system processes 10 concurrent uploads Then each document should be processed independently And Redis Streams should handle event ordering correctly And no processing conflicts should occur And total processing time should remain under 5 seconds per document And system resources should not exceed 80% utilization

Feature: Housing Law Violation Detection As the LeaseGuard system I need to accurately identify illegal lease clauses So that tenants receive reliable legal guidance

@system @redis-vector-search Scenario: Similarity matching against violation patterns Given a lease clause "Security deposit shall be 2.5 months rent" And the housing law database contains violation patterns in Redis When the system performs vector similarity search Then the clause vector should be compared against violation pattern vectors And similarity scores should be calculated using cosine distance And patterns with similarity >= 0.85 should be considered matches And the highest matching violation should be selected

@system @classification-logic Scenario: Violation severity classification Given a clause matches a known violation pattern When the system determines violation severity Then security deposit violations should be classified as "CRITICAL" And repair responsibility waivers should be classified as "HIGH" And minor disclosure issues should be classified as "MEDIUM" And unclear language should be classified as "LOW" And the classification should be stored with the clause metadata

@system @caching-strategy Scenario: Caching violation detection results Given a clause has been analyzed for violations When the analysis completes Then the violation result should be cached in Redis with TTL of 24 hours And the cache key should include clause text hash and law database version And future identical clauses should return cached results within 50ms And cache hit rate should exceed 70% for common clause patterns

Feature: Contextual AI Question Processing As the LeaseGuard system I need to provide contextually relevant answers to user questions So that tenants receive personalized and accurate guidance

@system @context-retrieval Scenario: Question context preparation Given a user asks "What can I do about the security deposit?" And the user's lease contains flagged security deposit violations When the system prepares the LLM context Then relevant lease clauses should be retrieved from Redis vector search And flagged violations should be included in context And previous conversation history should be retrieved from session storage And NYC housing law references should be added to context And the complete context should be under 4000 tokens

@system @llm-integration Scenario: Gemini Flash 1.5 query processing Given a user question with prepared context When the system sends the query to Gemini Flash 1.5 Then the request should include the user question And the request should include relevant lease clause context And the request should include applicable housing law information And the request should specify response format and length limits And the response should be received within 800ms And the response should be cached for identical future queries

@system @response-processing Scenario: LLM response validation and formatting Given Gemini returns a response to a user question When the system processes the response Then the response should be checked for harmful or incorrect legal advice And legal disclaimers should be automatically appended And the response should be formatted for mobile-friendly display And relevant clause references should be highlighted And the response should be stored in conversation history

Feature: Session State Management As the LeaseGuard system I need to maintain user session state across interactions So that conversations remain contextual and personalized

@system @session-creation Scenario: New user session initialization Given a user visits LeaseGuard for the first time When they begin the lease upload process Then a unique session_id should be generated And session metadata should be stored in Redis with 7-day TTL And the session should track user preferences (language, notifications) And anonymous user sessions should be supported without authentication And session state should be synced to Supabase for persistence

@system @session-persistence Scenario: Session data synchronization Given a user has an active session with lease analysis When they ask questions or interact with the system Then each interaction should update the session state in Redis And conversation history should be appended to the session And session metadata should be synced to Supabase every 5 minutes And session expiration should be extended on user activity And inactive sessions should be cleaned up after 7 days

@system @cross-device-continuity Scenario: Session recovery across devices Given a user starts analysis on mobile and switches to desktop When they log in with the same Clerk account Then their session state should be retrieved from Supabase And their lease analysis should be restored in Redis And conversation history should be available And flagged clauses should display with original analysis And they should be able to continue where they left off

Feature: Error Handling and System Resilience As the LeaseGuard system I need to handle failures gracefully So that users have a reliable experience even when components fail

@system @redis-failover Scenario: Redis connection failure handling Given Redis Cloud becomes temporarily unavailable When a user tries to ask a question about their lease Then the system should detect the Redis connection failure And a fallback message should be displayed: "Service temporarily unavailable" And the user question should be queued for processing when Redis recovers And basic functionality should continue using local storage cache And users should be notified when full service is restored

@system @llm-failure-recovery Scenario: Gemini API failure handling Given Gemini Flash 1.5 API returns an error or timeout When a user submits a question Then the system should retry the request up to 3 times And if all retries fail, a graceful error message should be shown And the user should be offered alternative actions like "Contact Legal Aid" And the failed query should be logged for later processing And the system should automatically retry when the API recovers

@system @data-consistency Scenario: Partial processing failure recovery Given a lease document is partially processed (text extracted but not vectorized) When the user tries to ask questions about clauses Then the system should detect incomplete processing state And processing should automatically resume from the last successful step And the user should see a progress indicator for the remaining processing And no duplicate processing should occur And processing state should be atomically updated in Redis

@system @monitoring-alerting Scenario: System health monitoring Given the LeaseGuard system is running in production When system metrics are collected every minute Then Redis query response times should be monitored And Gemini API latency and error rates should be tracked And document processing success rates should be measured And user session creation/expiration rates should be logged And alerts should be triggered if any metric exceeds thresholds And system administrators should receive notifications for critical issues

πŸ”§ Section 3: Technical Stack & Architecture πŸ’» Technology Stack (FINAL DECISIONS) Layer Technology Rationale Experience Level Frontend Next.js + Tailwind + Vercel SSR for SEO, fast prototyping, smooth deployment Intermediate Backend Node.js (Express) + LangChain Simple API endpoints + LLM orchestration Intermediate LLM Provider Google Gemini Flash 1.5 Fast inference, competitive pricing, multimodal support Intermediate Database Redis Cloud (Vector, JSON, Streams, Search) + Supabase (analytics) High-speed data ops, state caching, hybrid LLM context memory Intermediate Auth Clerk.dev Quick, secure auth with tenant-friendly UX Beginner OCR Strategy PDF.js (text extraction) + Tesseract.js (scanned images) Client-side processing, no server overhead for text PDFs Intermediate

🏠 Housing Law Data Source (IMPLEMENTED SOLUTION) Primary Data Sources: NYC Attorney General Tenant Rights Guide - Comprehensive guide covering discrimination, rent stabilization, and illegal lease clauses NYC Rent Guidelines Board - Official rent stabilization laws and building lists Legal Services NYC Resource Database - Complete tenant rights documentation with clause-by-clause analysis Implementation Strategy: Curated Knowledge Base: Manual compilation of illegal/unenforceable clauses from official NYC sources RedisJSON Storage: Structure common violations (security deposit limits, subletting restrictions, repair obligations) Vector Similarity Matching: Compare uploaded lease clauses against known problematic patterns Regular Updates: Monthly sync with NYC housing law changes via RSS/API monitoring Sample Illegal Clauses Database: { "security_deposit_violation": { "pattern": "security deposit exceeding one month rent", "law_reference": "NYC Housing Maintenance Code Β§27-2056", "severity": "high", "explanation": "NYC limits security deposits to 1 month's rent maximum" }, "repair_waiver": { "pattern": "tenant waives right to repairs", "law_reference": "Warranty of Habitability", "severity": "critical", "explanation": "Tenants cannot waive their right to habitable conditions" } } πŸ“Š Performance & Scale Requirements Expected User Load: MVP: ~200 users/day Hackathon demo: 10–15 concurrent sessions Scalable to: 10K monthly active users Data Volume: ~3–10 pages per lease upload β†’ 100–300 vectorized chunks per file Redis handles embedding store + JSON metadata Supabase logs ~1–3 interactions per session Response Time: AI Q&A latency < 2.5s (Gemini Flash 1.5 target: 800ms) Clause scan return time < 1s (Redis cached match) Availability: MVP: 99% uptime Goal: 99.9% (with Redis Cloud + fallback queue) 🌐 Deployment & Infrastructure Component Service Configuration Frontend Vercel Next.js auto-deploy from GitHub Backend Railway.app Node.js + Express API Database Redis Cloud Free tier: 30MB, vector search enabled Auth Clerk.dev Free tier: 5,000 users Storage Supabase PostgreSQL for session logs CI/CD GitHub Actions Auto-deploy on main branch push Monitoring Sentry + Redis Cloud Dashboard Error tracking + performance metrics

πŸ” Section 4: Security & Compliance (S.A.F.E. Focus) πŸ“¦ Data Classification & Protection Data Type Sensitivity Level Storage Method Retention Policy OWASP Risk Login Credentials High Clerk (offloaded auth) Account lifetime Broken Authentication Lease Documents High Redis (JSON), temp file for parse 30 days (MVP) Sensitive Data Exposure Session Metadata Medium Supabase 90 days Insufficient Logging Interaction Logs Medium Supabase 90 days Security Misconfiguration Uploaded Files High In-memory/file cache Deleted after parse XML External Entities (XXE)

πŸ›‘οΈ Security Requirements (OWASP Top 10 Defense) Concern Mitigation Strategy βœ… Injection Protection Redis + parameterized inputs, no raw DB queries βœ… Broken Authentication Clerk handles session, MFA optional βœ… Sensitive Data Exposure File parsing is ephemeral; vector store stripped of names/emails βœ… XML External Entities (XXE) File uploads validated, parsed using PDF.js + safety wrapper βœ… Broken Access Control Role-based routing (admin panel vs. anonymous user) βœ… Security Misconfiguration Hard-coded secrets avoided, env var checks + CI scanner βœ… Cross-Site Scripting (XSS) Escape inputs in frontend, LLM output sandboxed βœ… Insecure Deserialization Use JSON only; no parsing of serialized objects βœ… Known Vulnerabilities npm audit, Snyk monitoring βœ… Insufficient Logging Supabase logs, Redis Streams for access events

πŸ”— Section 5: Integrations & External Dependencies πŸ”Œ Required APIs & Services Service Purpose SLA Requirements Fallback Strategy Redis Cloud Tenant query context caching & routing <100ms round-trip Show "Offline β€” retry" UI fallback Supabase Logging, session metadata 99.9% Store to localStorage temporarily Clerk Auth & session management High availability Anonymous-only mode fallback PDF.js Local PDF parsing (no backend calls) Offline local parse Show "Invalid doc" for unsupported files Tesseract.js OCR for scanned lease images Client-side processing Manual text input fallback Google Gemini Flash 1.5 LLM query resolution

99% availability Retry logic, "Could not respond" UI

βš™οΈ Automation & Workflows Type Purpose/Flow Scheduled Tasks Clean expired sessions / anonymized logs every 24h (Supabase cron) Event-Driven Actions Redis Stream logs when tenant submits doc β†’ Trigger "Document Parsed" event Business Automation LLM auto-selects clause types and flags violations for review Integration Patterns Redis for async streaming state updates, Webhooks from Supabase/Clerk, REST fallback

πŸš€ Section 6: Growth & Evolution Strategy 🌱 Phase 2+ Features Category Feature Justification User-Requested Document comparison for renewals or new leases Users want to see clause changes over time User-Requested SMS/email summary of flagged clauses Enables async review without app re-login Business-Driven Landlord Risk Profile aggregation Build a database of recurring violations for legal orgs Business-Driven API access for housing justice orgs Integrate LeaseGuard into case tracking tools Technical-Driven Clause Embedding Optimization w/ hybrid vector search Improve LLM retrieval performance Technical-Driven Tenant rights law update auto-ingest via RSS/Gov APIs Prevent outdated clause classification

πŸ“ˆ Success Metrics & KPIs Dimension KPI Example User Engagement Daily/weekly active tenants uploading docs Business Impact

of flagged violations β†’ successful resolutions

Technical Avg clause match time (<700ms goal) Security Zero critical incidents; full audit traceability

πŸ§ͺ Section 7: Testing & Quality Assurance Strategy 🎯 Testing Requirements Test Type Description Tooling / Notes Unit Testing Validate key components like clause extractors, Redis task queues, and session logic. Jest, Vitest, RedisMock Integration Testing Ensure correct flow between LLM responses, Redis cache, Supabase logging. Supertest, RedisInsight End-to-End Testing Validate entire UX from PDF upload β†’ clause summary β†’ follow-up question β†’ chat export. Playwright, Cypress Security Testing File upload fuzzing, prompt injection resistance, Redis ACL verification. OWASP ZAP, manual testing Performance Testing Evaluate Redis Streams under burst traffic, file uploads at scale. k6, Artillery Usability Testing Text density, clause comprehension, multi-turn dialog clarity. Moderated sessions with 5 testers, analytics

πŸ” Quality Metrics Category Metric Code Quality Maintainability Index > 75, Cyclomatic Complexity < 10 per function Test Coverage 85% unit test coverage on backend, 70% on front-end, 100% LLM flow logic Performance Benchmarks <300ms response time Redis cache hit, <800ms full query resolution time Security Standards Weekly automated scan, no critical OWASP alerts in production

🧠 Section 8: Implementation Guide πŸš€ Development Phase Sequence Phase 1: Core Infrastructure (Days 1-2) Setup Redis Cloud

Redis Cloud configuration

REDIS_URL=redis://username:password@redis-cloud-endpoint:port

Enable vector search, JSON, and Streams modules

Initialize Next.js + Clerk Auth

npx create-next-app leaseguard --typescript --tailwind npm install @clerk/nextjs

Setup Express Backend with LangChain

npm install express langchain @google-ai/generativelanguage Phase 2: Document Processing Pipeline (Days 2-3) PDF Processing Setup

// Frontend: PDF.js for text extraction import { getDocument } from 'pdfjs-dist';

// Backend: Tesseract.js for OCR fallback import Tesseract from 'tesseract.js';

Redis Vector Database Schema

// Clause storage structure const clauseSchema = { id: 'clause_uuid', text: 'clause content', vector: [0.1, 0.2, ...], // Gemini embeddings metadata: { leaseId: 'lease_uuid', section: 'rent_payment', flagged: boolean, severity: 'low|medium|high|critical' } }; Phase 3: LLM Integration (Days 3-4) Gemini Flash 1.5 Configuration

import { GoogleGenerativeAI } from '@google/generative-ai';

const genAI = new GoogleGenerativeAI(process.env.GOOGLE_AI_API_KEY); const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });

Housing Law Knowledge Base Population

// Populate Redis with curated NYC housing violations const housingLawDB = { illegal_clauses: [ { pattern: 'security deposit > 1 month rent', law: 'NYC Housing Maintenance Code Β§27-2056', severity: 'high' } // ... additional clauses from research ] }; Phase 4: UI/UX Implementation (Days 4-5) Mobile-First Upload Interface Large touch targets for file upload Progress indicators during processing Clear error states and retry mechanisms Clause Flagging Display Color-coded severity levels (red=critical, yellow=warning) Expandable explanations with legal references One-click "Contact Legal Aid" integration Phase 5: Testing & Deployment (Day 5) Integration Testing Upload β†’ Processing β†’ Flagging β†’ Q&A flow Redis performance under load Error handling for all edge cases Production Deployment Vercel frontend deployment Railway backend deployment Redis Cloud production instance Environment variable configuration 🎯 Critical Implementation Details Redis Configuration // redis-config.js const redis = new Redis({ host: process.env.REDIS_HOST, port: process.env.REDIS_PORT, password: process.env.REDIS_PASSWORD, // Enable vector search modules: ['search', 'json', 'timeseries'] });

// Create vector index for clause similarity await redis.call('FT.CREATE', 'clause_idx', 'ON', 'JSON', 'PREFIX', '1', 'clause:', 'SCHEMA', '$.text', 'AS', 'text', 'TEXT', '$.vector', 'AS', 'vector', 'VECTOR', 'FLAT', '6', 'TYPE', 'FLOAT32', 'DIM', '768', 'DISTANCE_METRIC', 'COSINE' ); Clause Matching Algorithm // clause-matcher.js async function findSimilarClauses(inputClause, threshold = 0.85) { // Generate embedding for input clause const embedding = await generateEmbedding(inputClause);

// Vector search in Redis const results = await redis.call('FT.SEARCH', 'clause_idx', *=>[KNN 5 @vector $vector AS score], 'PARAMS', '2', 'vector', Buffer.from(Float32Array.from(embedding).buffer), 'RETURN', '3', 'text', 'metadata', 'score', 'SORTBY', 'score' );

// Filter by similarity threshold return results.filter(result => result.score >= threshold); } πŸ”§ Environment Configuration Required Environment Variables

Core Services

REDIS_URL=redis://username:password@host:port GOOGLE_AI_API_KEY=your_gemini_api_key CLERK_SECRET_KEY=your_clerk_secret NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=your_clerk_public_key

Database

SUPABASE_URL=your_supabase_url SUPABASE_ANON_KEY=your_supabase_key

Application

NEXT_PUBLIC_APP_URL=https://leaseguard.vercel.app NODE_ENV=production Deployment Checklist [ ] Redis Cloud instance configured with vector search [ ] Gemini API key with sufficient quota [ ] Clerk authentication configured for production domain [ ] Supabase database with session logging tables [ ] Vercel deployment with proper environment variables [ ] Railway backend deployment with Redis connectivity [ ] SSL certificates and security headers configured [ ] Error monitoring (Sentry) activated [ ] Performance monitoring (Redis Cloud dashboard) enabled

πŸ† Redis Hackathon Alignment 🎯 Redis 8 Features Showcased Vector Search & Similarity Matching Clause comparison using cosine similarity Real-time lease analysis with <100ms response times Semantic search for follow-up questions RedisJSON for Complex Data Lease metadata storage with nested clause structures Housing law database with hierarchical organization Session state management with complex user interactions Redis Streams for Event Processing Document upload β†’ processing β†’ analysis pipeline Real-time notifications for clause violations Audit logging for compliance tracking Hybrid Search Capabilities Full-text search combined with vector similarity Multi-language support (English/Spanish) with unified search Context-aware query resolution πŸ’‘ Innovation Highlights AI-Powered Legal Assistant: First Redis-based tenant rights platform Real-Time Document Analysis: Sub-second clause flagging with Redis vector search Multi-Modal Processing: PDF + OCR + Vector embeddings in unified pipeline Social Impact: Addresses housing justice with cutting-edge technology πŸš€ Demo Showcase Points Upload & Instant Analysis: Show 30-page lease β†’ flagged violations in <5 seconds Contextual Q&A: Demonstrate memory of previous questions via Redis caching Multi-Language Support: Switch between English and Spanish interfaces Legal Aid Integration: Show "Connect to Lawyer" workflow with session sharing Performance Metrics: Display Redis query times and vector similarity scores