Hermes Agent 透過 Telegram 切換家用伺服器上的 Gemma 與 Inkling
Running Two Local GGUF Models on a Home Server, Switched from Telegram
作者在家用伺服器以 llama.cpp 運行 Gemma 與 Inkling,並設定 Hermes Agent 透過 Telegram 的 /model 選單切換兩個本地 GGUF 模型。
Background
I run a personal AI assistant (Hermes Agent) on a VPS. The local models run on a home server with two GPUs: an RTX 3060 (12 GB) and an RTX 3070 (8 GB). The 3070 mines when idle. The 3060 normally serves a local Gemma model through llama.cpp. When the home server is off or unreachable, the assistant falls back to OpenRouter.
This post covers how I added a second local model (Inkling-2-Mini Uncensored, IQ2_M quantization, about 8.7 GB) and made it possible to switch between the two models from Telegram.
Goals
• Pick Gemma or Inkling from Telegram's /model menu
• Serve both models from the same llama.cpp server on port 8080
• Keep a 64k context, which the assistant needs for summarization
• Keep mining idle-only, and give the LLM priority whenever it is needed
What Went Wrong (and Why)
- The build ran out of memory
Running cmake --build with -j and no number runs unlimited parallel jobs. CUDA compilation is memory-heavy, so the kernel's OOM killer stopped the compiler. The fix was an explicit job count (-j 1) and a background build with nohup.
Lesson: A bare -j does not mean "use all cores." It means unlimited.
- cudaMalloc failed with plenty of free VRAM
The 3060 showed about 11.9 GB free, yet loading an 8 GB model failed. I tried several wrong fixes first: batch size, multimodal offload, and locked-memory limits.
The real cause was that the production service set CUDA_DEVICE_ORDER=PCI_BUS_ID, but my interactive shell did not. Without it, CUDA numbers GPUs by speed, so CUDA_VISIBLE_DEVICES=0 pointed at the 3070. nvidia-smi lists GPUs in PCI order, which made the mismatch hard to see.
Lesson: When CUDA reports out of memory on a device that has free memory, first check which physical GPU the process actually uses.
- The model list shows only the running model
In Telegram, /model for a custom provider lists the models returned by the endpoint's /v1/models. The server runs one model at a time, so the list showed only whichever model was loaded. That looked odd next to the OpenRouter list, which is a fixed catalog.
Reading the Hermes source showed that a custom provider's list comes from /models unless you set discover_models: false. With that flag, Hermes uses the models: dictionary in config.yaml.
Entries that share the same URL are also merged into one picker row. So I put both models under a single provider entry.
Lesson: For a fixed picker list on a custom provider, use discover_models: false with an explicit models: dictionary.
- A 64k context needs more VRAM than the 3060 had
Hermes requires at least 64,000 tokens of context for summarization. Inkling at 8k could not handle that step.
Running Inkling at 64k on the 3060 took two changes:
• Batch size -b 128 and micro-batch -ub 128, to shrink the compute buffer
• Quantized KV cache (--cache-type-k q8_0 --cache-type-v q8_0), to shrink the KV cache
Inkling then ran at about 11.3 GB with a 64k context. Only about 1 GB stayed free, so long conversations are the next thing to watch.
- Hidden restarts and timers
Several things start or stop the LLM server without a manual command:
• An idle monitor switches the 3060 to mining after 10 minutes without GPU load
• A wake hook starts the server before each model call (by design, so the LLM takes priority over mining)
• systemd restarts a failing service
I disabled the idle timer during tests and re-enabled it afterward.
Final Architecture
• One llama-server unit on port 8080. Model arguments come from an environment file (gemma.env or inkling.env).
• One binary (llama.cpp build 12652) for both models.
• A root-owned switch script that restarts the server only when the requested model differs from the running one.
• A restricted SSH command channel that the assistant uses to call wake-llama or wake-inkling.
• A per-turn wake plugin that picks the wake command from the model path in config.yaml.
Provider Configuration Example
Here is a sanitized example of the config.yaml sections that make the picker and the switching work. Replace the placeholders with your own values. Do not put API keys in the file. Use an environment variable instead.
model:
default: /models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf # the model in use now
provider: custom:home-server # the provider name shown in /model
base_url: http://:8080/v1
context_length: 65536 # global default (lost on /model switch)
providers:
custom:home-server:
base_url: http://:8080/v1
key_env: OPENAI_API_KEY # key comes from the environment, not from this file
default_model: /models/gemma4-12b-qat-q4_k_m.gguf # listed first in the picker
discover_models: false # do not ask the server for its model list
models:
/models/gemma4-12b-qat-q4_k_m.gguf:
context_length: 65536
/models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf:
context_length: 65536
request_timeout_seconds: 120
custom_providers: # per-model overrides that survive /model switches
- name: home-server base_url: http://:8080/v1 discover_models: false models: /models/gemma4-12b-qat-q4_k_m.gguf: context_length: 65536 /models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf: context_length: 65536
fallback_providers:
- provider: openrouter model: deepseek/deepseek-v4-flash-0731
- provider: openrouter model: openrouter/free
What each key does
• providers.custom:.discover_models: false stops Hermes from asking the server for its model list. The picker then shows only the models: you wrote.
• models: is a dictionary keyed by model path. The first entry in the list comes from default_model.
• custom_providers with the same URL is merged into the same picker row. Use it for per-model context_length, because model.context_length alone is lost after a /model switch.
• key_env names an environment variable that holds the key. The key itself stays out of the YAML.
How to Switch Models from Telegram
- Open the menu. Send /model to the bot. It shows the current model and a list of providers.
- Pick the provider. Choose custom:home-server from the provider buttons. It appears as a single button.
- Pick a model. The model screen shows both local models as separate buttons: Gemma and Inkling. Choose one. Hermes saves the choice to config.yaml (model.default and model.provider).
- Send a message. The first message after a switch triggers the wake hook, which tells the home server to load the chosen model. Loading takes roughly 30 to 60 seconds. During that time, the assistant may answer through OpenRouter.
- Check the result. On the server, confirm which model is active and how much VRAM it uses: readlink /home//llama-models/active.env nvidia-smi --query-gpu=index,memory.used --format=csv ◦ gemma.env with about 9.2 GB on the 3060 means Gemma. ◦ inkling.env with about 11.3 GB on the 3060 means Inkling.
Notes
• Pressing Cancel leaves the current setting unchanged.
• Choosing a model only records it. The model loads on the next message.
• If the home server is off, the assistant falls back to OpenRouter.
• Keep the menu's selection and the server's active model in agreement. If they differ, the wrong model can answer or the request can fail.
Takeaways
• Read the source of your tools before guessing. I spent hours tuning VRAM settings, but the picker problem was solved in minutes once I read the code.
• Keep "what the config says" and "what is running now" separate in your head, and check both.
• Back up before every config change, and keep a restore command next to each change.
--
Telegram からモデルを切り替える、自宅サーバーでのローカルLLM運用
背景
VPS で AI アシスタント(Hermes Agent)を動かしています。ローカルの LLM は、自宅サーバーの2枚の GPU(RTX 3060 12GB と RTX 3070 8GB)で動かしています。3070 はアイドル時に採掘に使い、3060 は通常、llama.cpp で Gemma を動かします。自宅サーバーに繋がらないときは、OpenRouter に自動で切り替わります。
この記事では、2つ目のローカルモデル(Inkling-2-Mini Uncensored、IQ2_M、約8.7GB)を追加して、Telegram から2つのモデルを切り替えられるようにした話をまとめます。
目的
• Telegram の /model から、Gemma と Inkling を選べるようにする
• 両方を、同じ llama.cpp サーバー(8080番)で動かす
• アシスタントが要約に使う、64k のコンテキストを確保する
• マイニングは隙間時間だけ。LLM が必要なときは LLM を優先する
つまずき(と原因)
- ビルドがメモリ不足で止まった
cmake --build で -j に数字を付けずに実行すると、並列数が無制限になります。CUDA のコンパイルはメモリを多く使うので、カーネルの OOM キラーがコンパイラを止めました。-j 1 と明示し、nohup でバックグラウンド実行して解決しました。
教訓: -j の後の数字は省略できません。省略すると「全コア使用」ではなく「無制限」になります。
- 空きVRAMがあるのに cudaMalloc failed
3060 に約11.9GBの空きがあるのに、8GBのモデルの読み込みが失敗しました。バッチサイズや mmproj、ロック可能メモリの上限を疑いましたが、どれも外れでした。
本当の原因は、サービス(systemd)には CUDA_DEVICE_ORDER=PCI_BUS_ID が設定されていたのに、手作業のシェルには設定されていなかったことです。この変数がないと、CUDA は GPU を速い順に番号付けします。その結果、CUDA_VISIBLE_DEVICES=0 が 3060 ではなく 3070 を指していました。nvidia-smi は PCI 順で表示するので、食い違いに気づきにくかったです。
教訓: CUDA の out of memory が出たら、パラメータを調整する前に、プロセスが実際にどの物理GPUを使っているかを確認しましょう。
- モデル一覧に「今動いているモデル」しか出ない
Telegram で custom provider の /model を開くと、エンドポイントの /v1/models が返すモデルが一覧に出ます。サーバーは同時に1つのモデルしか動かさないので、一覧には今読み込まれているモデルしか出ませんでした。OpenRouter の一覧は固定なので、この違いが気になりました。
Hermes のソースを読むと、custom provider の一覧は discover_models: false を設定しない限り /models から作られることが分かりました。false にすると、config.yaml の models:(モデル名をキーにした辞書)をそのまま一覧に使います。
同じURLの entry は、1つの行にまとめられることも分かりました。そこで、Gemma と Inkling を、1つの provider の中に入れることにしました。
教訓: custom provider で一覧を固定するには、discover_models: false と、明示的な models: の辞書を使います。
- 64k のコンテキストは、3060 には重かった
Hermes は、要約(圧縮)のために少なくとも 64,000 トークンのコンテキストを必要とします。8k で動かしていた Inkling では、この条件を満たせませんでした。
Inkling を 64k で3060に載せるには、次の2つを組み合わせました。
• バッチサイズ -b 128 と、マイクロバッチ -ub 128(計算用バッファを小さくする)
• KV キャッシュの量子化 --cache-type-k q8_0 --cache-type-v q8_0(KV の領域を小さくする)
結果として、Inkling は 64k で約11.3GB を使って動きました。空きは1GB程度しかないので、長い会話をしたときは注意が必要です。
- 勝手に止まる・起きる仕組み
LLM のサーバーは、次の仕組みで自動的に止まったり起きたりします。
• アイドル監視:GPU の負荷が10分間ないと、3060 をマイニングに切り替える
• wake フック:LLM を呼ぶ前に、サーバーを起こす(LLM を採掘より優先するため、毎ターン送っています)
• systemd:失敗したサービスを再起動する
テスト中は、アイドル監視のタイマーを止めておき、終わったら戻しました。
最終的な構成
• llama-server の unit は1つ(8080番)。モデルの引数は、環境ファイル(gemma.env / inkling.env)から読む
• バイナリは1つ(llama.cpp の build 12652)で、両方のモデルを動かす
• root 所有の切り替えスクリプトが、要求されたモデルと今のモデルが違うときだけ、サーバーを止めて起動し直す
• 制限された SSH コマンドから、wake-llama か wake-inkling を呼ぶ
• 毎ターンの wake プラグインが、config.yaml のモデルのパスから、どちらを呼ぶかを決める
プロバイダー設定の例(YAML)
Telegram の一覧と切り替えを動かしている config.yaml の部分を、伏せた形で載せます。 などは、自分の値に置き換えてください。APIキーはファイルに書かず、環境変数(key_env)で渡します。
model:
default: /models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf # 今使っているモデル
provider: custom:home-server # /model に出る provider 名
base_url: http://:8080/v1
context_length: 65536 # 全体の既定値(/model で切り替えると消える)
providers:
custom:home-server:
base_url: http://:8080/v1
key_env: OPENAI_API_KEY # キーは環境変数から読む。ファイルには書かない
default_model: /models/gemma4-12b-qat-q4_k_m.gguf # 一覧の先頭に出る
discover_models: false # サーバーにモデル一覧を聞きに行かない
models:
/models/gemma4-12b-qat-q4_k_m.gguf:
context_length: 65536
/models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf:
context_length: 65536
request_timeout_seconds: 120
custom_providers: # /model の切り替え後も残る、モデルごとの設定
- name: home-server base_url: http://:8080/v1 discover_models: false models: /models/gemma4-12b-qat-q4_k_m.gguf: context_length: 65536 /models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf: context_length: 65536
fallback_providers:
- provider: openrouter model: deepseek/deepseek-v4-flash-0731
- provider: openrouter model: openrouter/free
各項目の意味
• providers.custom:<名前>.discover_models: false:サーバーにモデル一覧を聞きに行かず、models: に書いたものだけを一覧に出す。
• models: はモデルのパスをキーにした辞書。一覧の先頭は default_model で決まる。
• 同じURLの custom_providers は、同じ行にまとめられる。モデルごとの context_length は、ここに書くと /model で切り替えても残る。
• key_env は、キーが入った環境変数の名前を指定する。キーそのものは YAML に書かない。
Telegram からモデルを切り替える方法
- メニューを開く:ボットに /model と送ります。現在のモデルと、provider の一覧が出ます。
- provider を選ぶ:custom:home-server を選びます。ローカルの2つのモデル(Gemma と Inkling)が並びます。
- モデルを選ぶ:Gemma か Inkling を選びます。Hermes は選んだ内容を config.yaml(model.default と model.provider)に保存します。
- プロバイダーを選ぶ:プロバイダーのボタン一覧から custom:home-server を押します。ここでは1つのボタンとして表示されます。
- モデルを選ぶ:押したあとの画面に、ローカルの2つのモデル(Gemma と Inkling)が別々のボタンで並びます。どちらかを押すと、Hermes が選んだ内容を config.yaml(model.default と model.provider)に保存します。
- メッセージを送る:切り替え後の最初のメッセージで、wake フックが走り、自宅サーバーが選んだモデルを読み込みます。読み込みには30〜60秒ほどかかり、その間は OpenRouter で応答することがあります。
- 結果を確認する:サーバーで、今のモデルと VRAM の使用量を確認します。 readlink /home/<ユーザー名>/llama-models/active.env nvidia-smi --query-gpu=index,memory.used --format=csv ◦ gemma.env で、3060 が約9.2GB なら Gemma です。 ◦ inkling.env で、3060 が約11.3GB なら Inkling です。
補足
• Cancel を押すと、設定は変わりません。
• モデルを選んだだけでは、読み込まれません。読み込まれるのは、次のメッセージを送ったときです。
• 自宅サーバーが落ちているときは、OpenRouter に自動で切り替わります。
• メニューで選んだモデルと、サーバーで今動いているモデルは、必ず揃えておきましょう。ずれると、違うモデルが応答したり、リクエストが失敗したりします。
まとめ
• ツールの動きは、推測よりソースを読むほうが早いです。VRAM の調整に何時間もかけましたが、一覧の問題は、コードを読んだら数分で分かりました。
• 「設定に書いてあること」と「今動いていること」は、分けて考えて、両方を確認しましょう。
• 設定を変えるたびにバックアップを取り、元に戻すコマンドも用意しておきましょう。
來源:DEV Community · dev.to