Open3

OpenAI Codex / Claude codeでLocal LLMを実行

はじ環はじ環

方針

unslothのチュートリアルに従ってGemma4のモデルを使って試してみる。

Install llama.cpp

llama.cppの

# Install dependency
sudo apt-get update
sudo apt-get install pciutils build-essential cmake curl libcurl4-openssl-dev git-all -y

# Cllone
git clone https://github.com/ggml-org/llama.cpp

cmake llama.cpp -B llama.cpp/build -DBUILD_SHARED_LIBS=OFF -DGGML_CUDA=ON
cmake --build llama.cpp/build --config Release -j --clean-first --target llama-cli llama-mtmd-cli llama-server llama-gguf-split

# Copy
cp llama.cpp/build/bin/llama-* llama.cpp
# パスが通っている場所においてもいい
# 例: cp llama.cpp/build/bin/llama-* ~/.local/bin

Donwlaod model

モデルは16GBのVRAMに収まる8bit量子化モデルを使用する。

aria2c -c -x 4 -s 4 -j 4 https://huggingface.co/unsloth/gemma-4-E4B-it-GGUF/resolve/main/gemma-4-E4B-it-Q8_0.gguf -o gemma-4-E4B-it-Q8_0.gguf

Llama-serverを起動

gemma-4-E4B-itをデプロイするために、llama-serverを使用する。パスの通っているディレクトリにllama-serverコマンドがある前提で下記を実行。

llama-server \
    --model gemma-4-E4B-it-Q8_0.gguf \
    --alias "gemma-4-E4B-it" \
    --port 8001 \
    --kv-unified \
    --temp 1.0 \
    --top-p 0.95 \
    --top-k 64 \
    --chat-template-kwargs '{"enable_thinking":true}' \
    --flash-attn on \
    --batch-size 4096 --ubatch-size 1024 \
    --ctx-size 131072

OpenAI Codex CLIの設定と実行

config.tomlにmodelとしてデプロイしているLLMの設定を追記

[model_providers.llama_cpp]
name = "llama_cpp API"
base_url = "http://localhost:8001/v1"
wire_api = "responses"
stream_idle_timeout_ms = 10000000

フォルダ作成して実行

mkdir local_llm_codex ; cd local_llm_codex
codex --model gemma-4-E4B-it -c model_provider=llama_cpp --search

とここまで進めて、以下のエラー。

srv    operator(): got exception: {"error":{"code":400,"message":"'type' of tool must be 'function'","type":"invalid_request_error"}}
srv  log_server_r: done request: POST /v1/responses 127.0.0.1 400

llama.cpp の OpenAI 互換 Responses 実装が Codex 側の期待とズレていることが原因の模様。Codex CLIを0.87.0に戻せばいいらしいが、その選択はないのでllama.cppベースではないLM studioでのdeployを試してみる。

はじ環はじ環

LM Studio

llama.cppからLM Studioに変更して再チャレンジ

Install LM Studio

LM Studio の headless daemon (llmster) と lmsで組んでいく。

curl -fsSL https://lmstudio.ai/install.sh | bash
lms --version
lms version
   __   __  ___  ______          ___        _______   ____
  / /  /  |/  / / __/ /___ _____/ (_)__    / ___/ /  /  _/
 / /__/ /|_/ / _\ \/ __/ // / _  / / _ \  / /__/ /___/ /
/____/_/  /_/ /___/\__/\_,_/\_,_/_/\___/  \___/____/___/

lms is LM Studio's CLI utility for your models, server, and inference runtime.
CLI commit: 8d3f370

Docs: https://lmstudio.ai/docs/developer
Join our Discord: https://discord.gg/lmstudio
Contribute: https://github.com/lmstudio-ai/lms

helpを確認

lms --help
Usage: lms [options] [command]

Local models
   chat               Start an interactive chat with a model
   get                Search and download local models or presets
   load               Load a model
   unload             Unload a model
   ls                 List the models available on disk
   ps                 List the models currently loaded in memory
   import             Import a model file into LM Studio

Serve
   server             Commands for managing the local server
   log                Log incoming and outgoing messages

Remote Instances
   link               Commands for managing LM Link

Runtime
   runtime            Manage and update the inference runtime

Develop & Publish (Beta)
   clone              Clone an artifact from LM Studio Hub to a local folder
   push               Uploads the artifact in the current folder to LM Studio Hub
   dev                Starts a plugin dev server in the current folder
   login              Authenticate with LM Studio
   logout             Log out of LM Studio
   whoami             Check the current authentication status

Learn more:           https://lmstudio.ai/docs/developer
Join our Discord:     https://discord.gg/lmstudio

daemon を起動

lms daemon up

常駐確認

lms status
Server: ON (port: 1234)

No Models Loaded

モデルのロード

Gemma 4のGGUFは先程ダウンロードしたものを使用。

モデルのインポート

lms import ~/.llm/models/gemma-4-E4B-it-Q8_0.gguf
 Choose categorization option Auto search Hugging Face (Recommended for models downloaded from Hugging Face)
Searching for the model on Hugging Face using the file name...
Found the following repositories on Hugging Face containing this file:
 Please select the correct one | unsloth/gemma-4-E4B-it-GGUF
File moved to ~/.lmstudio/models/unsloth/gemma-4-E4B-it-GGUF/gemma-4-E4B-it-Q8_0.gguf

手元のモデル一覧を確認

lms ls
You have 3 models, taking up 20.39 GB of disk space.

LLM                               PARAMS    ARCH       SIZE        DEVICE
gemma-4-e4b-it                    7.5B      gemma4     8.19 GB     Local
openai/gpt-oss-20b (1 variant)    20B       gpt-oss    12.11 GB    Local

EMBEDDING                               PARAMS    ARCH          SIZE        DEVICE
text-embedding-nomic-embed-text-v1.5              Nomic BERT    84.11 MB    Local
lms server start --port 1234
# Success! Server is now running on port 1234

Gemma4をロード

lms load gemma-4-e4b-it --context-length 32768
Model loaded successfully in 2.49s.
(7.63 GiB)
To use the model in the API/SDK, use the identifier "gemma-4-e4b-it".

codexの設定

oss_provider = "lmstudio"

[model_providers.self_lmstudio]
name = "LM Studio"
base_url = "http://127.0.0.1:1234/v1"
wire_api = "responses"

codexの実行

codex --oss -m gemma-4-e4b-it

できた。unslothのチュートリアルにあるpromptを使ってみる。

You can only work in the cwd project/. Do not search for AGENTS.md - this is it. Install Unsloth via a virtual environment via uv. See https://unsloth.ai/docs/get-started/install/pip-install on how (get it and read). Then do a simple Unsloth finetuning run described in https://github.com/unslothai/unsloth. You have access to 1 GPU.

curlなどによる情報取得を実施するなら、少しconfig.tomlを更新する必要がある模様。

oss_provider = "lmstudio"
sandbox_mode = "workspace-write"

[sandbox_workspace_write]
network_access = true

[model_providers.self_lmstudio]
name = "LM Studio"
base_url = "http://127.0.0.1:1234/v1"
wire_api = "responses"

認証などのステップを経て作業を完了させることが出来た。

codexのToke 2倍期間が終わって5h limitのTokenをすぐに消費されてしまうのをちょっとでも避けたい。

参照

はじ環はじ環

Claude code

普段は使っていないがLocal llmで使用するなら追加料金はかからないはずなので試してみる。アカウント登録したら使えるのかな?

Install Claude code

curl -fsSL https://claude.ai/install.sh | bash
✔ Claude Code successfully installed!

  Version: 2.1.92
  Location: ~/.local/bin/claude

  Next: Run claude --help to get started
✅ Installation complete!

先人[1]に習って、アカウント登録はせずにANTHROPIC_AUTH_TOKENANTHROPIC_BASE_URLに接続先情報指定し直して実行する。

export ANTHROPIC_AUTH_TOKEN="lms"
export ANTHROPIC_BASE_URL="http://localhost:1234"

続いて、LM Studio起動

# Context sizeを指定
lms load openai/gpt-oss-20b --identifier gpt-oss-20b-cc --context-length 65536 --gpu max

適当なフォルダに移動して起動

mkdir -p test_cc && cd $_
claude --model gpt-oss-20b-cc


カラーテーマ

注意喚起

プロンプト入力できる画面: 途中で-ccに変えた

llm-jp-4-32b-a3b-thinging

国立情報学研究所(NII)のLLM-jpプロジェクトが公開した「LLM-jp-4-32B-A3B-thinking」モデルを使えるようにしてみる。

lms import ~/.llm/models/llm-jp-4-32b-a3b-thinking.gguf
 Choose categorization option Auto search Hugging Face (Recommended for models downloaded
from Hugging Face)
Searching for the model on Hugging Face using the file name...
W Cannot find the model on Hugging Face, you need to manually specify the user/repo.
 Who is the creator of the model? alfredplpl
 What is the model name? llm-jp-4-32b-a3b-thinking
File moved to ~/.lmstudio/models/alfredplpl/llm-jp-4-32b-a3b-thinking/llm-jp-4-32b-a3b-thinking.gguf
lms ls
You have 4 models, taking up 38.53 GB of disk space.

LLM                               PARAMS     ARCH        SIZE        DEVICE
gemma-4-e4b-it                    7.5B       gemma4      8.19 GB     Local
llm-jp-4-32b-a3b-thinking         32B-a3B    qwen3moe    18.14 GB    Local
openai/gpt-oss-20b (1 variant)    20B        gpt-oss     12.11 GB    Local     ✓ LOADED

EMBEDDING                               PARAMS    ARCH          SIZE        DEVICE
text-embedding-nomic-embed-text-v1.5              Nomic BERT    84.11 MB    Local

llm-jp-4-32b-a3b-thingingをloadする。

lms unload && lms load llm-jp-4-32b-a3b-thinking --identifier llm-jp-4-32b-a3b-thinking-cc --context-length 32768 --gpu 0.7
Model loaded successfully in 4.02s.
(16.89 GiB)
To use the model in the API/SDK, use the identifier "llm-jp-4-32b-a3b-thinking-cc".
lms ls
You have 4 models, taking up 38.53 GB of disk space.

LLM                               PARAMS     ARCH        SIZE        DEVICE
gemma-4-e4b-it                    7.5B       gemma4      8.19 GB     Local
llm-jp-4-32b-a3b-thinking         32B-a3B    qwen3moe    18.14 GB    Local     ✓ LOADED
openai/gpt-oss-20b (1 variant)    20B        gpt-oss     12.11 GB    Local

EMBEDDING                               PARAMS    ARCH          SIZE        DEVICE
text-embedding-nomic-embed-text-v1.5              Nomic BERT    84.11 MB    Local

claude起動

claude --model llm-jp-4-32b-a3b-thinking-cc

コードレビューなどには thinking モデルは適していない模様。

脚注
  1. https://zenn.dev/urakawa_jinsei/articles/2b707394d6c216 ↩︎