Cline+ローカル版DeepSeek R1でAIコーディングを使い放題にする(高スペックマシン向け)
しぴぴぴ!
Vtuberのしぴちゃん (https://www.youtube.com/@CP-chan) です。
配信ではゲームの話しかしてませんが、今回はAIに関する連載ということでローカル環境(手元のマシン)で動かせるAIの話をしていきます。
第一弾 DeepSeek R1をほぼ準備なしからローカルGPUで動かす
第二弾 本記事 Cline+ローカル版DeepSeek R1でAIコーディングを使い放題にする(高スペックマシン向け)
第三弾 GPUなしでも動く!ローカルLLMとllama.vscodeでコード補完
今回は連載の第二弾です。
LLMをローカルにインストールする大きなメリットとして、API制限や従量課金から解放されてLLMが「使い放題」になるという点があります。
DeepSeekは一世代前のV3ならWebで使ってもそんなに高くないのですが (https://api-docs.deepseek.com/quick_start/pricing) 、せっかく使い放題なのですから、AIコーディングエージェントのClineを利用してローカルでコードを生成できるようにしましょう。
注意点
本記事はハイエンドPC向けです。
自分の環境 (RTX 3080 + 32GB RAM) では動くけどスムーズではない、くらいです。
7Bのモデルだと本当に全然役に立たないので、最低でも大きいコンテキストウィンドウで14Bモデルを動かせないマシンの場合は素直にOpenAI, Anthropic, DeepSeek等の好きなAPIに課金した方が幸せだと思います。
第三弾の記事で紹介している llama.vscode なら、コード補完しかできませんがちょっといいノートPCくらいのスペックでも動かせます。
Clineとは
ClineはVSCodeの拡張機能として利用できるAIツールで、LLMが自動で方針を検討し、コードやファイルを作成・編集し、アプリケーションを作成してくれる夢のツールです。
もちろん途中で人間が方針を指示したりソースコードを直接編集したりすることもできます。詳しくはこのあたりの記事を読んでください。
今回は前のしぴちゃんの記事を実行済みである状態を想定し、Clineのインストールとローカルモデルへの対応を行います。
環境
Windows 11
メインメモリ 32GB(もっと少なくても動くけど遅くなる)
GPU NVIDIA RTX 3080 VRAM 10GB(もっと低スペックでも動くけど遅くなる)
llama.cppをインストールしている
モデルのggufファイルを入手できる
本記事では DeepSeek-R1-Distill-Qwen-14B-Q4_K_M.gguf を使います。
他のモデルでも理論上は動作するのですが、7BのモデルだとLLMがClineの出力フォーマットを守れないようで正しく動いてくれません。
前回の記事よりもかなり重い設定で動かすので、ギリギリのモデルサイズで動かしていた人は量子化のレベルを調整してください。
1. VSCodeのインストール
ここからダウンロードできます。すでにインストール済みの人は飛ばしてください。
2. Ollama のインストール
Ollama はLLMを動かすエンジンです。ClineはOllamaエンジンに対応しており、Ollama を通してローカルモデルを読み込むことでローカルLLMにコードを書かせることができます。
ここからインストールできます。

3. モデルをOllamaに登録
Ollama を使うために、手持ちのggufモデルを読み込んで登録します。
まず、ggufファイルと同じフォルダにモデルの定義となるモデルファイルを作成します。
今回はDeepSeek-R1-Distill-Qwen-14B-Q4_K_M.model という名前にします。拡張子は何でもよく、中身は以下の通りのテキストファイルです。
FROM DeepSeek-R1-Distill-Qwen-14B-Q4_K_M.gguf
PARAMETER temperature 0.1
PARAMETER num_ctx 16384
PARAMETER stop "User:"
PARAMETER stop "Assistant:"
PARAMETER stop "<|endoftext|>"
SYSTEM """
You are an advanced AI coding assistant, specifically designed to help with complex programming tasks, tool use, code analysis, and software architecture design. Your primary focus is on providing expert-level assistance in coding, with a special emphasis on using tool-calling capabilities when necessary. Here are your key characteristics and instructions:
1. Coding Expertise:
- You have deep knowledge of multiple programming languages, software design patterns, and best practices.
- Provide detailed, accurate, and efficient code solutions without additional explanations or conversational dialogue unless requested by the user.
- When suggesting code changes, consider scalability, maintainability, and performance implications.
2. Tool Usage:
- You have access to various tools that can assist in completing tasks. Always consider if a tool can help in your current task.
- When you decide to use a tool, you must format your response as a JSON object:
{"name": "tool_name", "arguments": {"arg1": "value1", "arg2": "value2"}}
- Common tools include but are not limited to:
- `view_file`: To examine the contents of a specific file
- `modify_code`: To suggest changes to existing code
- `create_file`: To create new files with specified content
- `ask_followup_question`: To request more information from the user
- `attempt_completion`: To indicate that you've completed the assigned task
3. Task Approach:
- Break down complex tasks into smaller, manageable steps unless requested to solve the task at once.
- If a task is large or complex, outline your approach before diving into details unless using a tool.
- Use tools to gather necessary information before proposing solutions.
4. Code Analysis and Refactoring:
- When analysing existing code, consider its structure, efficiency, and adherence to best practices.
- Suggest refactoring when you see opportunities for improvement, explaining the benefits of your suggestions unless using a tool.
- If you encounter or anticipate potential errors, explain them clearly and suggest solutions unless using a tool.
- When providing code solutions, include relevant comments to explain complex logic.
- Adhere to coding standards and best practices specific to each programming language or framework.
- Suggest optimisations and improvements where applicable.
5. Clarity and Communication:
- Explain your reasoning and decisions clearly, especially when suggesting architectural changes or complex solutions unless using a tool.
- If you're unsure about any aspect of the task or need more information, use the `ask_followup_question` tool to clarify.
Remember, your primary goal is to assist with coding tasks and tool use efficiently and effectively. Utilise your tool-calling capabilities wisely to enhance your problem-solving and code generation abilities.
"""
TEMPLATE """
{{- if .Suffix }}<|fim_prefix|>{{ .Prompt }}<|fim_suffix|>{{ .Suffix }}<|fim_middle|>
{{- else if .Messages }}
{{- if or .System .Tools }}<|im_start|>system
{{- if .System }}
{{ .System }}
{{- end }}
{{- if .Tools }}
# Tools
You may call one or more functions to assist with the user query.
You are provided with function signatures within <tools></tools> XML tags:
<tools>
{{- range .Tools }}
{"type": "function", "function": {{ .Function }}}
{{- end }}
</tools>
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call>
{{- end }}<|im_end|>
{{ end }}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
{{- if eq .Role "user" }}<|im_start|>user
{{ .Content }}<|im_end|>
{{ else if eq .Role "assistant" }}<|im_start|>assistant
{{ if .Content }}{{ .Content }}
{{- else if .ToolCalls }}<tool_call>
{{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
{{ end }}</tool_call>
{{- end }}{{ if not $last }}<|im_end|>
{{ end }}
{{- else if eq .Role "tool" }}<|im_start|>user
<tool_response>
{{ .Content }}
</tool_response><|im_end|>
{{ end }}
{{- if and (ne .Role "assistant") $last }}<|im_start|>assistant
{{ end }}
{{- end }}
{{- else }}
{{- if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>user
{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
{{ end }}{{ .Response }}{{ if .Response }}<|im_end|>{{ end }}
"""
記事の最後にモデルファイルの簡単な説明を付けておきます。
Ollama に登録させるには、前回インストールしたw64devkitを起動し、モデルファイルがある場所に移動します。
ollama create DeepSeek-R1-14B-Q4_K_M -f DeepSeek-R1-Distill-Qwen-14B-Q4_K_M.modelDeepSeek-R1-14B-Q4_K_M はモデル名です。好きな名前を付けることができます。 -f で モデルファイルを指定します。
success と表示されたら成功です。続いて、
ollama serveでOllamaサーバーを起動しておきます。 デフォルトでは localhost:11434 で起動します。環境変数OLLAMA_HOSTで別のホストで起動できます。
4. Clineのインストール
VSCodeを起動したら、左のタブの拡張機能/Extensions (□が4個あるやつ) から拡張機能を検索します。Cline と検索してインストールします。

インストールすると、左側にロボみたいなClineのアイコンのタブが出てきます。Clineのアイコンを押して設定に進みます。

設定画面では、まず API ProviderにOllamaを選びます。
Ollama をデフォルト設定で起動していれば、Base URLは変更する必要はありません。
Model IDからDeepSeek-R1-14B-Q4_K_M:latestなど、自分が登録したモデルを選びます。設定が終わったらDoneを押し、設定を終了します。
続いて、下に Auto-approve: None があります。デフォルトではほとんどあらゆる操作に対して人間に許可を求めてきますが、これを押して各設定にチェックを入れると、自動で承認できます。
完全に自動化したい場合はすべてにチェックを入れればよいのですが、LLMが誤ったコードやコマンドを実行するとPCのデータが破壊される可能性もあります。使う場合は注意してください。
また、下にはMax Requests: 20 という設定もあります。連続して実行するAPIリクエストの回数で、これを超えるとそのまま続行していいか許可を求めてきます。高額なAPI利用料を請求されないための設定ですが、ローカルLLMは使い放題なので、気前よく100くらいに設定してみてもよいでしょう。
Enable Notificationsにチェックを入れると、実行終了時やLLMが実行の許可を求めている時に通知を送ってきます。好みでONにしてください。
個人的な初心者向けおすすめ設定を下に示しておきます。
Edit filesとUse MCP serversをOFF、それ以外ONです。

5. 使ってみる
左上の+ボタン、New Taskからタスクを作成できます。設定の歯車の左にある四角+右上矢印みたいなアイコンをクリックすると、右側の枠で実行状況を見ることができます。
モードにはPlanとActがあり、Planでは抽象的なタスクを言語化したり設計をまとめたりすることができます。Actでは実際に開発を行います。

例えば、
"現在のディレクトリに test.py を作成し、pythonでHello Worldを記述してください。test.py を実行し、実行結果をoutput.txt に格納してください。"
などの指令をActモードで行うと、機能がちゃんと動いているか確認することができます。ただし、複雑なタスクを実行すると超低速になったり、モデルがClineの出力形式を忘れてエラーになったりします。
PCが高スペックなら32Bのモデルにしたり量子化レベルを改善したりコンテキストウィンドウを20480にしたりすることで解決する可能性はありますが、スペック不足の場合は素直にOpenAI, Anthropic, DeepSeek等の好きなAPIに課金した方が幸せだと思います。
モデルファイルの説明
FROM 行で .ggufファイルを指定します。違うファイル名の場合ここを書き換えます。
Clineは内部でかなり長いプロンプトを実行するようで、コンテキストウィンドウ(同時に覚えておけるトークン数)を16384に設定します。
RTX4090とかを持ってる人で、エラー率を減らしたいなら20480がいいかもしれません。たぶん3080では20480は無理です。システムプロンプトとtemplate(Clineのツールの使い方)、temperatureなどは、Cline向けのqwen2.5-coderを参考に設定します。 (hhao/qwen2.5-coder-tools)
