跳到正文
DEV Community· StellarDev·· 37 分鐘前AI 評分55

Stellar 的 Discord 機械人如何在純 CPU VPS 上運行自託管 AI,並以執行緒設定提升 70% 速度

Running a self-hosted AI for a Discord bot on a CPU-only VPS (and the thread trick that made it 70% faster)

AI 導讀

Stellar 作者在純 CPU VPS 上以 Ollama 運行 Qwen 2.5 3B,並把執行緒數設為 4、配合 4 核 CPU 配額,使生成速度由 5.1 提升至 8.6 tokens/s。

正文

StellarDev

I wanted my Discord bot, Stellar, to answer questions with AI: a /ask command, plus a first reply when someone opens a support ticket. I didn't want to pay per message or send people's questions to a third party. So the model runs on the same 6-core VPS as the bot. No GPU.

The setup

  • Ollama running Qwen 2.5 3B, reachable only from the bot's Docker network, never from the internet.
  • Capped at 4 of the 6 cores (systemd CPUQuota=400%), so the bot itself stays fast.
  • Answers stream into Discord as they're written, editing one message every second. People see text within a couple of seconds even though a full answer takes 10–15 seconds.

The thread trick

Ollama starts one thread per core by default. With the CPU capped at 4 cores, 6 threads were fighting over 4 cores. Setting num_thread to match the cap:

options: { num_predict: 200, temperature: 0.3, num_ctx: 2048, num_thread: 4 }

took generation from 5.1 to 8.6 tokens a second, without giving it any more CPU.

Small models and other languages

A 3B model answering Polish or Spanish directly gave vague or made-up answers, and its knowledge base (the bot's command list) is in English anyway. So questions go through three steps:

  1. The model names the language and translates the question to English.
  2. It answers in English, with the right facts from the command list.
  3. It translates the answer back.

In tickets, the English copy stays underneath so staff can read it. Polish answers went from useless to correct.

Keeping it fair on one CPU

  • One answer at a time, with a short queue.
  • A 15-second cooldown per user and a daily cap per server.
  • Nothing is stored: only the answer's length and how long it took are logged.

If you run a Discord server, Stellar is free: https://stellarbot.xyz. Questions about the setup are welcome in the comments.

來源:DEV Community · dev.to