Instructions to use unsloth/Qwen3.5-4B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use unsloth/Qwen3.5-4B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="unsloth/Qwen3.5-4B") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("unsloth/Qwen3.5-4B") model = AutoModelForImageTextToText.from_pretrained("unsloth/Qwen3.5-4B") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use unsloth/Qwen3.5-4B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "unsloth/Qwen3.5-4B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "unsloth/Qwen3.5-4B", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/unsloth/Qwen3.5-4B
- SGLang
How to use unsloth/Qwen3.5-4B with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "unsloth/Qwen3.5-4B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "unsloth/Qwen3.5-4B", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "unsloth/Qwen3.5-4B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "unsloth/Qwen3.5-4B", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Unsloth Studio
How to use unsloth/Qwen3.5-4B with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for unsloth/Qwen3.5-4B to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for unsloth/Qwen3.5-4B to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for unsloth/Qwen3.5-4B to start chatting
Load model with FastModel
pip install unsloth from unsloth import FastModel model, tokenizer = FastModel.from_pretrained( model_name="unsloth/Qwen3.5-4B", max_seq_length=2048, ) - Docker Model Runner
How to use unsloth/Qwen3.5-4B with Docker Model Runner:
docker model run hf.co/unsloth/Qwen3.5-4B
Add developer role + tool-call fixes to chat_template.jinja (thinking ON default)
Browse files- chat_template.jinja +22 -19
chat_template.jinja
CHANGED
|
@@ -42,6 +42,19 @@
|
|
| 42 |
{%- if not messages %}
|
| 43 |
{{- raise_exception('No messages provided.') }}
|
| 44 |
{%- endif %}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
{%- if tools and tools is iterable and tools is not mapping %}
|
| 46 |
{{- '<|im_start|>system\n' }}
|
| 47 |
{{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
|
|
@@ -51,17 +64,13 @@
|
|
| 51 |
{%- endfor %}
|
| 52 |
{{- "\n</tools>" }}
|
| 53 |
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
|
| 54 |
-
{%- if
|
| 55 |
-
{
|
| 56 |
-
{%- if content %}
|
| 57 |
-
{{- '\n\n' + content }}
|
| 58 |
-
{%- endif %}
|
| 59 |
{%- endif %}
|
| 60 |
{{- '<|im_end|>\n' }}
|
| 61 |
{%- else %}
|
| 62 |
-
{%- if
|
| 63 |
-
{
|
| 64 |
-
{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
|
| 65 |
{%- endif %}
|
| 66 |
{%- endif %}
|
| 67 |
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
|
@@ -75,16 +84,10 @@
|
|
| 75 |
{%- endif %}
|
| 76 |
{%- endif %}
|
| 77 |
{%- endfor %}
|
| 78 |
-
{%- if ns.multi_step_tool %}
|
| 79 |
-
{{- raise_exception('No user query found in messages.') }}
|
| 80 |
-
{%- endif %}
|
| 81 |
{%- for message in messages %}
|
|
|
|
| 82 |
{%- set content = render_content(message.content, true)|trim %}
|
| 83 |
-
{%- if message.role == "
|
| 84 |
-
{%- if not loop.first %}
|
| 85 |
-
{{- raise_exception('System message must be at the beginning.') }}
|
| 86 |
-
{%- endif %}
|
| 87 |
-
{%- elif message.role == "user" %}
|
| 88 |
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 89 |
{%- elif message.role == "assistant" %}
|
| 90 |
{%- set reasoning_content = '' %}
|
|
@@ -141,8 +144,7 @@
|
|
| 141 |
{%- elif loop.last %}
|
| 142 |
{{- '<|im_end|>\n' }}
|
| 143 |
{%- endif %}
|
| 144 |
-
{%-
|
| 145 |
-
{{- raise_exception('Unexpected message role.') }}
|
| 146 |
{%- endif %}
|
| 147 |
{%- endfor %}
|
| 148 |
{%- if add_generation_prompt %}
|
|
@@ -152,4 +154,5 @@
|
|
| 152 |
{%- else %}
|
| 153 |
{{- '<think>\n' }}
|
| 154 |
{%- endif %}
|
| 155 |
-
{%- endif %}
|
|
|
|
|
|
| 42 |
{%- if not messages %}
|
| 43 |
{{- raise_exception('No messages provided.') }}
|
| 44 |
{%- endif %}
|
| 45 |
+
{%- set num_sys = 0 %}
|
| 46 |
+
{%- set merged_system = '' %}
|
| 47 |
+
{%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
|
| 48 |
+
{%- set first = render_content(messages[0].content, false, true)|trim %}
|
| 49 |
+
{%- if messages|length > 1 and (messages[1].role == 'system' or messages[1].role == 'developer') %}
|
| 50 |
+
{%- set second = render_content(messages[1].content, false, true)|trim %}
|
| 51 |
+
{%- set merged_system = first + '\n' + second %}
|
| 52 |
+
{%- set num_sys = 2 %}
|
| 53 |
+
{%- else %}
|
| 54 |
+
{%- set merged_system = first %}
|
| 55 |
+
{%- set num_sys = 1 %}
|
| 56 |
+
{%- endif %}
|
| 57 |
+
{%- endif %}
|
| 58 |
{%- if tools and tools is iterable and tools is not mapping %}
|
| 59 |
{{- '<|im_start|>system\n' }}
|
| 60 |
{{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
|
|
|
|
| 64 |
{%- endfor %}
|
| 65 |
{{- "\n</tools>" }}
|
| 66 |
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
|
| 67 |
+
{%- if merged_system %}
|
| 68 |
+
{{- '\n\n' + merged_system }}
|
|
|
|
|
|
|
|
|
|
| 69 |
{%- endif %}
|
| 70 |
{{- '<|im_end|>\n' }}
|
| 71 |
{%- else %}
|
| 72 |
+
{%- if merged_system %}
|
| 73 |
+
{{- '<|im_start|>system\n' + merged_system + '<|im_end|>\n' }}
|
|
|
|
| 74 |
{%- endif %}
|
| 75 |
{%- endif %}
|
| 76 |
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
|
|
|
| 84 |
{%- endif %}
|
| 85 |
{%- endif %}
|
| 86 |
{%- endfor %}
|
|
|
|
|
|
|
|
|
|
| 87 |
{%- for message in messages %}
|
| 88 |
+
{%- if loop.index0 >= num_sys and message.role != "system" and message.role != "developer" %}
|
| 89 |
{%- set content = render_content(message.content, true)|trim %}
|
| 90 |
+
{%- if message.role == "user" %}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 92 |
{%- elif message.role == "assistant" %}
|
| 93 |
{%- set reasoning_content = '' %}
|
|
|
|
| 144 |
{%- elif loop.last %}
|
| 145 |
{{- '<|im_end|>\n' }}
|
| 146 |
{%- endif %}
|
| 147 |
+
{%- endif %}
|
|
|
|
| 148 |
{%- endif %}
|
| 149 |
{%- endfor %}
|
| 150 |
{%- if add_generation_prompt %}
|
|
|
|
| 154 |
{%- else %}
|
| 155 |
{{- '<think>\n' }}
|
| 156 |
{%- endif %}
|
| 157 |
+
{%- endif %}
|
| 158 |
+
{#- Unsloth fixes - developer role, tool calling #}
|