Zenn
Closed7

ローカルのggufモデルをollamaで利用する

moriokamorioka

はじめに

DeepSeek-R1のQwen/Llamaモデルを使いたかった。ちょうどcyberagentさんが日本語で追加学習してくれていたし。

同様の記事はいろいろあるだろうが、自分用。

後から思えば、実質的にこの記事の一部をなぞる格好。自分は、GGUF化+量子化されたモデルを拾ってきただけだ。
https://note.com/catap_art3d/n/n523e1fe9bfee
https://qiita.com/katabami83/items/dae8601fbfd7245c3a71

moriokamorioka

実際の手順

対象は、DeepSeek-R1-Distill-Qwen-32B の日本語追加学習モデル

https://gihyo.jp/article/2025/01/deepseek-r1-japanese

https://huggingface.co/cyberagent/DeepSeek-R1-Distill-Qwen-32B-Japanese

さらに、これを GGUF化 + 量子化したモデル

https://huggingface.co/mmnga/cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese-gguf

mmnga/cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese-gguf の q4_k_m を利用することにする。

export GGUF_FILE=./mmnga-cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese-Q4_K_M.gguf

# mmnga/cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese-gguf の q4_k_m を取得する
wget https://huggingface.co/mmnga/cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese-gguf/resolve/main/cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese-Q4_K_M.gguf?download=true
mv 'cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese-Q4_K_M.gguf?download=true'  ${GGUF_FILE}

# モデルファイルをベースのdeepseek-r1:32b (qwen:32bへの蒸留モデル)から流用する
ollama pull deepseek-r1:32b
ollama show deepseek-r1:32b --modelfile > modelfile
ollama rm deepseek-r1:32b  # 不要なら消す

# modelfileのFROM行を手元のGGUFファイルを参照するよう修正する
sed "s|^FROM .*|FROM ${GGUF_FILE}|" modelfile > modelfile.new

# ollama形式のモデルを作成する
ollama create mmnga-cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese:Q4_K_M -f modelfile.new

# 実行
ollama run mmnga-cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese:Q4_K_M
moriokamorioka

修正前の modelfile

ちなみに modelfile はこんな感じ

$ ollama show deepseek-r1:32b --modelfile
# Modelfile generated by "ollama show"
# To build a new Modelfile based on this, replace FROM with:
# FROM deepseek-r1:32b

FROM /home/morioka/.ollama/models/blobs/sha256-6150cb382311b69f09cc0f9a1b69fc029cbd742b66bb8ec531aa5ecf5c613e93
TEMPLATE """{{- if .System }}{{ .System }}{{ end }}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1}}
{{- if eq .Role "user" }}<|User|>{{ .Content }}
{{- else if eq .Role "assistant" }}<|Assistant|>{{ .Content }}{{- if not $last }}<|end▁of▁sentence|>{{- end }}
{{- end }}
{{- if and $last (ne .Role "assistant") }}<|Assistant|>{{- end }}
{{- end }}"""
PARAMETER stop <|begin▁of▁sentence|>
PARAMETER stop <|end▁of▁sentence|>
PARAMETER stop <|User|>
PARAMETER stop <|Assistant|>
LICENSE """MIT License

Copyright (c) 2023 DeepSeek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
moriokamorioka

実行環境

IBM Legion T5 26IAB7
i7-12700F 2.10 GHz / RAM 128GB / RTX3060-12G
Windows11Home + Ubuntu22.04/WSL2 で、1~2文字/秒。

実行例:3ヒントクイズの作成
(ハルシネーションが多発。ネット検索で事実を確認できれば、この辺りは解決されるだろう。ちなみに、谷村新司は無関係なので以下の内容は誤り。リリースは1989年なので、以下の内容は誤り。海外での知名度も不明。)

$ ollama run mmnga-cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese:Q4_K_M
>>> hello
<think>

</think>

Hello! How can I assist you today? 😊

>>>  3ヒントクイズを作る。ヒントは段階的に出し、回答者の反応をみて、次のヒントを出す。回答者
... は常に沈黙する。
... クイズの正解は歌謡曲「川の流れのように」。クイズの対象は70歳代の男性。海外生活が長く、
... 日本文化は知らない。
... 日本語でで考え、日本語で回答せよ
<think>
まず、ユーザーが求めているのは、段階的なヒントを出す3ヒントクイズです。正解は「川の流れのように」で、対象は
70歳代の男性、特に海外生活が長く日本文化に詳しくない方に対するものです。

最初に考えるのは、その男性が知っている可能性が高いキーワードや要素を探ることです。日本の歌謡曲に詳しくない場合
でも、国際的に有名なアーティストや曲名をヒントにする必要があります。例えば、「千代田線」は東京の地下鉄路線で、
海外在住の方には知られていませんが、曲名の一部として使われているので、ここから入るのが良いかもしれません。

次に、次のヒントでは「雨の中」というフレーズを出すことで、日本の季節感や情景描写に関連付けます。これにより、自
然な流れで「川の流れのように」へのつながりを導きます。最終的には「流れる水のように」という表現から、直接的なヒ
ントに到達します。

各段階での回答者の反応を考えると、最初のヒントで沈黙した場合、次のヒントではより具体的な要素(雨や川)を加える
ことで、徐々に正解に近づけます。海外生活が長い方でも、日常的な情景から連想できるように工夫することが重要です。

</think>

**クイズの構成と進行フロー:**

### **ヒント1:**
「千代田線や雨宮の駅名が曲名に関わる日本の名曲です。」

#### **意図:**
- 東京メトロ千代田線(海外在住の方には未知)+**「雨宮」(作曲家・谷村新司の別名義)**をヒントにし、谷村作品で
あることを暗示
- 知識不足なら**沈黙 → 次のヒントへ移行**

---

### **ヒント2 (回答者が沈黙した場合):**
「雨の中を歩く情景や、『愛』をテーマにした名曲で、海外でも1980年代から広く知られています。」

#### **意図:**
- 海外知名度(英語タイトル『Like a River Flows』)+自然な情景描写(雨・川)で**谷村新司の特徴的詞曲スタイル
**を誘導
- 代わりに「海外でも人気」という情報で洋楽との混同防止

---

### **ヒント3 (回答者が沈黙した場合):**
「『流れる水のように』という表現がタイトルに使われ、1985年に海外でも大ヒットした日本の曲です。」

#### **意図:**
- 歌詞の直訳的イメージ(川/雨→水)+**1985年発売情報**で年代を絞り込み
- 「海外でも」を強調し、谷村作品の国際展開歴を想起させる

---

### **進行想定:**
1. 初ヒント→沈黙 → 2. 情景描写+海外ヒット → 3. 正解直前表現で回答促す
2. 回答者に「正解は『川の流れのように』です」と明示する前に、**海外在住経験者の共感を呼ぶ言葉**を添える(例:
「海外でも谷村新司さんの曲はよく放送されましたね」)

この設計により、日本文化に詳しくない層も、自然な情景→国際知名度→直訳的表現の3段階で導かれます。

>>>
>>> 各ヒントをjson形式で出力して
<think>
ユーザーが求めているのは、各クイズのヒントをJSON形式で出力することです。以前に作成した3ヒントの構成を確認し、
それぞれのヒント文とその意図、進行フローを整理します。

まず、各ヒントの内容を正確に抽出する必要があります。最初のヒントは「千代田線や雨宮の駅名が曲名に関わる…」とい
う表現で、谷村新司の別名義である「雨宮」と曲名の関連性を示しています。2つ目のヒントでは「雨の中を歩く情景…海外
でも1980年代から広く知られている」と、自然な情景描写と国際的な知名度を強調しています。3つ目のヒントは「『流れ
る水のように』という表現がタイトルに使われ、1985年に…」で、直接的な表現と発売年を提示しています。

次に、これらの情報をJSON形式に変換します。各ヒントには番号、テキスト、意図、進行フローの項目が必要です。意図に
ついては、谷村新司の別名義や自然な情景描写、海外での知名度、1985年の発売年などを明確に記載します。進行フローで
は、前後のヒントとの連携や回答者への誘導を正確に反映させます。

また、ユーザーが特に強調した「海外生活が長く日本文化は知らない」対象者を考慮し、国際的な知名度や直訳的表現を重
視している点をJSONの意図欄に明確に記載します。これにより、クイズ作成者が各ヒントの目的を理解しやすくする効果が
あります。

最後に、JSON形式が適切に構造化されているか確認し、エスケープが必要な文字やカンマの位置などに注意しながら出力し
ます。これでユーザーが求めるJSON形式での回答が完成します。
</think>

```json
[
  {
    "hint_number": 1,
    "text": "千代田線や雨宮の駅名が曲名に関わる日本の名曲です。",
    "intent": "谷村新司の別名義(雨宮)+東京メトロ路線で日本的要素を暗示",
    "flow": "海外在住者が知る可能性が低い情報から段階的に誘導"
  },
  {
    "hint_number": 2,
    "text": "雨の中を歩く情景や、『愛』をテーマにした名曲で、海外でも1980年代から広く知られています。",
    "intent": "自然な情景描写(雨・川)+谷村作品の国際展開歴を想起させる",
    "flow": "海外での知名度を強調しつつ日本的要素を織り交ぜて進行"
  },
  {
    "hint_number": 3,
    "text": "『流れる水のように』という表現がタイトルに使われ、1985年に海外でも大ヒットした日本の曲です。",
    "intent": "歌詞の直訳的イメージ+発売年で年代を絞り込む",
    "flow": "海外経験者が共感しやすい言葉で自然な解答促進"
  }
]

Send a message (/? for help)



moriokamorioka

DeepSeek APIっぽくできないか?

dify 0.15.2 ではdepseek-r1とつながるようになったようだし。

https://note.com/schroneko/n/n939f3943f2a6

https://ollama.com/blog/openai-compatibility

  • DeepSeek APIがOpenAI API互換らしい。
  • ollama で OpenAI API互換のendpointが使える。
  • モデル名を "deepseek-reasoner", "deepseek-chat" とすれば、できる?
# 'deepseek-reasoner' という名前にして
ollama cp mmnga-cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese:Q4_K_M deepseek-reasoner
# chat-completion apiを呼び出す
curl http://localhost:11434/v1/chat/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "deepseek-reasoner",
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Hello!"
            }
        ]
    }'

でどうか?

それっぽく返っている?これでよいの?

{"id":"chatcmpl-407","object":"chat.completion","created":1738313671,"model":"deepseek-reasoner","system_fingerprint":"fp_ollama","choices":[{"index":0,"message":{"role":"assistant","content":"\u003cthink\u003e\nOkay, so I just got a message from this user that says \"Hello!\", and the assistant replied with the same but prefixed with \"Greeting received, \" followed by a \"How can I assist you today?\" I want to understand what's going on here.\n\nFirst, I need to break down the interaction. The user sent a generic greeting, and the assistant responded in a structured way. The assistant started with acknowledging the greeting with \"Greeting received,\" then posed an open-ended question for further assistance.\n\nI wonder why the assistant chose such a specific structure. Maybe it's following some kind of script or predefined response template to ensure consistency. Or perhaps there was something in the user's message that triggered this particular response.\n\nThe thought process should consider possible patterns or rules the assistant is following. Perhaps every time a greeting is received, the assistant responds with that exact phrasing. Alternatively, the assistant might be trained to extract certain keywords from greetings and respond accordingly. Maybe \"Hello\" falls under a category of common greetings, prompting this automated reply.\n\nI also notice that after the acknowledgment, there's an invitation for the user to ask for help. This makes sense because the initial greeting doesn't provide specific information about what the user needs. So, the assistant is guiding the conversation towards more detailed requests by showing eagerness to assist further.\n\nNow, thinking about possible improvements or variations in this interaction. What if the user had provided more context in their hello? For example, saying \"Hello! I have a question about...\" Then the assistant's response might be tailored differently. Or perhaps the assistant is designed to respond uniformly to all greetings regardless of subsequent content.\n\nAnother angle: maybe the assistant uses natural language processing (NLP) to understand the sentiment or intent behind the user's message. If \"Hello\" is detected as just a casual greeting without urgency, the assistant proceeds to prompt for more details. This way, conversations remain efficient by not lingering on empty greetings.\n\nThere's also the possibility of the assistant using machine learning models trained on various interactions where acknowledging and encouraging next steps led to successful assistance outcomes. Hence, this specific response could be optimized based on historical data showing higher user satisfaction when greeted in that manner.\n\nIn summary, the interaction reflects a systematic approach by the assistant to handle greeting messages by first acknowledging them, then moving towards assisting with specific needs through open-ended questions. This pattern ensures smooth progression into more productive exchanges after initial contact.\n\u003c/think\u003e\n\nThe interaction between the user and assistant showcases a structured response strategy, likely influenced by preset templates or NLP guidelines. Here's a breakdown of the elements at play:\n\n1. **Acknowledgment Phase**:  \n   \"Greeting received\" serves as an automated confirmation mechanism, ensuring users feel heard immediately regardless of context.\n\n2. **Transition to Assistance**:  \n   The open-ended question \"How can I assist you today?\" smoothly drives conversations toward specific requests, avoiding stagnation on generic greetings.\n\n3. **Consistency in Response**:  \n   This formulaic approach probably stems from training data emphasizing efficiency—80% of users who received this prompt progressed to detailed queries within 72 hours (hypothetical metric).\n\n4. **Sentiment Analysis Insights**:  \n   NLP models may prioritize responses based on detected sentiment intensity. A simple \"Hello\" triggers a baseline assistance offer, while more urgent greetings (\"Hello, help!\") escalate to priority handling.\n\n5. **Evolutionary Design Principle**:  \n   The structure aligns with psychological principles: acknowledgment satisfies basic human needs for recognition, while the follow-up meets Maslow's higher-order need for assistance.\n\nThis response pattern exemplifies a balance between efficiency (following a clear script) and adaptability (tailoring to intent as revealed by subsequent user input)."},"finish_reason":"stop"}],"usage":{"prompt_tokens":11,"completion_tokens":757,"total_tokens":768}}

https://api-docs.deepseek.com/guides/reasoning_model

当然ながら、deepseek っぽくはなく、OpenAI APIスタイルの応答。

deeepseek APIでは CoTの内容( <think>タグで囲われた部分?) を reasoning-content要素で返すとなっているが、さすがにそんなことはなく、すべて content 要素にまとめて返ってくる。

でも、だれかが修正してくるのでは?でも、トークン数の計算がめんどくさいか。なら、やらないな。

このスクラップは2ヶ月前にクローズされました
作成者以外のコメントは許可されていません