New Tool Radar

Local AI

How to Pass a System Prompt to mlx_lm.generate

Give mlx_lm.generate a system prompt and confirm the answer follows it.

Schematic with three steps: Pick a system prompt; Run generate with the flag; Check the answer follows the prompt.

A system prompt sets the role and the rules for a run: answer briefly, answer as a code reviewer, or answer in plain English. mlx_lm.generate takes a system prompt as a flag, so you can set the behaviour for one run without changing the model.

What you need

  • A Mac with Apple Silicon and mlx-lm installed
  • A downloaded MLX model, for example an instruct model
  • A terminal with the virtual environment activated

Step 1: Pick a system prompt

Write one sentence that sets the role, for example ‘Answer in short bullet points and say clearly when you are not sure.’ Keep it to one rule so the effect is easy to see.

Step 2: Run generate with the flag

Run mlx_lm.generate with the model, a prompt, and –system-prompt followed by your sentence. Use an instruct model, because base models do not follow instructions well.

Step 3: Check the answer follows the prompt

Read the answer and confirm it follows the system prompt: short bullets, and a clear statement when it is not sure. If it ignores the prompt, check you used an instruct model with a chat template.

Worked example: synthetic system prompt run

The table below is a synthetic example. It shows the expected result of using a system prompt; no run was made.

Run System prompt Answer shape
No system prompt None Long paragraph
With system prompt Short bullets, say when unsure Short bullets
With system prompt Short bullets, say when unsure Says when unsure

Expected result (UNTESTED). the run without a system prompt gives a long paragraph, while the run with one gives short bullets and says when it is not sure. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • A system prompt works best with an instruct model; a base model may ignore it.
  • One long system prompt can crowd out the question, so keep it to one or two rules.
  • The flag applies to that run only; save a working prompt in a script or a note.

Sources

See all