Introduction
Running AI models on a CPU‑only virtual private server (VPS) used to be considered impractical for real‑time chat bots. Recent community experiments show that with careful resource management, a 6‑core VPS can power a responsive Discord bot without any GPU.
Self‑hosted AI refers to running a machine‑learning model on your own infrastructure rather than relying on third‑party APIs.
Why Choose a Self‑Hosted CPU‑Only Solution?
Self‑hosting eliminates per‑message costs, protects user privacy, and gives you full control over model updates. A CPU‑only VPS is significantly cheaper than a GPU instance, making it attractive for startups and SMEs that need to manage budgets tightly.
Setting Up Ollama with Qwen 2.5 3B on Docker
Ollama provides a lightweight container that serves LLMs locally. Follow these steps to get Qwen 2.5 (3 billion parameters) up and running:
- Install Docker on your VPS.
- Pull the Ollama image: docker pull ollama/ollama.
- Create a dedicated Docker network so the bot can communicate with the model but the internet cannot reach it.
- Run the container with the model: docker run -d --name qwen --network botnet -p 11434:11434 ollama/ollama serve --model qwen2.5‑3b.
By isolating the model container, you reduce attack surface and keep API calls internal.
Optimizing CPU Usage: The Thread Trick
The default Ollama configuration spawns a thread for each CPU core, which can lead to contention with the Discord bot process. Limiting Ollama to four cores using systemd’s CPUQuota=400% keeps the bot responsive. The real gain comes from adjusting the internal thread pool:
- Set OMP_NUM_THREADS=2 inside the container to allocate two threads per core.
- Enable numactl --interleave=all to balance memory access across CPUs.
- Result: response latency drops by about 70% compared to the unmodified setup.
Integrating the Model with Your Discord Bot
With the model exposed on localhost:11434, the bot can issue HTTP POST requests to the Ollama API. A typical flow:
- User invokes
/askin Discord. - Bot forwards the prompt to
http://localhost:11434/api/generate. - Model streams the answer back; the bot relays the text to the Discord channel.
- For support tickets, the bot sends an initial greeting using the same endpoint.
Because the model runs on the same VPS, round‑trip latency stays under 300 ms, delivering a smooth user experience.
Best Practices & Cost Considerations
To keep the VPS affordable and stable, follow these guidelines:
- Monitor CPU and memory usage with htop or Prometheus.
- Set resource limits in systemd for both the bot and the model containers.
- Schedule nightly restarts of the Ollama container to clear memory fragmentation.
- Choose a VPS provider that offers burstable CPU credits if your traffic spikes.
Frequently Asked Questions
Can I run larger models on a CPU‑only VPS?
You can, but performance degrades quickly. Models above 7 B parameters often require more than 8 cores or a GPU to stay responsive.
Is the thread trick safe for production?
Yes. Adjusting OMP_NUM_THREADS only changes how the model utilizes existing cores; it does not compromise stability when combined with proper systemd limits.
Do I need to expose the Ollama API to the internet?
No. Keep the API bound to the Docker network or localhost. This prevents unauthorized access and aligns with data‑privacy best practices.
How do I handle multiple concurrent Discord requests?
Implement a request queue in your bot logic. Since the model processes one prompt at a time per thread pool, queuing ensures orderly handling without overloading the CPU.
What if I need GPU acceleration later?
Ollama supports GPU backends. Migrating involves swapping the container image and updating the hardware driver, while keeping your bot integration unchanged.
Neptune Infotech can help you design, deploy, and fine‑tune self‑hosted AI solutions that scale with your business needs.