Roo Code(Roo-Cline)でローカルLLM(Ollama)のQwen3を設定する


# 動作イメージ

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/284824/ac61a41f-b651-4853-8fc1-9e2ab7446ed7.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/284824/d79a887c-b678-4f92-8031-84accc848d4c.png)

# step1: Ollamaをダウンロードする

![image.png](https://ollama.com/public/ollama.png)

### ollama download
https://ollama.com/

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/284824/7d354de1-24c9-416d-9ed0-bfa3d506990a.png)

# step2: ダウンロードするモデルを確認する

https://huggingface.co/Qwen/Qwen3-14B-GGUF#qwen3-14b-gguf

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/284824/1a581531-65b4-42c0-a2ce-fc2f09e1cc8c.png)

```md
# ハードウェア互換性(いわゆる量子化)
4-bit Q4_K_M    9 GB
5-bit Q5_0   10.3 GB
5-bit Q5_K_M 10.5 GB
6-bit Q6_K   12.1 GB
8-bit Q8_0   15.7 GB
```

```md
# Quantization Types
```

https://huggingface.co/docs/hub/gguf#quantization-types

```md
# GGUF
## https://huggingface.co/docs/hub/gguf#gguf
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/284824/93737d23-7187-4c7e-b2b2-e1bf7ae295fc.png)

# step3: Qwen3をダウンロードする

※powershellで作業する

```powershell
# 管理者権限
setx /M PATH "%PATH%;%LOCALAPPDATA%\Programs\Ollama"
```

```powershell
# ollama version check
& "$env:LOCALAPPDATA\Programs\Ollama\ollama.exe" --version
ollama version is 0.6.7
```

```powershell
## ollama download model command
& "$env:LOCALAPPDATA\Programs\Ollama\ollama.exe" run hf.co/Qwen/Qwen3-14B-GGUF:Q4_K_M
pulling manifest
pulling 500a8806e85e: 100% ▕██████████████████████████████████████████████████████████▏ 9.0 GB
pulling 5de36594c108: 100% ▕██████████████████████████████████████████████████████████▏  11 KB
pulling eb4402837c78: 100% ▕██████████████████████████████████████████████████████████▏ 1.5 KB
pulling 79712d8831ed: 100% ▕██████████████████████████████████████████████████████████▏  247 B
pulling 46771b859aa5: 100% ▕██████████████████████████████████████████████████████████▏  697 B
verifying sha256 digest
writing manifest
success
>>> Send a message (/? for help)
```

```powershell
# ollama download list
& "$env:LOCALAPPDATA\Programs\Ollama\ollama.exe" list
NAME                                ID              SIZE      MODIFIED    
hf.co/Qwen/Qwen3-14B-GGUF:Q4_K_M    dd1715a0104f    9.0 GB    3 hours ago
```

# step4: Roo-Clineの設定する

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/284824/c940a8e5-1465-478b-81ae-d968e7d30c92.png)

```powershell
# roo-cline settings

## APIプロバイダー
Ollama

## ベースURL
http://localhost:11434

## モデルID
hf.co/Qwen/Qwen3-14B-GGUF:Q4_K_M
```

# 設定修正:追記_2025/05/04
Roo-Cline用にモデルファイル(Modelfile)を設定しないと動かないので、以下修正

```powershell
# モデルがダウンロードされる先
# C:\Users\user-name-here>\.ollama\.
# OllamaのexeのPath
# C:\Users\<user-name-here>\AppData\Local\Programs\Ollama\

# modelfileフォルダを作る(管理しやすいように勝手に作っておく)
mkdir "$env:LOCALAPPDATA\Programs\Ollama\modelfile"
```

Qwen3-14B-cline.Modelfile
以下を設定する
 - PARAMETER num_ctx 20480
 - SYSTEM
 - TEMPLATE

```md
# Modelfile
FROM hf.co/Qwen/Qwen3-14B-GGUF:Q4_K_M

PARAMETER num_ctx 20480

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.
  - Speak in Japanese.


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 }}
{{ .Prompt }}
{{ .Suffix }}

{{- else if .Messages }}
{{- if or .System .Tools }}
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 }}

{{ end }}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
{{- if eq .Role "user" }}
user
{{ .Content }}

{{ else if eq .Role "assistant" }}
assistant
{{ if .Content }}{{ .Content }}
{{- else if .ToolCalls }}<tool_call>
{{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
{{ end }}</tool_call>
{{- end }}{{ if not $last }}

{{ end }}
{{- else if eq .Role "tool" }}
user
<tool_response>
{{ .Content }}
</tool_response>

{{ end }}
{{- if and (ne .Role "assistant") $last }}
assistant
{{ end }}
{{- end }}
{{- else }}
{{- if .System }}
system
{{ .System }}

{{ end }}{{ if .Prompt }}
user
{{ .Prompt }}

{{ end }}
assistant
{{ end }}{{ .Response }}{{ if .Response }}
{{ end }}

"""
```

## Modelfileを適応させる

```powershell
& "$env:LOCALAPPDATA\Programs\Ollama\ollama.exe" create qwen3-14b-gguf-q4_k_m-roo-cline -f .\modelfile\Qwen3-14B-cline.Modelfile
```

```powershell
PS C:\Users\<user-name-here>\AppData\Local\Programs\Ollama> & "$env:LOCALAPPDATA\Programs\Ollama\ollama.exe" create qwen3-14b-gguf-q4_k_m-roo-cline -f .\Qwen3-14B-cline.Modelfile
gathering model components
using existing layer sha256:500a8806e85ee9c83f3ae08420295592451379b4f8cf2d0f41c15dffeb6b81f0
using existing layer sha256:5de36594c10839788a8c589443a8ef9d8b8d17c65a1b5807206ae037fc36c6bd
using existing layer sha256:5de36594c10839788a8c589443a8ef9d8b8d17c65a1b5807206ae037fc36c6bd
creating new layer sha256:941adb9894eed9f92d5e124cebcf819d9e8b96c1bf242e6dcb689ea097442958
creating new layer sha256:a0600f8902c6f24eeb78b908a74a92db950a8039931b51f9b68376675dcb90f1
creating new layer sha256:c9b4a5dc3b5476e8d66e0e2d7b3ce59985d2c1cdd01cb6667d35805aa0f9da08
writing manifest
success
```

```powershell
& "$env:LOCALAPPDATA\Programs\Ollama\ollama.exe" list
```

```powershell
PS C:\Users\<user-name-here>\AppData\Local\Programs\Ollama> & "$env:LOCALAPPDATA\Programs\Ollama\ollama.exe" list
NAME                                      ID              SIZE      MODIFIED
qwen3-14b-gguf-q4_k_m-roo-cline:latest    d9773359244d    9.0 GB    17 seconds ago
hf.co/Qwen/Qwen3-14B-GGUF:Q4_K_M          dd1715a0104f    9.0 GB    About an hour ago
```

```powershell
& "$env:LOCALAPPDATA\Programs\Ollama\ollama.exe" ps
```

```powershell
PS C:\Users\<user-name-here>\AppData\Local\Programs\Ollama> & "$env:LOCALAPPDATA\Programs\Ollama\ollama.exe" ps
NAME                                      ID              SIZE     PROCESSOR          UNTIL
qwen3-14b-gguf-q4_k_m-roo-cline:latest    d9773359244d    15 GB    23%/77% CPU/GPU    4 minutes from now
```

### Roo-Cline(Roo Code)の設定を変えておく

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/284824/3018a4d2-53f6-4a71-81b5-4ac664f5c1bf.png)

# 補足:PCスペック

```md
# 動作環境

### プロセッサ
プロセッサ    Intel(R) Core(TM) i7-14700KF   3.40 GHz

### グラフィックス カード
グラフィックス カード NVIDIA GeForce RTX 3090 24GB
ドライバー 32.0.15.7283

### RAM
実装 RAM     64.0 GB (63.8 GB 使用可能)
速度         4800MHz

### ストレージ
SSD NVMe 2TB

### OS
エディション Windows 11 Pro
バージョン   24H2
OS ビルド    26100.3915
```

# 余談:

LM Studioでも同様にモデルをダウンロードしてRoo-Cline側でベースURLなどを指定したが、コンテキストが4096に制限されたのか(3090なら問題ないはずだが)18kくらいに増やすとPCがクラッシュして電源OFF/ONを5回くらい繰り返させられた。

LM Studio自体はオフラインChatGPT的な使い方はできるのでWEB検索はQwen3対応していないらしいが、そのうちできるようになるでしょう。

この記事は久々に人間が書いた記事。

それでは、また次回。