Before you start, please give a βοΈ to this repository if you find it useful! And open an issue if you have any questions or suggestions.
An advanced, production-ready spam detection system built with LangChain.js, Next.js, and GitHub Models. Features multiple AI-powered detection algorithms, memory capabilities, caching, and a beautiful web interface.
Click in the Codespaces button below to get started.
- β 4 Different Detection Algorithms - Basic, Advanced, Memory, and Comparison modes
- β LangChain.js Integration - Structured output parsing and chain orchestration
- β Memory & Learning - Detector that learns from previous analyses
- β Smart Caching - Reduces API calls and improves performance
- β Multi-step Analysis - Content analysis β Threat assessment β Final decision
- β GitHub Models Integration - Cost-effective AI inference
- β Beautiful Web Interface - Modern UI built with Next.js and Tailwind CSS
- β TypeScript - Full type safety and better DX
- β Production Ready - Error handling, retries, and validation
| Technology | Purpose | Version |
|---|---|---|
| Next.js 15 | Full-stack React framework | ^15.3.4 |
| TypeScript | Type safety and better DX | ^5.0 |
| LangChain.js | LLM orchestration and chains | ^0.3.29 |
| @langchain/openai | OpenAI integration | ^0.5.16 |
| Tailwind CSS | Styling and responsive design | ^3.4.16 |
| Zod | Schema validation | ^3.25.71 |
| GitHub Models | AI inference provider | Latest |
- Purpose: Fast, lightweight spam detection
- Technology: LangChain with structured output parsing
- Use Case: Quick analysis for high-volume scenarios
- Response Time: ~1-2 seconds
// Basic analysis with confidence scoring
{
isSpam: boolean,
reason: string,
confidence: number,
threatLevel: "LOW" | "MEDIUM" | "HIGH"
}- Purpose: Multi-step analysis with threat categorization
- Technology: Chain of specialized analyzers
- Steps: Content Analysis β Threat Assessment β Final Decision
- Use Case: Detailed analysis requiring comprehensive evaluation
// Advanced analysis with detailed breakdown
{
isSpam: boolean,
threatLevel: "LOW" | "MEDIUM" | "HIGH" | "CRITICAL",
analysis: {
suspiciousKeywords: string[],
phishingProbability: number,
spamCategory: "FINANCIAL" | "PHISHING" | "LOTTERY" | etc.
}
}- Purpose: Learning detector with context awareness
- Technology: BufferMemory + intelligent caching
- Features:
- Learns from previous analyses
- Pattern similarity scoring
- Smart caching (1-hour expiry, 100 item limit)
- Use Case: Adaptive detection that improves over time
// Memory-enhanced analysis
{
isSpam: boolean,
patternSimilarity: number,
learningFeedback: string,
fromCache: boolean
}- Purpose: Consensus-based detection using all algorithms
- Technology: Parallel execution with agreement scoring
- Output: Results from all detectors + consensus decision
- Use Case: High-stakes scenarios requiring maximum accuracy
- Node.js 18+
- npm or yarn
- GitHub Models API token (free)
git clone https://github.com/your-username/spam-ai-detector.git
cd spam-ai-detectornpm install
# or
yarn installCreate a .env.local file in the root directory:
NEXT_PUBLIC_OPEN_API_GITHUB_MODEL_TOKEN="your_github_token_here"
NEXT_PUBLIC_OPEN_API_GITHUB_MODEL_ENDPOINT="https://models.inference.ai.azure.com"- Go to GitHub Settings
- Navigate to Developer settings β Personal access tokens
- Create a new token with GitHub Models access
- Copy the token to your
.env.localfile
Development Mode:
npm run devProduction Build:
npm run build
npm startOpen http://localhost:3000 in your browser.
POST /api/analyze
// Request
{
"email": "Email content to analyze",
"detectorType": "basic" | "advanced" | "memory",
"compare": false
}
// Response
{
"success": true,
"data": {
"isSpam": boolean,
"reason": string,
"confidence": number,
"threatLevel": string,
"detectorUsed": string,
"analysisTime": number,
"additionalInfo": { ... }
}
}GET /api/analyze?action=stats
// Response
{
"status": "ok",
"service": "LangChain.js Spam AI Detector",
"version": "3.0.0",
"detectors": ["basic", "advanced", "memory"],
"memoryStats": { ... }
}GET /api/analyze?action=clear-cache
The application includes a modern, responsive web interface with:
- Detector Selection: Choose between Basic, Advanced, Memory, or Compare modes
- Real-time Analysis: Live spam detection with loading states
- Detailed Results: Comprehensive analysis breakdown
- Example Library: Pre-loaded test cases for different spam types
- Statistics Dashboard: Cache performance and memory usage
- Responsive Design: Works on desktop, tablet, and mobile
βββ src/
β βββ app/
β β βββ api/analyze/ # REST API endpoints
β β βββ globals.css # Global styles
β β βββ layout.tsx # App layout
β β βββ page.tsx # Main interface
β βββ lib/
β β βββ spam-detector-langchain.ts # Basic detector
β β βββ spam-detector-advanced.ts # Advanced detector
β β βββ spam-detector-memory.ts # Memory detector
β β βββ unified-spam-detector.ts # Unified API
β βββ index.ts # Legacy detector
βββ public/ # Static assets
βββ .env.local # Environment variables
βββ package.json # Dependencies
The application includes built-in test cases:
Hello John, I hope you're doing well. I'd like to follow up on our meeting yesterday about the project timeline...
CONGRATULATIONS!!! You WON $1,000,000 in our AMAZING lottery! CLICK HERE NOW to claim your prize...
Hello, I detected suspicious activity on your bank account. To protect your data, click this link immediately...
- Input Sanitization: Automatic removal of prompt injection attempts
- Rate Limiting: Built-in protection against abuse
- Error Handling: Graceful degradation on API failures
- Type Safety: Full TypeScript implementation
- Validation: Zod schemas for all inputs/outputs
| Detector | Avg Response Time | Memory Usage | Cache Hit Rate |
|---|---|---|---|
| Basic | ~1-2s | Low | N/A |
| Advanced | ~3-5s | Medium | N/A |
| Memory | ~1-3s | Medium | ~60-80% |
| Compare | ~5-8s | High | Mixed |
"Token not found"
- Verify your GitHub token in
.env.local - Ensure the token has GitHub Models access
"API Error" / Rate Limits
- Check your GitHub Models quota
- The app has built-in retry logic with exponential backoff
"NaN%" Confidence
- Fixed in latest version with proper validation
- Ensure you're using the updated detector files
Memory Detector Not Learning
- Clear cache using the API endpoint
- Check browser console for memory-related errors
Set NODE_ENV=development for detailed logging:
NODE_ENV=development npm run dev# Required
NEXT_PUBLIC_OPEN_API_GITHUB_MODEL_TOKEN=your_token
NEXT_PUBLIC_OPEN_API_GITHUB_MODEL_ENDPOINT=https://models.inference.ai.azure.com
# Optional
NODE_ENV=production- Database for analysis history
- User authentication
- Advanced rate limiting
- Monitoring and analytics
- Email integration
- Webhook support
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- LangChain.js team for the excellent framework
- GitHub Models for free AI inference
- Next.js team for the amazing React framework
- Tailwind CSS for beautiful, responsive design
Built with β€οΈ by Glaucia Lemos
Fighting spam, one email at a time π‘οΈ
