🎓

Ollamaを使ってローカル環境でgpt-oss-20bモデルを試してみた

に公開

はじめに

昨日参加したオフラインイベントで、たまたまローカルLLMの話題になり、面白そうだったので試してみました。

タイムリーで類似記事がたくさん見受けられましたが、そこは無視してマイペースにまとめてみようと思います。

概要

Ollamaについて

https://github.com/ollama/ollama

調べると以下の特徴がありました。

  • オープンソースである: Ollamaは誰でも自由に使用・改良できるソフトウェア
  • ローカル実行できる: インターネットに接続せずに、自分のPC上でAIモデルを動かすことができます。
  • 対応プラットフォームが様々: Linux、Mac、Windowsなどで利用可能

gpt-oss-20bについて

https://openai.com/ja-JP/index/introducing-gpt-oss/

2025年8月5日に登場したOpenAIが提供する最先端のオープンウェイト言語モデルです。
gpt-oss-20bは発表された2種類のうちの軽量な方です。

始め方

以下にアクセスし、「gpt-oss-20b」の横のダウンロードボタンを選択すると、3種類ほど簡易的に使い始める方法が表示されます。
https://gpt-oss.com/

Hugging Face

こちらの方法については、結果的に私のmacbook pro環境で使えませんでした

一応エラーも含め実施した内容を載せます。

まずはpipコマンドで以下をインストールしました。

pip install transformers torch

次に以下コマンドを実行します。

transformers serve

ModuleNotFoundError: No module named 'PIL'

moduleが足りないので追加します。

pip install Pillow

再度実行します。

transformers serve

ImportError: Missing dependencies for the serving CLI. Please install with `pip install transformers[serving]`

エラー通りまたmoduleを追加します。

pip install transformers[serving]

再度実行します。

transformers serve

ImportError: You need to install rich to use the chat interface. (`pip install rich`)

また追加します。
エラー通りまたmoduleを追加します。

pip install rich

再度実行します。

% transformers serve
INFO:     Started server process [59028]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://localhost:8000 (Press CTRL+C to quit)

別ターミナル開いて以下実行します。

transformers chat localhost:8000 --model-name-or-path openai/gpt-oss-20b

ImportError: Please install aiohttp to use `AsyncInferenceClient` (`pip install aiohttp`).

またエラー通りmoduleを追加します。

pip install aiohttp

これでmodule周りは完全にクリアしたようですが、結局NVIDIAのGPUを使用している環境でないとダメなようでした。

  File "/Users/username/.pyenv/versions/3.11.3/lib/python3.11/site-packages/transformers/quantizers/quantizer_mxfp4.py", line 60, in validate_environment
    raise RuntimeError("Using MXFP4 quantized models requires a GPU")
RuntimeError: Using MXFP4 quantized models requires a GPU

Ollama

こちらの方法は私のmac環境でも実現できました。

https://ollama.com/download

アイコンがとてもキュートです。

表示コマンド通り、モデルをpullしてきます。全部で12.8GBでした。

ollama pull gpt-oss:20b

そしていよいよ起動してみます。

% ollama run gpt-oss:20b
>>> hello
Thinking...
User says "hello". So we need to respond in a friendly manner. Probably ask how we can help.
...done thinking.

Hello! 👋 How can I help you today?

無事動作しました。
macのアクティビティモニタでメモリ使用率を確認した所、確認時は13.81GBでした。

最後に、簡単な英語でk8sのマニフェストに関して尋ね、返ってきた内容を掲載いたします。
念の為、本当にオフラインで問題ないのか確かめたく、ネット回線を切って(Wi-FiをOFFにして)実行してみました。
(結構長いです)

聞いた内容は、kubernetesでhpaを用いたDeploymentはどう作るの?という内容です。
(英語は何も調べないで書いてしまったので文法間違っているかも)

以降、出力結果

>>> how to make for k8s manifest of Deployment with hpa ?
Thinking...

(中略)

Ok let's write.
...done thinking.

Below is a **ready‑to‑apply** example that shows a simple `Deployment` and a matching 
`HorizontalPodAutoscaler` (HPA) in the same YAML file.  
Feel free to copy it, tweak the image, the resources, and the thresholds, then run

```bash
kubectl apply -f deployment‑hpa.yaml

1. The Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx‑demo
  namespace: default
  labels:
    app: nginx‑demo
spec:
  replicas: 2                # start with 2 pods
  selector:
    matchLabels:
      app: nginx‑demo
  template:
    metadata:
      labels:
        app: nginx‑demo
    spec:
      containers:
      - name: nginx
        image: nginx:1.27   # <-- replace with your image
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu:    200m      # CPU requested by the pod
            memory: 256Mi     # Memory requested by the pod
          limits:
            cpu:    500m      # Max CPU the pod can use
            memory: 512Mi     # Max Memory the pod can use

Why those fields matter

Field Why it matters
replicas Initial number of pods (will be overridden by HPA).
labels The HPA uses these labels to discover which deployment to scale.
resources.requests The HPA reads CPU/Memory requests to compute usage percentages.
resources.limits Optional – caps how much a pod can actually use.

2. The HorizontalPodAutoscaler

---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx‑demo-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx‑demo            # must match the Deployment name
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50   # scale when avg CPU > 50%
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 70   # optional: also scale on memory
A few notes on the HPA section
Field What it does
scaleTargetRef Tells the HPA which resource to autoscale (here a Deployment).
minReplicas / maxReplicas The lower and upper bounds for scaling.
metrics The conditions that trigger scaling. The example uses CPU and Memory
utilization percentages.
averageUtilization The target average usage across all pods. If the current average
exceeds this, the HPA will try to add pods; if it falls below, it will try to remove pods.

Tip – If you only need CPU‑based scaling, drop the memory block.


3. Prerequisites

  1. Metrics Server – HPA needs real‑time metrics.
    Install it (if not already present):

    kubectl apply -f 
    

https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml


2. **Namespace** – The example uses the `default` namespace.  
If you want a different namespace, create it first:

```bash
kubectl create ns my‑app

Then adjust the metadata.namespace fields accordingly.


4. Applying the manifests

kubectl apply -f deployment‑hpa.yaml

Verify the Deployment and HPA status:

# Pods
kubectl get pods -l app=nginx-demo

# Deployment
kubectl describe deployment nginx-demo

# HPA
kubectl get hpa nginx-demo-hpa
kubectl describe hpa nginx-demo-hpa

You should see the HPA reporting Desired replicas, Current replicas, and Target CPU
utilization
.


5. Optional: Auto‑scaling with kubectl autoscale

If you prefer a quick command instead of writing YAML:

kubectl autoscale deployment nginx-demo \
  --cpu-percent=50 \
  --min=2 \
  --max=10

This generates a simple HPA (resource‑based, CPU only) for you.


6. Going further

Scenario What to change Why
Scale on custom metrics (e.g. requests per second) Add a type: External or `type:
Object` metric block and reference a Prometheus Adapter or KEDA source. Use non‑CPU
metrics.
Use a StatefulSet or DaemonSet Replace Deployment with StatefulSet/DaemonSet
in both specs. Some workloads require stable identities or one pod per node.
Add a Service Create a Service that points to the Deployment’s pods. Expose the
app outside the cluster or to other pods.

Quick reference cheat‑sheet
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata: {name: <name>, labels: {app: <app>}}
spec:
  replicas: <number>
  selector: {matchLabels: {app: <app>}}
  template:
    metadata: {labels: {app: <app>}}
    spec:
      containers:
      - name: <container>
        image: <image>
        resources:
          requests: {cpu: <m>, memory: <Mi>}
          limits:   {cpu: <m>, memory: <Mi>}

# HPA
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: {name: <name>-hpa}
spec:
  scaleTargetRef: {apiVersion: apps/v1, kind: Deployment, name: <deployment>}
  minReplicas: <min>
  maxReplicas: <max>
  metrics:
  - type: Resource
    resource: {name: cpu, target: {type: Utilization, averageUtilization: <pct>}}

Happy autoscaling! 🚀 If you hit any snags (e.g., “Failed to fetch metrics”), double‑check
that the Metrics Server is running and that the pod has the correct resource requests.

おわりに

これでオフライン環境でもある程度使えそうなLLM環境が実現できました。
個人的に気になったのは、AIが回答を生成中、mac本体からかすかな音で「キュルルるる...」と鳴っていたことですね。
やはり本体にそれなりに負荷がかかっているのでしょうか。

ちなみに余談ですがollama runを実行した後、終了するにはCtrl+dもしくは/byeでした。
いつものくせでCtrl+C押してしまう...

>>> 
Use Ctrl + d or /bye to exit.

他の類似記事と合わせて参考になれば幸いです。
ここまで読んでいただきありがとうございました。

Discussion