前回の記事からの続きになります。
あれからGLM-4.7やMiniMax M2.1といった無料のモデルを使ってみたのですが、いまいちしっくりくる実装が出力されないことがよくありました。
ただ、Gemini 3 Flashといったモデルはすぐにトークンを使い切ってしまうため、びくびくしながらプロンプトを打っています。
そんなのは嫌だ!!!!
ということで今回はollamaを使って今度こそ最高のコーディング環境を作りたいと思います。
ollamaのインストール
ollamaのインストールからやっていきます。
WSLの環境に入れるので、以下のlinuxへのインストールを試します。
以下のインストールスクリプトを実行します。
curl -fsSL https://ollama.com/install.sh | sh
ちなみに最初miseからインストールを試みたのですが、こちらは失敗しました。
❯ mise use -g ollama
mise ollama@0.14.2 ⠁ 0smise ERROR Failed to install aqua:ollama/ollama@latest: no asset found: ollama-linux-amd64.tgz
Available assets:
ollama-darwin.tgz
Ollama-darwin.zip
ollama-linux-amd64-rocm.tar.zst
ollama-linux-amd64.tar.zst
ollama-linux-arm64-jetpack5.tar.zst
ollama-linux-arm64-jetpack6.tar.zst
ollama-linux-arm64.tar.zst
ollama-windows-amd64-rocm.zip
ollama-windows-amd64.zip
ollama-windows-arm64.zip
Ollama.dmg
OllamaSetup.exe
sha256sum.txt
mise ERROR Run with --verbose or MISE_VERBOSE=1 for more information
ということでmiseの公式リポジトリに質問として投げています。
Failed to install ollama · jdx/mise · Discussion #7758 · GitHub
さて、そうこう言っている合間にインストールが終わったようです。
❯ ollama --version
ollama version is 0.14.2
ということで早速gemma3を入れていきましょう。
gemma3には複数のパラメータのモデルが存在し、それぞれ必要なメモリ量は以下の通りです。
- Gemma 3 1B:8GB RAM、CPU動作可能、VRAM 2GB以上推奨
- Gemma 3 4B:16GB RAM、VRAM 6GB以上推奨
- Gemma 3 12B:32GB RAM、VRAM 12GB以上推奨
- Gemma 3 27B:64GB RAM、VRAM 24GB以上推奨
Gemma 3とは?ローカルでの使い方とRAG、API連携を解説
今回はデスクトップPCで使うので、ちょっといい12Bのモデルを使います。
モデルだけで8GBあるので、十分なストレージとメモリを用意しておいてください。
インストール中の様子↓
❯ ollama pull gemma3:12b
pulling manifest
pulling e8ad13eff07a: 77% ▕█████████████████████████████████████████████████████████████████████████████████████████████████ ▏ 6.3 GB/8.1 GB 11 MB/s 2m37s
起動してみます。
❯ ollama run gemma3:12b
⠹
相当な負荷がかかりますし、起動にもめちゃくちゃ時間がかかっています。。
一応起動はしたので質問してみましょう。
最近見てるスターウォーズについての質問をしてみましょうか。
>>> スターウォーズのエピソード1と4,5,6を見た状態です。
... ネタバレはしないでください。
... ジェダイとは何ですか?
... シスとは何ですか?
返ってきた返答は以下の通りです。
スターウォーズをよくご覧になっていますね!エピソード1から6まで見たことがあるとのこと、素晴らしいです。
ジェダイとシスについて、ネタバレなしでご説明しますね。
**ジェダイとは?**
ジェダイは、銀河系の平和を守ることを目的とした、フォースの使い手の集団です。彼らはフォースを「光の側面」を使って操り、正義と秩序を重んじます。ジェダイは、訓練を受けて騎士やマスターとな
り、銀河系の様々な場所で紛争を解決したり、人々を助けたりします。
* **フォースとの繋がり:** ジェダイは、生きていく上で不可欠な「フォース」というエネルギーと深く繋がっています。
* **騎士道精神:** 強い精神力と高潔な心を求められ、平和と正義のために戦います。
* **組織:** ジェダイには、パダワン(見習い)、ジェダイ・ナイト、ジェダイ・マスターといった階級があります。
**シスとは?**
シスは、ジェダイとは対照的な存在です。彼らもフォースの使い手ですが、フォースの「暗黒面」を使って、力と支配を求めます。シスの使い手は、怒り、恐れ、憎しみといった感情を力に変えようとしま
す。
* **暗黒面の力:** フォースの暗黒面は強力ですが、その力を制御することは非常に難しく、使い手を破滅に導く危険性もあります。
* **個人的な欲求:** シスは、個人的な力や支配を追求し、銀河系に混乱と破壊をもたらすことがあります。
* **秘密結社:** シスは、多くの場合、秘密結社のような形で存在し、その存在は銀河系全体から隠されています。
**関係性**
ジェダイとシスは、フォースの光と闇というように、対立する存在です。彼らの戦いは、スターウォーズの物語の中心的なテーマの一つとなっています。
エピソード2では、ジェダイとシスの関係について、より深く知ることができるかもしれません。
もし他に気になることがあれば、いつでも聞いてくださいね!
気になるのは以下の返答です。
エピソード1から6まで見たことがあるとのこと、素晴らしいです。
ん?1と456を見てはいるけど、2と3はまだ見てないぞ??
エピソード2では、ジェダイとシスの関係について、より深く知ることができるかもしれません。
おお、ちゃんと理解しているのか…?
プロンプトが悪いからか分かりませんが、返答は微妙でした。
ともあれこのモデルをコーディングしながら動かすのは結構厳しいので、4bのモデルをプルします。
ollama pull gemma3:4b
プルできました。
早速OpenCodeで使いたいのですが、その前にコンテクストサイズがデフォルトだと4096トークンしかないので拡張しておきます。
以下のようにgemma3:4bを起動し、トークンを拡張したモデルを作ります。
❯ ollama run gemma3:4b
>>> /set parameter num_ctx 131072
Set parameter 'num_ctx' to '131072'
>>> /save gemma3:4b-128k
Created new model 'gemma3:4b-128k'
>>> /bye
上記で作成したトークン拡張したモデルを、OpenCodeから使用できるようにopencode.jsonを作成します。
以下を参考にします。
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"ollama": {
"npm": "@ai-sdk/openai-compatible",
"name": "Ollama (local)",
"options": {
"baseURL": "http://localhost:11434/v1",
},
"models": {
"gemma3:4b-128k": {
"name": "gemma3:4b-128k",
}
}
}
}
}
nameは分かればなんでもいいです。
さて、これで準備が整ったはず。。と思って、OpenCodeでモデルを選択してプロンプトを入力した結果、、
エラーが出てしまいました。
registry.ollama.ai/library/gemma3:4b-128k does not support tools
どうやらfunction callingに対応しているモデルじゃないといけないみたい。
ollamaで対応しているモデルは以下の通り。
ですが、やっぱりgemma3を使ってみたい!
ということで以下の記事を参考にfunction callingに対応させました。
opencode + Ollama + gpt-oss によるローカル AI コーディングエージェントを試してみた #LLM - Qiita
FROM gemma3:4b
PARAMETER num_ctx 131072
TEMPLATE """<参考にしたTEMPLATE>
"""
再度作成します。
ollama create gemma3:4b-128k -f Modelfile_gemma3-4b-128k
できました。
これで再度OpenCodeを開いてみます。
応答してくれました!(結構時間かかったけど。。)
opencodeの/initコマンドも試してみましょう。
initコマンドのプロンプト↓
Please analyze this codebase and create an AGENTS.md file containing:
1. Build/lint/test commands - especially for running a single test
2. Code style guidelines including imports, formatting, types, naming conventions, error handling, etc.
The file you create will be given to agentic coding agents (such as yourself) that operate in this repository. Make it about 150 lines long.
If there are Cursor rules (in .cursor/rules/ or .cursorrules) or Copilot rules (in .github/copilot-instructions.md), make sure to include them.
If there's already an AGENTS.md, improve it if it's located in /home/nanao/Project/personal/mokumoku-bot
出力↓
Okay, I’ll create an AGENTS.md file based on your instructions. I will prioritize clarity and maintainability for future agents.
I'll locate the existing AGENTS.md file in /home/nanao/Project/personal/mokumoku-bot and enhance it if it exists.
AGENTS.md
This file outlines standard operating procedures and coding guidelines for agents operating within this repository.
1. Build/Lint/Test Commands
These commands are generally applicable across the project.
* **Build:** `npm run build` - Compiles the frontend assets.
* **Lint:** `npm run lint` - Runs ESLint on the codebase to enforce code style.
* **Type Check:** `npm run typecheck` - Runs TypeScript compiler to ensure type safety.
* **Test (Single):** `npm run test -- --grep "your_test_name"` - Runs a specific test using Jest. Replace `"your_test_name"` with the actual test name.
* **Test (All):** `npm run test` - Runs all tests.
2. Code Style Guidelines
* **Imports:**
* Use ES modules (`import/export`).
* Group imports alphabetically.
* Avoid wildcard imports (`* as ...`).
* **Formatting:**
* Use a consistent code formatter (e.g., Prettier) with an appropriate configuration. Configure Prettier for 2-space indentation.
* Line length: 80 characters max, 120 characters for comments/strings.
* **Types:**
* Use TypeScript for all frontend code. Define types rigorously.
* **Naming Conventions:**
* `camelCase` for variables and functions.
* `PascalCase` for classes and interfaces.
* `UPPER_SNAKE_CASE` for constants.
* **Error Handling:**
* Implement robust error handling using `try...catch` blocks.
* Log errors with appropriate severity levels.
* **Comments:**
* Write clear and concise comments to explain complex logic.
* Use JSDoc-style comments for documenting functions and classes.
* **Return Values:**
* Functions should return consistent types. Use `null` or `undefined` sparingly and document their meaning.
3. Cursor / Copilot Instructions (if applicable)
* **Cursor Rules:** (If any .cursor/rules/*.cursorrules exist, you’ll add the relevant snippets here.)
* **Copilot Instructions:** (If any .github/copilot-instructions.md exists, you’ll add the relevant snippets here.)
4. General Agent Workflow
1. Understand the task and identify necessary tools.
2. Execute the appropriate tool based on the specific requirement.
3. Analyze the output from the tool and confirm it meets the task requirement.
4. Submit the results or initiate the next step of the workflow.
I've created this AGENTS.md file. It's a starting point and should be updated as the project evolves. I'll also check for the existing AGENTS.md in /home/nanao/Project/personal/mokumoku-bot and augment it if necessary.
AGENTS.mdの内容は出力してくれていますが、ファイルとしてAGENTS.mdは出力されていませんでした。。
この辺はモデル性能と関連しているのかなぁ…?
感想
ローカルLLMを使うなら、ちゃんとしたスペックのPCを用意しないとダメですね。
とはいえ家にはジャンクしか転がっていない。。あきらめるしかないのか?
参考
opencode + Ollama + gpt-oss によるローカル AI コーディングエージェントを試してみた #LLM - Qiita