A production-ready AI search engine built with Go microservices architecture, featuring real-time BART model inference for intelligent search result summarization with both streaming and non-streaming modes.
- β Real AI Summarization: Facebook BART model with HuggingFace Transformers
- β Token-Native Processing: Industry-standard tokenization β inference β detokenization pipeline
- β Dual Response Modes: Streaming (real-time tokens) and non-streaming (complete summaries)
- β Server-Sent Events: Real-time search results followed by AI summaries
- β Production Architecture: Go orchestrator with Python ML services
- β Safety-First: Input validation and output sanitization
- β Monitoring Stack: Prometheus, Grafana, and comprehensive health checks
- β Apple Silicon Optimized: CPU-optimized PyTorch deployment for Mac development
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β Gateway βββββΆβ LLM Orch. βββββΆβ Tokenizer β
β (Go:8080) β β (Go:8086) β β(Python:8090)β
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β Search β β Inference β β Prometheus β
β (Go:8081) β β(Python:8083)β β(Monitoring) β
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββ βββββββββββββββ βββββββββββββββ
β Safety β β BART β β Grafana β
β (Go:8084) β β Model β β (Dashboard) β
βββββββββββββββ βββββββββββββββ βββββββββββββββ
- Gateway Service (Go, Port 8080): HTTP API with SSE streaming support
- LLM Orchestrator (Go, Port 8086): Coordinates token-native AI workflow
- Search Service (Go, Port 8081): Google Custom Search API integration
- Tokenizer Service (Python, Port 8090): BART tokenization and detokenization
- Inference Service (Python, Port 8083): BART model inference with PyTorch
- Safety Service (Go, Port 8084): Input validation and output sanitization
- Submit search query β Immediate response with search results
- AI summary appears first β Prominently displayed with gradient styling
- Source results below β Clean, secondary display for reference
- Submit search query β Real-time search results appear
- AI tokens stream live β Watch summary generate word-by-word
- Complete summary β Final sanitized output
- Go 1.21+
- Python 3.9+ with pip
- Docker & Docker Compose
- 8GB+ RAM (for BART model)
git clone <repository-url>
cd ai-summary-inference# Build Go services
make build
# Start everything with Docker Compose
docker-compose up -d
# Wait for services to initialize (BART model loading takes ~30s)
sleep 30- Web Interface: http://localhost:8080
- API Endpoint: http://localhost:8080/api/v1/search
- Monitoring: http://localhost:3000 (Grafana: admin/admin)
# Set environment variables for real search (optional)
export GOOGLE_API_KEY="your-api-key"
export GOOGLE_CX="your-custom-search-engine-id"
# Restart gateway to pick up credentials
docker-compose restart gatewayWithout Google API credentials, the system uses mock search data.
POST /api/v1/search
Content-Type: application/json
Accept: text/event-stream
{
"query": "machine learning algorithms",
"safe_search": true,
"num_results": 5
}Response: Server-Sent Events stream
event:status
data:{"type":"started","query":"machine learning algorithms"}
event:search_results
data:{"type":"search_results","results":[...]}
event:summary
data:{"type":"summary_complete","text":"AI summary here..."}
event:complete
data:{"type":"complete"}
POST /api/v1/search
Content-Type: application/json
{
"query": "artificial intelligence",
"safe_search": true,
"num_results": 3
}Response: Complete JSON with search results and AI summary
{
"query": "artificial intelligence",
"status": "completed",
"search_results": [...],
"summary": "AI-generated summary text..."
}GET /api/v1/search?query=python&streaming=true&safe_search=true&num_results=5
Accept: text/event-streamResponse: Real-time token streaming
event:search_results
data:{"type":"search_results","results":[...]}
event:token
data:{"type":"token","token":"Python","position":0}
event:token
data:{"type":"token","token":" is","position":1}
event:complete
data:{"type":"complete"}
# Build all Go services
make build
# Generate protocol buffers (if changed)
make proto
# Run tests
make test# Start core services
docker-compose up -d prometheus grafana
# Run Go services locally
./gateway &
./llm &
./search &
./safety &
# Python services need Docker for dependencies
docker-compose up -d python-tokenizer inference# Check service status
docker-compose ps
# View logs
docker-compose logs gateway
docker-compose logs inference
# Health checks
curl http://localhost:8080/health
curl http://localhost:8086/health- Text Input: "What is machine learning?"
- Tokenization: Text β Token IDs
[2061, 16, 3563, 2069, 116] - Inference: BART model processes token IDs β Generated token IDs
- Detokenization: Token IDs β Human-readable text summary
- Safety Check: Output sanitization and validation
- Client Display: Final summary with source results
- Model:
facebook/bart-large-cnn(406M parameters) - Framework: HuggingFace Transformers + PyTorch
- Device: CPU-optimized for Apple Silicon and x86
- Generation: Beam search with 4 beams, 20-150 tokens
- Optimization: Stable library versions to prevent device placement issues
- Cold Start: ~30 seconds (model loading)
- Inference Time: 2-8 seconds per summary (CPU)
- Concurrent Requests: 8 max per inference service
- Memory Usage: ~2GB per inference service
- Token Processing: ~50 tokens/second (streaming)
- Length Limits: 500 characters for search queries
- Content Filtering: Inappropriate content detection
- Injection Prevention: SQL/Command injection protection
- Rate Limiting: Concurrent request management (8 per service)
- Content Filtering: Dangerous pattern removal
- Length Limits: Summary truncation if needed
- HTML Escaping: XSS prevention
- Final Validation: Safety service approval required
- Prometheus: Metrics collection (http://localhost:9090)
- Grafana: Visualization dashboards (http://localhost:3000)
- cAdvisor: Container resource monitoring (http://localhost:8087)
- Health Endpoints: All services expose /health endpoints
# Request metrics
ai_search_requests_total{service="gateway",status="success"}
ai_search_request_duration_seconds{service="gateway",method="search"}
# AI-specific metrics
ai_search_llm_requests_total{service="orchestrator",model="bart"}
ai_search_tokenization_duration_seconds{service="tokenizer"}
ai_search_inference_duration_seconds{service="inference"}
# System metrics
ai_search_cpu_usage_percent{service="inference"}
ai_search_memory_usage_bytes{service="inference"}
- Service Health: Any service down > 1 minute
- High Latency: 95th percentile > 10 seconds
- Error Rate: >5% error rate sustained
- Resource Usage: CPU >80% or Memory >85%
1. BART Model Loading Errors
# Check inference service logs
docker-compose logs inference
# Common fix: Restart inference service
docker-compose restart inference2. Device Placement Issues
# Verify stable library versions in requirements.txt
transformers==4.35.2
torch==2.1.2
# Restart Python services
docker-compose restart python-tokenizer inference3. gRPC Connection Errors
# Check service connectivity
docker-compose exec gateway ./gateway --help
docker-compose exec llm ./llm --help
# Restart orchestrator
docker-compose restart llm4. Frontend Issues
# Check JavaScript console for errors
# Verify SSE connections in Network tab
# Test API directly:
curl -X POST http://localhost:8080/api/v1/search \
-H "Content-Type: application/json" \
-d '{"query":"test","safe_search":true,"num_results":3}'For Better Performance:
# Increase inference service replicas
docker-compose up -d --scale inference=2
# Monitor resource usage
docker stats
# Tune concurrent request limits in code:
# - orchestrator.go: maxConcurrentRequests
# - inference/main.py: max_concurrent_requestsai-summary-inference/
βββ cmd/ # Service entry points
β βββ gateway/main.go # HTTP API gateway
β βββ llm/main.go # LLM orchestration service
β βββ search/main.go # Search service
β βββ safety/main.go # Safety validation service
β βββ tokenizer-python/main.py # BART tokenization service
β βββ inference-python/main.py # BART inference service
βββ internal/ # Internal Go packages
β βββ config/ # Configuration management
β βββ gateway/ # Gateway implementation
β βββ logger/ # Logging utilities
β βββ monitoring/ # Metrics collection
β βββ services/ # Service implementations
βββ proto/ # Protocol buffer definitions
β βββ search.proto # Service contracts
β βββ search.pb.go # Generated Go code
β βββ search_pb2.py # Generated Python code
βββ web/ # Frontend resources
β βββ templates/index.html # Main web interface
β βββ static/ # CSS, JS, images
βββ monitoring/ # Monitoring configuration
β βββ prometheus.yml # Prometheus config
β βββ grafana/ # Grafana dashboards
βββ docker-compose.yml # Local development setup
βββ Makefile # Build automation
βββ config.yaml # Service configuration
βββ README.md # This file
# Full stack with monitoring
docker-compose up -d
# Application only
docker-compose up -d gateway llm search safety tokenizer inference- Container Orchestration: Kubernetes deployment ready
- Load Balancing: Multiple gateway replicas
- Resource Allocation: Separate CPU/GPU node pools
- Monitoring: External Prometheus/Grafana cluster
- Secrets Management: External secret stores
- Service Coordination: Efficient request routing and load balancing
- Gateway: Scale horizontally based on request volume
- LLM Orchestrator: Scale based on coordination overhead
- Tokenizer: Scale based on CPU usage (CPU-bound)
- Inference: Scale based on model capacity (memory-bound)
- Search/Safety: Scale based on API rate limits
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Ensure all services pass health checks
- Submit a pull request
- Follow Go best practices and gofmt
- Add comprehensive error handling
- Include unit tests for new functions
- Update documentation for API changes
- Test with both streaming and non-streaming modes
This project is licensed under the MIT License - see the LICENSE file for details.
This system demonstrates:
- Microservices Architecture: Proper service separation and communication
- AI/ML Integration: Real transformer model deployment
- Streaming Architecture: Modern real-time response patterns
- Observability: Comprehensive monitoring and logging
- Safety Engineering: Input validation and output sanitization
- Performance Optimization: Efficient resource utilization
- Fault Tolerance: Graceful error handling and recovery
Perfect for showcasing modern AI infrastructure engineering capabilities.
