JustinLin610 commited on
Commit
aa2b6a4
·
verified ·
1 Parent(s): 539969e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +53 -22
README.md CHANGED
@@ -44,16 +44,29 @@ KeyError: 'qwen3'
44
  ```
45
 
46
  The following contains a code snippet illustrating how to use the model generate content based on given inputs.
 
47
  ```python
48
  from mlx_lm import load, generate
 
49
  model, tokenizer = load("Qwen/Qwen3-4B-MLX-4bit")
50
  prompt = "hello, Introduce yourself, and what can you do ?"
 
51
  if tokenizer.chat_template is not None:
52
  messages = [{"role": "user", "content": prompt}]
53
  prompt = tokenizer.apply_chat_template(
54
- messages, add_generation_prompt=True
 
55
  )
56
- response = generate(model, tokenizer, prompt=prompt, verbose=True, max_tokens=1024)
 
 
 
 
 
 
 
 
 
57
  ```
58
 
59
  ## Switching Between Thinking and Non-Thinking Mode
@@ -107,6 +120,8 @@ Here is an example of a multi-turn conversation:
107
 
108
  ```python
109
  from mlx_lm import load, generate
 
 
110
  class QwenChatbot:
111
  def __init__(self, model_name="Qwen/Qwen3-4B-MLX-4bit"):
112
  self.model, self.tokenizer = load(model_name)
@@ -121,13 +136,20 @@ class QwenChatbot:
121
  add_generation_prompt=True
122
  )
123
 
124
- response = generate(self.model, self.tokenizer, prompt=text, verbose=True, max_tokens=32768)
 
 
 
 
 
 
125
  # Update history
126
  self.history.append({"role": "user", "content": user_input})
127
  self.history.append({"role": "assistant", "content": response})
128
 
129
  return response
130
 
 
131
  # Example Usage
132
  if __name__ == "__main__":
133
  chatbot = QwenChatbot()
@@ -143,7 +165,7 @@ if __name__ == "__main__":
143
  user_input_2 = "Then, how many r's in blueberries? /no_think"
144
  print(f"User: {user_input_2}")
145
  response_2 = chatbot.generate_response(user_input_2)
146
- print(f"Bot: {response_2}")
147
  print("----------------------")
148
 
149
  # Third input with /think
@@ -162,52 +184,61 @@ if __name__ == "__main__":
162
  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.
163
 
164
  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.
 
165
  ```python
166
  from qwen_agent.agents import Assistant
167
 
168
  # Define LLM
169
  llm_cfg = {
170
- 'model': 'Qwen3-4B-MLX-4bit',
171
 
172
  # Use the endpoint provided by Alibaba Model Studio:
173
- # 'model_type': 'qwen_dashscope',
174
- # 'api_key': os.getenv('DASHSCOPE_API_KEY'),
175
 
176
  # Use a custom endpoint compatible with OpenAI API:
177
- 'model_server': 'http://localhost:8000/v1', # api_base
178
- 'api_key': 'EMPTY',
179
 
180
  # Other parameters:
181
- # 'generate_cfg': {
182
- # # Add: When the response content is `<think>this is the thought</think>this is the answer;
183
- # # Do not add: When the response has been separated by reasoning_content and content.
184
- # 'thought_in_content': True,
185
- # },
186
  }
187
 
188
  # Define Tools
189
  tools = [
190
- {'mcpServers': { # You can specify the MCP configuration file
191
- 'time': {
192
- 'command': 'uvx',
193
- 'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
 
194
  },
195
  "fetch": {
196
  "command": "uvx",
197
- "args": ["mcp-server-fetch"]
198
- }
199
  }
200
  },
201
- 'code_interpreter', # Built-in tools
202
  ]
203
 
204
  # Define Agent
205
  bot = Assistant(llm=llm_cfg, function_list=tools)
206
 
207
  # Streaming generation
208
- messages = [{'role': 'user', 'content': 'https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen'}]
 
 
 
 
 
 
209
  for responses in bot.run(messages=messages):
210
  pass
 
211
  print(responses)
212
  ```
213
 
 
44
  ```
45
 
46
  The following contains a code snippet illustrating how to use the model generate content based on given inputs.
47
+
48
  ```python
49
  from mlx_lm import load, generate
50
+
51
  model, tokenizer = load("Qwen/Qwen3-4B-MLX-4bit")
52
  prompt = "hello, Introduce yourself, and what can you do ?"
53
+
54
  if tokenizer.chat_template is not None:
55
  messages = [{"role": "user", "content": prompt}]
56
  prompt = tokenizer.apply_chat_template(
57
+ messages,
58
+ add_generation_prompt=True
59
  )
60
+
61
+ response = generate(
62
+ model,
63
+ tokenizer,
64
+ prompt=prompt,
65
+ verbose=True,
66
+ max_tokens=1024
67
+ )
68
+
69
+ print(response)
70
  ```
71
 
72
  ## Switching Between Thinking and Non-Thinking Mode
 
120
 
121
  ```python
122
  from mlx_lm import load, generate
123
+
124
+
125
  class QwenChatbot:
126
  def __init__(self, model_name="Qwen/Qwen3-4B-MLX-4bit"):
127
  self.model, self.tokenizer = load(model_name)
 
136
  add_generation_prompt=True
137
  )
138
 
139
+ response = generate(
140
+ self.model,
141
+ self.tokenizer,
142
+ prompt=text,
143
+ verbose=True,
144
+ max_tokens=32768
145
+ )
146
  # Update history
147
  self.history.append({"role": "user", "content": user_input})
148
  self.history.append({"role": "assistant", "content": response})
149
 
150
  return response
151
 
152
+
153
  # Example Usage
154
  if __name__ == "__main__":
155
  chatbot = QwenChatbot()
 
165
  user_input_2 = "Then, how many r's in blueberries? /no_think"
166
  print(f"User: {user_input_2}")
167
  response_2 = chatbot.generate_response(user_input_2)
168
+ print(f"Bot: {response_2}")
169
  print("----------------------")
170
 
171
  # Third input with /think
 
184
  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.
185
 
186
  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.
187
+
188
  ```python
189
  from qwen_agent.agents import Assistant
190
 
191
  # Define LLM
192
  llm_cfg = {
193
+ "model": "Qwen3-4B-MLX-4bit",
194
 
195
  # Use the endpoint provided by Alibaba Model Studio:
196
+ # "model_type": "qwen_dashscope",
197
+ # "api_key": os.getenv("DASHSCOPE_API_KEY"),
198
 
199
  # Use a custom endpoint compatible with OpenAI API:
200
+ "model_server": "http://localhost:8000/v1", # api_base
201
+ "api_key": "EMPTY",
202
 
203
  # Other parameters:
204
+ # "generate_cfg": {
205
+ # # Add: When the response content is `<think>this is the thought</think>this is the answer;
206
+ # # Do not add: When the response has been separated by reasoning_content and content.
207
+ # "thought_in_content": True,
208
+ # },
209
  }
210
 
211
  # Define Tools
212
  tools = [
213
+ {
214
+ "mcpServers": { # You can specify the MCP configuration file
215
+ "time": {
216
+ "command": "uvx",
217
+ "args": ["mcp-server-time", "--local-timezone=Asia/Shanghai"],
218
  },
219
  "fetch": {
220
  "command": "uvx",
221
+ "args": ["mcp-server-fetch"],
222
+ },
223
  }
224
  },
225
+ "code_interpreter", # Built-in tools
226
  ]
227
 
228
  # Define Agent
229
  bot = Assistant(llm=llm_cfg, function_list=tools)
230
 
231
  # Streaming generation
232
+ messages = [
233
+ {
234
+ "role": "user",
235
+ "content": "https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen",
236
+ }
237
+ ]
238
+
239
  for responses in bot.run(messages=messages):
240
  pass
241
+
242
  print(responses)
243
  ```
244