MLXDocs
Local AI · mlx-lm

Chat completions

Markdown

POST /v1/chat/completions on mlx-lm's server takes a chat and returns the model's answer. Every field mlx-lm documents, with its default.

POSThttp://127.0.0.1:8080/v1/chat/completions

The fields below are the ones listed in mlx-lm's server guide (SERVER.md, mlx-lm 0.32.2). The API is meant to be similar to OpenAI's chat API; a field that isn't listed here isn't documented by mlx-lm.

Request

ParameterTypeNotes
messagesarrayThe conversation so far. Each message has a role (for example user, assistant) and content (the text).
modelstringOptional. A local path (relative to the folder the server was started in) or a Hugging Face repo id.
role_mappingobjectOptional. Custom role prefixes for the prompt; the defaults are used without it.
stopstring or arrayOptional. Sequences on which generation stops.
max_tokensintegerOptional. Most tokens to generate. Default 512.
streambooleanOptional. Send the answer as it is generated. Default false.
temperaturenumberOptional. Sampling temperature. Default 0.0.
top_pnumberOptional. Nucleus sampling. Default 1.0.
top_kintegerOptional. Top-k sampling. Default 0 (off).
min_pnumberOptional. Min-p sampling. Default 0.0 (off).
repetition_penaltynumberOptional. Multiplicative penalty on repeated tokens. Default 0.0 (off).
repetition_context_sizeintegerOptional. Window for the repetition penalty. Default 20.
presence_penaltynumberOptional. Additive penalty on tokens that appeared before. Default 0.0 (off).
presence_context_sizeintegerOptional. Window for the presence penalty. Default 20.
frequency_penaltynumberOptional. Additive penalty in proportion to how often a token appeared. Default 0.0 (off).
frequency_context_sizeintegerOptional. Window for the frequency penalty. Default 20.
logit_biasobjectOptional. Token id → bias.
logprobsintegerOptional. Return this many top tokens with their log probabilities for each output token, 1 to 10.
adaptersstringOptional. Path to low-rank adapters, relative to the folder the server was started in.
draft_modelstringOptional. A smaller model for speculative decoding; null unloads it.
num_draft_tokensintegerOptional. Tokens the draft model predicts at once. Default 3.

Response

FieldNotes
idA unique id for the chat.
system_fingerprintA unique id for the system.
objectchat.completion, or chat.completion.chunk when streaming.
modelThe model repo or path.
createdWhen the request was processed.
choicesA list of outputs, each with index, logprobs (token_logprobs, tokens, top_logprobs), finish_reason (stop or length) and message.
usageprompt_tokens, completion_tokens and total_tokens.

Example

curl localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
     "messages": [{"role": "user", "content": "Say this is a test!"}],
     "temperature": 0.7
   }'

A finish_reason of length means the answer hit max_tokens: raise it and ask again.