Category: Technical Notes

  • AI Model from Hugging Face to Ollama

    Modelfile Setup Guide

    To import a model from Hugging Face into Ollama for coding, do the following.

    1. Download the model

    $ ollama pull hf.co/Jackrong/Qwen3.5-9B-Claude-4.6-Opus-Reasoning-Distilled-GGUF:Q8_0
    

    2. Find the correct blob

    Hint: It’s the big one.

    $ ollama show --modelfile hf.co/Jackrong/Qwen3.5-9B-Claude-4.6-Opus-Reasoning-Distilled-GGUF:Q8_0 \
      | awk '/^FROM/ {print $2}' \
      | xargs -r du -h
    

    3. Create a new Modelfile

    Set the context length to at least 16k or 32k, and add the TEMPLATE to enable tool calling.

    Sources:
    The TEMPLATE is from a GitHub comment.
    The additional Coding parameters for Qwen 3.5 are from hf.co. Parameters for additional uses are also available.

    # Modelfile generated by "ollama show"
    # To build a new Modelfile based on this, replace FROM with:
    # FROM hf.co/Jackrong/Qwen3.5-9B-Claude-4.6-Opus-Reasoning-Distilled-GGUF:Q8_0
    
    FROM /usr/share/ollama/.ollama/models/blobs/sha256-01ab75e862bf61c2fd20babc55d396181580722b7af76ec4ebfb83224218c723
    
    PARAMETER num_ctx 32768
    PARAMETER temperature 0.6
    PARAMETER top_p 0.95
    PARAMETER top_k 20
    PARAMETER min_p 0.0
    PARAMETER presence_penalty 0.0
    PARAMETER repeat_penalty 1.0
    
    TEMPLATE """{{- if .Suffix }}<|fim_prefix|>{{ .Prompt }}<|fim_suffix|>{{ .Suffix }}<|fim_middle|>
    {{- else -}}
    {{- $lastUserIdx := -1 -}}
    {{- range $idx, $msg := .Messages -}}
    {{- if eq $msg.Role "user" }}{{ $lastUserIdx = $idx }}{{ end -}}
    {{- end }}
    {{- 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 }}
    {{ else if eq .Role "assistant" }}<|im_start|>assistant
    {{- if .Content }}
    {{ .Content }}
    {{- else if .ToolCalls }}
    {{- range .ToolCalls }}
    <tool_call>
    {"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
    </tool_call>
    {{- end }}
    {{- 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 }}
    {{- end }}
    """
    

    4. Create a new model from the Modelfile

    $ ollama create Qwen3.5-Coder-Distilled