This guide covers deploying the Cognitive Engine in various environments.
The Cognitive Engine can be deployed in various configurations:
# Clone repository
git clone <repository-url>
cd cognitive_engine
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Edit .env with your settings
# Run tests
python run.py test
# Start engine
python run.py
# Install system dependencies
sudo apt-get update
sudo apt-get install python3.10 python3.10-venv sqlite3
# Create dedicated user
sudo useradd -r -s /bin/false cognitive_engine
# Set up directory structure
sudo mkdir -p /opt/cognitive_engine
sudo chown cognitive_engine:cognitive_engine /opt/cognitive_engine
# Install application
cd /opt/cognitive_engine
sudo -u cognitive_engine python3.10 -m venv venv
sudo -u cognitive_engine venv/bin/pip install -r requirements.txt
# Configure systemd service
sudo tee /etc/systemd/system/cognitive-engine.service > /dev/null <<EOF
[Unit]
Description=Cognitive Engine Service
After=network.target
[Service]
Type=simple
User=cognitive_engine
WorkingDirectory=/opt/cognitive_engine
Environment="PATH=/opt/cognitive_engine/venv/bin"
ExecStart=/opt/cognitive_engine/venv/bin/python run.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
# Enable and start service
sudo systemctl enable cognitive-engine
sudo systemctl start cognitive-engine
Create a Dockerfile in the project root:
FROM python:3.10-slim
# Set working directory
WORKDIR /app
# Install system dependencies
RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*
# Copy requirements first for caching
COPY requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Create non-root user
RUN useradd -m -u 1000 cognitive_engine && \
chown -R cognitive_engine:cognitive_engine /app
USER cognitive_engine
# Expose dashboard port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD python -c "import requests; requests.get('http://localhost:8000/health')" || exit 1
# Run application
CMD ["python", "run.py"]
Create a docker-compose.yml for easy deployment:
version: '3.8'
services:
cognitive-engine:
build: .
container_name: cognitive-engine
restart: unless-stopped
ports:
- "8000:8000"
environment:
- OPENAI_API_KEY=${OPENAI_API_KEY}
- ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
- DEFAULT_LLM_PROVIDER=${DEFAULT_LLM_PROVIDER:-openai}
- MIN_ITERATIONS=${MIN_ITERATIONS:-3}
- MAX_ITERATIONS=${MAX_ITERATIONS:-50}
- ENABLE_DASHBOARD=${ENABLE_DASHBOARD:-true}
- DASHBOARD_PORT=${DASHBOARD_PORT:-8000}
- LOG_LEVEL=${LOG_LEVEL:-INFO}
volumes:
- ./data:/app/data
- ./cognitive_engine.db:/app/cognitive_engine.db
- ./cognitive_engine.log:/app/cognitive_engine.log
networks:
- cognitive-network
healthcheck:
test: ["CMD", "python", "-c", "import requests; requests.get('http://localhost:8000/health')"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
networks:
cognitive-network:
driver: bridge
# Build image
docker build -t cognitive-engine:latest .
# Run with docker-compose
docker-compose up -d
# Run standalone
docker run -d \
--name cognitive-engine \
-p 8000:8000 \
-e OPENAI_API_KEY=your_key \
-e ANTHROPIC_API_KEY=your_key \
-v $(pwd)/data:/app/data \
cognitive-engine:latest
# View logs
docker logs -f cognitive-engine
# Stop container
docker stop cognitive-engine
docker rm cognitive-engine
Storage: 20 GB GP2
Security Groups
Outbound: All traffic
Deploy Application
:::bash
ssh -i your-key.pem ubuntu@your-instance-ip
sudo apt-get update
sudo apt-get install docker.io docker-compose
git clone <repository-url>
cd cognitive_engine</repository-url>
cp .env.example .env
nano .env # Add your API keys
docker-compose up -d
Create ECR Repository
:::bash
aws ecr create-repository --repository-name cognitive-engine
Build and Push Image
:::bash
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account-id>.dkr.ecr.us-east-1.amazonaws.com</account-id>
docker build -t cognitive-engine .
docker tag cognitive-engine:latest <account-id>.dkr.ecr.us-east-1.amazonaws.com/cognitive-engine:latest</account-id>
docker push <account-id>.dkr.ecr.us-east-1.amazonaws.com/cognitive-engine:latest</account-id>
Create Task Definition
:::json
{
"family": "cognitive-engine",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "512",
"memory": "1024",
"containerDefinitions": [
{
"name": "cognitive-engine",
"image": "<account-id>.dkr.ecr.us-east-1.amazonaws.com/cognitive-engine:latest",
"portMappings": [
{
"containerPort": 8000,
"protocol": "tcp"
}
],
"environment": [
{
"name": "OPENAI_API_KEY",
"value": "your-key"
},
{
"name": "ANTHROPIC_API_KEY",
"value": "your-key"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/cognitive-engine",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
}
}
]
}</account-id>
# Build and deploy
gcloud builds submit --tag gcr.io/PROJECT_ID/cognitive-engine
# Deploy to Cloud Run
gcloud run deploy cognitive-engine \
--image gcr.io/PROJECT_ID/cognitive-engine \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--set-env-vars OPENAI_API_KEY=your_key,ANTHROPIC_API_KEY=your_key
# k8s/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: cognitive-engine
spec:
replicas: 2
selector:
matchLabels:
app: cognitive-engine
template:
metadata:
labels:
app: cognitive-engine
spec:
containers:
- name: cognitive-engine
image: gcr.io/PROJECT_ID/cognitive-engine:latest
ports:
- containerPort: 8000
env:
- name: OPENAI_API_KEY
valueFrom:
secretKeyRef:
name: api-keys
key: openai-key
- name: ANTHROPIC_API_KEY
valueFrom:
secretKeyRef:
name: api-keys
key: anthropic-key
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "1Gi"
cpu: "1000m"
:::bash
# Apply deployment
kubectl apply -f k8s/deployment.yaml
# Create service
kubectl expose deployment cognitive-engine --type=LoadBalancer --port 8000
# Create resource group
az group create --name cognitive-engine-rg --location eastus
# Create container instance
az container create \
--resource-group cognitive-engine-rg \
--name cognitive-engine \
--image cognitive-engine:latest \
--cpu 2 \
--memory 4 \
--ports 8000 \
--environment-variables \
OPENAI_API_KEY=your_key \
ANTHROPIC_API_KEY=your_key
# Add to dashboard/server.py
@app.get("/health")
async def health_check():
"""Health check endpoint."""
return {
"status": "healthy",
"timestamp": datetime.now().isoformat(),
"version": "1.0.0"
}
Network I/O
Application Metrics
Active connections
Cognitive Metrics
Configure centralized logging:
# Use structured logging
import json
import logging
class JsonFormatter(logging.Formatter):
def format(self, record):
log_obj = {
"timestamp": self.formatTime(record),
"level": record.levelname,
"message": record.getMessage(),
"module": record.name,
"line": record.lineno
}
if hasattr(record, 'extra'):
log_obj.update(record.extra)
return json.dumps(log_obj)
Set up alerts for:
Run multiple instances behind a load balancer:
# docker-compose.yml for scaling
version: '3.8'
services:
cognitive-engine:
image: cognitive-engine:latest
deploy:
replicas: 3
environment:
- ENABLE_DASHBOARD=false # Disable dashboard on workers
Increase resources for single instance:
services:
cognitive-engine:
deploy:
resources:
limits:
cpus: '4'
memory: 8G
reservations:
cpus: '2'
memory: 4G
For high-volume deployments:
Implement caching for:
Pattern extraction results
:::python
import redis
redis_client = redis.Redis(host='localhost', port=6379, db=0)
def cached_llm_call(prompt):
cache_key = f"llm:{hash(prompt)}"
cached = redis_client.get(cache_key)
if cached:
return cached.decode('utf-8')
result = llm_client.generate(prompt)
redis_client.setex(cache_key, 3600, result)
return result
# Automated backup script
#!/bin/bash
BACKUP_DIR="/backups/cognitive-engine"
DATE=$(date +%Y%m%d_%H%M%S)
DB_PATH="/app/cognitive_engine.db"
mkdir -p $BACKUP_DIR
# Backup database
cp $DB_PATH $BACKUP_DIR/cognitive_engine_$DATE.db
# Compress
gzip $BACKUP_DIR/cognitive_engine_$DATE.db
# Keep last 7 days
find $BACKUP_DIR -name "*.db.gz" -mtime +7 -delete
# Backup environment configuration
cp .env $BACKUP_DIR/.env_$DATE.backup
Symptoms: Container exits immediately or won't start
Solutions:
docker logs <container-name>Symptoms: Memory usage grows continuously
Solutions:
Symptoms: Slow response times
Solutions:
Symptoms: API calls failing with rate limit errors
Solutions:
For deployment issues:
cognitive_engine.log