cjpais commited on
Commit
34b4772
·
verified ·
1 Parent(s): 2422e95

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +407 -0
README.md ADDED
@@ -0,0 +1,407 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ pipeline_tag: text-generation
4
+ license_link: LICENSE
5
+ base_model:
6
+ - Qwen/Qwen3-0.6B-Base
7
+ quantized_by: bartowski
8
+ tags:
9
+ - llamafile
10
+ - chat
11
+ ---
12
+
13
+ # Qwen 3 0.6B - llamafile
14
+
15
+ - Model creator: [Qwen](https://huggingface.co/Qwen/)
16
+ - Original model: [Qwen/Qwen3-0.6B](https://huggingface.co/Qwen/Qwen3-0.6B)
17
+
18
+ Mozilla packaged the Qwen 3 models into executable weights that we
19
+ call [llamafiles](https://github.com/Mozilla-Ocho/llamafile). This gives
20
+ you the easiest fastest way to use the model on Linux, MacOS, Windows,
21
+ FreeBSD, OpenBSD and NetBSD systems you control on both AMD64 and ARM64.
22
+
23
+ *Software Last Updated: 2025-05-14*
24
+
25
+ *Llamafile Version: 0.9.3*
26
+
27
+ ## Quickstart
28
+
29
+ To get started, you need both the Qwen 3 weights, and the llamafile
30
+ software. Both of them are included in a single file, which can be
31
+ downloaded and run as follows:
32
+
33
+ ```
34
+ wget https://huggingface.co/Mozilla/Qwen3-0.6B-llamafile/resolve/main/Qwen_Qwen3-0.6B-Q4_K_M.llamafile
35
+ chmod +x Qwen_Qwen3-0.6B-Q4_K_M.llamafile
36
+ ./Qwen_Qwen3-0.6B-Q4_K_M.llamafile
37
+ ```
38
+
39
+ The default mode of operation for these llamafiles is our new command
40
+ line chatbot interface.
41
+
42
+ ## Usage
43
+
44
+ You can use triple quotes to ask questions on multiple lines. You can
45
+ pass commands like `/stats` and `/context` to see runtime status
46
+ information. You can change the system prompt by passing the `-p "new
47
+ system prompt"` flag. You can press CTRL-C to interrupt the model.
48
+ Finally CTRL-D may be used to exit.
49
+
50
+ If you prefer to use a web GUI, then a `--server` mode is provided, that
51
+ will open a tab with a chatbot and completion interface in your browser.
52
+ For additional help on how it may be used, pass the `--help` flag. The
53
+ server also has an OpenAI API compatible completions endpoint that can
54
+ be accessed via Python using the `openai` pip package.
55
+
56
+ ```
57
+ ./Qwen_Qwen3-0.6B-Q4_K_M.llamafile --server
58
+ ```
59
+
60
+ An advanced CLI mode is provided that's useful for shell scripting. You
61
+ can use it by passing the `--cli` flag. For additional help on how it
62
+ may be used, pass the `--help` flag.
63
+
64
+ ```
65
+ ./Qwen_Qwen3-0.6B-Q4_K_M.llamafile --cli -p 'four score and seven' --log-disable
66
+ ```
67
+
68
+ ## Troubleshooting
69
+
70
+ Having **trouble?** See the ["Gotchas"
71
+ section](https://github.com/mozilla-ocho/llamafile/?tab=readme-ov-file#gotchas-and-troubleshooting)
72
+ of the README.
73
+
74
+ On Linux, the way to avoid run-detector errors is to install the APE
75
+ interpreter.
76
+
77
+ ```sh
78
+ sudo wget -O /usr/bin/ape https://cosmo.zip/pub/cosmos/bin/ape-$(uname -m).elf
79
+ sudo chmod +x /usr/bin/ape
80
+ sudo sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
81
+ sudo sh -c "echo ':APE-jart:M::jartsr::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
82
+ ```
83
+
84
+ On Windows there's a 4GB limit on executable sizes.
85
+
86
+ ## Context Window
87
+
88
+ This model has a max context window size of 128k tokens. By default, a
89
+ context window size of 8192 tokens is used. You can ask llamafile
90
+ to use the maximum context size by passing the `-c 0` flag. That's big
91
+ enough for a small book. If you want to be able to have a conversation
92
+ with your book, you can use the `-f book.txt` flag.
93
+
94
+ ## GPU Acceleration
95
+
96
+ On GPUs with sufficient RAM, the `-ngl 999` flag may be passed to use
97
+ the system's NVIDIA or AMD GPU(s). On Windows, only the graphics card
98
+ driver needs to be installed if you own an NVIDIA GPU. On Windows, if
99
+ you have an AMD GPU, you should install the ROCm SDK v6.1 and then pass
100
+ the flags `--recompile --gpu amd` the first time you run your llamafile.
101
+
102
+ On NVIDIA GPUs, by default, the prebuilt tinyBLAS library is used to
103
+ perform matrix multiplications. This is open source software, but it
104
+ doesn't go as fast as closed source cuBLAS. If you have the CUDA SDK
105
+ installed on your system, then you can pass the `--recompile` flag to
106
+ build a GGML CUDA library just for your system that uses cuBLAS. This
107
+ ensures you get maximum performance.
108
+
109
+ For further information, please see the [llamafile
110
+ README](https://github.com/mozilla-ocho/llamafile/).
111
+
112
+ ## About llamafile
113
+
114
+ llamafile is a new format introduced by Mozilla on Nov 20th 2023. It
115
+ uses Cosmopolitan Libc to turn LLM weights into runnable llama.cpp
116
+ binaries that run on the stock installs of six OSes for both ARM64 and
117
+ AMD64.
118
+
119
+ ---
120
+
121
+ # Qwen3-0.6B
122
+
123
+ ## Qwen3 Highlights
124
+
125
+ Qwen3 is the latest generation of large language models in Qwen series, offering a comprehensive suite of dense and mixture-of-experts (MoE) models. Built upon extensive training, Qwen3 delivers groundbreaking advancements in reasoning, instruction-following, agent capabilities, and multilingual support, with the following key features:
126
+
127
+ - **Uniquely support of seamless switching between thinking mode** (for complex logical reasoning, math, and coding) and **non-thinking mode** (for efficient, general-purpose dialogue) **within single model**, ensuring optimal performance across various scenarios.
128
+ - **Significantly enhancement in its reasoning capabilities**, surpassing previous QwQ (in thinking mode) and Qwen2.5 instruct models (in non-thinking mode) on mathematics, code generation, and commonsense logical reasoning.
129
+ - **Superior human preference alignment**, excelling in creative writing, role-playing, multi-turn dialogues, and instruction following, to deliver a more natural, engaging, and immersive conversational experience.
130
+ - **Expertise in agent capabilities**, enabling precise integration with external tools in both thinking and unthinking modes and achieving leading performance among open-source models in complex agent-based tasks.
131
+ - **Support of 100+ languages and dialects** with strong capabilities for **multilingual instruction following** and **translation**.
132
+
133
+ ## Model Overview
134
+
135
+ **Qwen3-0.6B** has the following features:
136
+ - Type: Causal Language Models
137
+ - Training Stage: Pretraining & Post-training
138
+ - Number of Parameters: 0.6B
139
+ - Number of Paramaters (Non-Embedding): 0.44B
140
+ - Number of Layers: 28
141
+ - Number of Attention Heads (GQA): 16 for Q and 8 for KV
142
+ - Context Length: 32,768
143
+
144
+ For more details, including benchmark evaluation, hardware requirements, and inference performance, please refer to our [blog](https://qwenlm.github.io/blog/qwen3/), [GitHub](https://github.com/QwenLM/Qwen3), and [Documentation](https://qwen.readthedocs.io/en/latest/).
145
+
146
+ > [!TIP]
147
+ > If you encounter significant endless repetitions, please refer to the [Best Practices](#best-practices) section for optimal sampling parameters, and set the ``presence_penalty`` to 1.5.
148
+
149
+ ## Quickstart
150
+
151
+ The code of Qwen3 has been in the latest Hugging Face `transformers` and we advise you to use the latest version of `transformers`.
152
+
153
+ With `transformers<4.51.0`, you will encounter the following error:
154
+ ```
155
+ KeyError: 'qwen3'
156
+ ```
157
+
158
+ The following contains a code snippet illustrating how to use the model generate content based on given inputs.
159
+ ```python
160
+ from transformers import AutoModelForCausalLM, AutoTokenizer
161
+
162
+ model_name = "Qwen/Qwen3-0.6B"
163
+
164
+ # load the tokenizer and the model
165
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
166
+ model = AutoModelForCausalLM.from_pretrained(
167
+ model_name,
168
+ torch_dtype="auto",
169
+ device_map="auto"
170
+ )
171
+
172
+ # prepare the model input
173
+ prompt = "Give me a short introduction to large language model."
174
+ messages = [
175
+ {"role": "user", "content": prompt}
176
+ ]
177
+ text = tokenizer.apply_chat_template(
178
+ messages,
179
+ tokenize=False,
180
+ add_generation_prompt=True,
181
+ enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
182
+ )
183
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
184
+
185
+ # conduct text completion
186
+ generated_ids = model.generate(
187
+ **model_inputs,
188
+ max_new_tokens=32768
189
+ )
190
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
191
+
192
+ # parsing thinking content
193
+ try:
194
+ # rindex finding 151668 (</think>)
195
+ index = len(output_ids) - output_ids[::-1].index(151668)
196
+ except ValueError:
197
+ index = 0
198
+
199
+ thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
200
+ content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
201
+
202
+ print("thinking content:", thinking_content)
203
+ print("content:", content)
204
+ ```
205
+
206
+ For deployment, you can use `sglang>=0.4.6.post1` or `vllm>=0.8.5` or to create an OpenAI-compatible API endpoint:
207
+ - SGLang:
208
+ ```shell
209
+ python -m sglang.launch_server --model-path Qwen/Qwen3-0.6B --reasoning-parser qwen3
210
+ ```
211
+ - vLLM:
212
+ ```shell
213
+ vllm serve Qwen/Qwen3-0.6B --enable-reasoning --reasoning-parser deepseek_r1
214
+ ```
215
+
216
+ For local use, applications such as Ollama, LMStudio, MLX-LM, llama.cpp, and KTransformers have also supported Qwen3.
217
+
218
+ ## Switching Between Thinking and Non-Thinking Mode
219
+
220
+ > [!TIP]
221
+ > The `enable_thinking` switch is also available in APIs created by SGLang and vLLM.
222
+ > Please refer to our documentation for [SGLang](https://qwen.readthedocs.io/en/latest/deployment/sglang.html#thinking-non-thinking-modes) and [vLLM](https://qwen.readthedocs.io/en/latest/deployment/vllm.html#thinking-non-thinking-modes) users.
223
+
224
+ ### `enable_thinking=True`
225
+
226
+ By default, Qwen3 has thinking capabilities enabled, similar to QwQ-32B. This means the model will use its reasoning abilities to enhance the quality of generated responses. For example, when explicitly setting `enable_thinking=True` or leaving it as the default value in `tokenizer.apply_chat_template`, the model will engage its thinking mode.
227
+
228
+ ```python
229
+ text = tokenizer.apply_chat_template(
230
+ messages,
231
+ tokenize=False,
232
+ add_generation_prompt=True,
233
+ enable_thinking=True # True is the default value for enable_thinking
234
+ )
235
+ ```
236
+
237
+ In this mode, the model will generate think content wrapped in a `<think>...</think>` block, followed by the final response.
238
+
239
+ > [!NOTE]
240
+ > For thinking mode, use `Temperature=0.6`, `TopP=0.95`, `TopK=20`, and `MinP=0` (the default setting in `generation_config.json`). **DO NOT use greedy decoding**, as it can lead to performance degradation and endless repetitions. For more detailed guidance, please refer to the [Best Practices](#best-practices) section.
241
+
242
+
243
+ ### `enable_thinking=False`
244
+
245
+ We provide a hard switch to strictly disable the model's thinking behavior, aligning its functionality with the previous Qwen2.5-Instruct models. This mode is particularly useful in scenarios where disabling thinking is essential for enhancing efficiency.
246
+
247
+ ```python
248
+ text = tokenizer.apply_chat_template(
249
+ messages,
250
+ tokenize=False,
251
+ add_generation_prompt=True,
252
+ enable_thinking=False # Setting enable_thinking=False disables thinking mode
253
+ )
254
+ ```
255
+
256
+ In this mode, the model will not generate any think content and will not include a `<think>...</think>` block.
257
+
258
+ > [!NOTE]
259
+ > For non-thinking mode, we suggest using `Temperature=0.7`, `TopP=0.8`, `TopK=20`, and `MinP=0`. For more detailed guidance, please refer to the [Best Practices](#best-practices) section.
260
+
261
+ ### Advanced Usage: Switching Between Thinking and Non-Thinking Modes via User Input
262
+
263
+ We provide a soft switch mechanism that allows users to dynamically control the model's behavior when `enable_thinking=True`. Specifically, you can add `/think` and `/no_think` to user prompts or system messages to switch the model's thinking mode from turn to turn. The model will follow the most recent instruction in multi-turn conversations.
264
+
265
+ Here is an example of a multi-turn conversation:
266
+
267
+ ```python
268
+ from transformers import AutoModelForCausalLM, AutoTokenizer
269
+
270
+ class QwenChatbot:
271
+ def __init__(self, model_name="Qwen/Qwen3-0.6B"):
272
+ self.tokenizer = AutoTokenizer.from_pretrained(model_name)
273
+ self.model = AutoModelForCausalLM.from_pretrained(model_name)
274
+ self.history = []
275
+
276
+ def generate_response(self, user_input):
277
+ messages = self.history + [{"role": "user", "content": user_input}]
278
+
279
+ text = self.tokenizer.apply_chat_template(
280
+ messages,
281
+ tokenize=False,
282
+ add_generation_prompt=True
283
+ )
284
+
285
+ inputs = self.tokenizer(text, return_tensors="pt")
286
+ response_ids = self.model.generate(**inputs, max_new_tokens=32768)[0][len(inputs.input_ids[0]):].tolist()
287
+ response = self.tokenizer.decode(response_ids, skip_special_tokens=True)
288
+
289
+ # Update history
290
+ self.history.append({"role": "user", "content": user_input})
291
+ self.history.append({"role": "assistant", "content": response})
292
+
293
+ return response
294
+
295
+ # Example Usage
296
+ if __name__ == "__main__":
297
+ chatbot = QwenChatbot()
298
+
299
+ # First input (without /think or /no_think tags, thinking mode is enabled by default)
300
+ user_input_1 = "How many r's in strawberries?"
301
+ print(f"User: {user_input_1}")
302
+ response_1 = chatbot.generate_response(user_input_1)
303
+ print(f"Bot: {response_1}")
304
+ print("----------------------")
305
+
306
+ # Second input with /no_think
307
+ user_input_2 = "Then, how many r's in blueberries? /no_think"
308
+ print(f"User: {user_input_2}")
309
+ response_2 = chatbot.generate_response(user_input_2)
310
+ print(f"Bot: {response_2}")
311
+ print("----------------------")
312
+
313
+ # Third input with /think
314
+ user_input_3 = "Really? /think"
315
+ print(f"User: {user_input_3}")
316
+ response_3 = chatbot.generate_response(user_input_3)
317
+ print(f"Bot: {response_3}")
318
+ ```
319
+
320
+ > [!NOTE]
321
+ > For API compatibility, when `enable_thinking=True`, regardless of whether the user uses `/think` or `/no_think`, the model will always output a block wrapped in `<think>...</think>`. However, the content inside this block may be empty if thinking is disabled.
322
+ > When `enable_thinking=False`, the soft switches are not valid. Regardless of any `/think` or `/no_think` tags input by the user, the model will not generate think content and will not include a `<think>...</think>` block.
323
+
324
+ ## Agentic Use
325
+
326
+ Qwen3 excels in tool calling capabilities. We recommend using [Qwen-Agent](https://github.com/QwenLM/Qwen-Agent) to make the best use of agentic ability of Qwen3. Qwen-Agent encapsulates tool-calling templates and tool-calling parsers internally, greatly reducing coding complexity.
327
+
328
+ To define the available tools, you can use the MCP configuration file, use the integrated tool of Qwen-Agent, or integrate other tools by yourself.
329
+ ```python
330
+ from qwen_agent.agents import Assistant
331
+
332
+ # Define LLM
333
+ llm_cfg = {
334
+ 'model': 'Qwen3-0.6B',
335
+
336
+ # Use the endpoint provided by Alibaba Model Studio:
337
+ # 'model_type': 'qwen_dashscope',
338
+ # 'api_key': os.getenv('DASHSCOPE_API_KEY'),
339
+
340
+ # Use a custom endpoint compatible with OpenAI API:
341
+ 'model_server': 'http://localhost:8000/v1', # api_base
342
+ 'api_key': 'EMPTY',
343
+
344
+ # Other parameters:
345
+ # 'generate_cfg': {
346
+ # # Add: When the response content is `<think>this is the thought</think>this is the answer;
347
+ # # Do not add: When the response has been separated by reasoning_content and content.
348
+ # 'thought_in_content': True,
349
+ # },
350
+ }
351
+
352
+ # Define Tools
353
+ tools = [
354
+ {'mcpServers': { # You can specify the MCP configuration file
355
+ 'time': {
356
+ 'command': 'uvx',
357
+ 'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
358
+ },
359
+ "fetch": {
360
+ "command": "uvx",
361
+ "args": ["mcp-server-fetch"]
362
+ }
363
+ }
364
+ },
365
+ 'code_interpreter', # Built-in tools
366
+ ]
367
+
368
+ # Define Agent
369
+ bot = Assistant(llm=llm_cfg, function_list=tools)
370
+
371
+ # Streaming generation
372
+ messages = [{'role': 'user', 'content': 'https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen'}]
373
+ for responses in bot.run(messages=messages):
374
+ pass
375
+ print(responses)
376
+ ```
377
+
378
+ ## Best Practices
379
+
380
+ To achieve optimal performance, we recommend the following settings:
381
+
382
+ 1. **Sampling Parameters**:
383
+ - For thinking mode (`enable_thinking=True`), use `Temperature=0.6`, `TopP=0.95`, `TopK=20`, and `MinP=0`. **DO NOT use greedy decoding**, as it can lead to performance degradation and endless repetitions.
384
+ - For non-thinking mode (`enable_thinking=False`), we suggest using `Temperature=0.7`, `TopP=0.8`, `TopK=20`, and `MinP=0`.
385
+ - For supported frameworks, you can adjust the `presence_penalty` parameter between 0 and 2 to reduce endless repetitions. However, using a higher value may occasionally result in language mixing and a slight decrease in model performance.
386
+
387
+ 2. **Adequate Output Length**: We recommend using an output length of 32,768 tokens for most queries. For benchmarking on highly complex problems, such as those found in math and programming competitions, we suggest setting the max output length to 38,912 tokens. This provides the model with sufficient space to generate detailed and comprehensive responses, thereby enhancing its overall performance.
388
+
389
+ 3. **Standardize Output Format**: We recommend using prompts to standardize model outputs when benchmarking.
390
+ - **Math Problems**: Include "Please reason step by step, and put your final answer within \boxed{}." in the prompt.
391
+ - **Multiple-Choice Questions**: Add the following JSON structure to the prompt to standardize responses: "Please show your choice in the `answer` field with only the choice letter, e.g., `"answer": "C"`."
392
+
393
+ 4. **No Thinking Content in History**: In multi-turn conversations, the historical model output should only include the final output part and does not need to include the thinking content. It is implemented in the provided chat template in Jinja2. However, for frameworks that do not directly use the Jinja2 chat template, it is up to the developers to ensure that the best practice is followed.
394
+
395
+ ### Citation
396
+
397
+ If you find our work helpful, feel free to give us a cite.
398
+
399
+ ```
400
+ @misc{qwen3,
401
+ title = {Qwen3},
402
+ url = {https://qwenlm.github.io/blog/qwen3/},
403
+ author = {Qwen Team},
404
+ month = {April},
405
+ year = {2025}
406
+ }
407
+ ```