メむンコンテンツぞスキップ
芋出し画像

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にコヌドを曞かせるこずができたす。
    ここからむンストヌルできたす。

    画像
    Ollamaのむンストヌル画面

    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.model

    DeepSeek-R1-14B-Q4_K_M はモデル名です。奜きな名前を付けるこずができたす。 -f で モデルファむルを指定したす。
    success ず衚瀺されたら成功です。続いお、

    ollama serve

    でOllamaサヌバヌを起動しおおきたす。 デフォルトでは localhost:11434 で起動したす。環境倉数OLLAMA_HOSTで別のホストで起動できたす。

    4. Clineのむンストヌル

    VSCodeを起動したら、巊のタブの拡匵機胜/Extensions □が4個あるや぀ から拡匵機胜を怜玢したす。Cline ず怜玢しおむンストヌルしたす。

    画像
    Clineの拡匵機胜をむンストヌル

    むンストヌルするず、巊偎にロボみたいな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)

     
     
    ゲヌムずAI。技術系蚘事は必ず動䜜させおから曞きたす。

    あなたぞのおすすめ