Skip to main content

Fine-tune with the API or CLI

Learn how to tune Qwen models in Alibaba Cloud Model Studio using the API (HTTP) and the command line (shell). Model tuning involves three methods: supervised fine-tuning (SFT), continual pre-training (CPT), and direct preference optimization (DPO).

This topic is applicable only to the China (Beijing) region.

Prerequisites

The API supports only token-based billing for training jobs. To use model training units (prepaid or postpaid), create the job in the console.

Tuning file upload

Preparing fine-tuning files

  • SFT training set
  • SFT thinking model
  • SFT visual understanding (Qwen-VL)
  • DPO dataset
  • CPT training set
SFT ChatML (Chat Markup Language) format training data supports multi-turn conversations and various role settings.
The OpenAI name and weight parameters are not supported. All assistant outputs will be trained.
# A single line of training data (in JSON format) has the following typical structure when expanded:
{"messages": [
  {"role": "system", "content": "System input 1"},
  {"role": "user", "content": "User input 1"},
  {"role": "assistant", "content": "Expected model output 1"},
  {"role": "user", "content": "User input 2"},
  {"role": "assistant", "content": "Expected model output 2"}
  ...
]}
For information about the differences between system, user, and assistant, see Overview. Sample training datasets: SFT-ChatML_format_example.jsonl, SFT-ChatML_format_example.xlsx. XLS and XLSX formats support only single-turn conversations.All assistant lines in a single training data entry support the "loss_weight" parameter, which sets the relative importance of that line during training. (Range: 0.0 to 1.0. A larger value indicates higher importance.)
This parameter is available for invitational preview. To use it, contact your account manager.
 {"role": "assistant", "content": "Expected model output 1", "loss_weight": 1.0},
 {"role": "assistant", "content": "Expected model output 2", "loss_weight": 0.5}

You can also download a data template from the Model Studio console.

image

Upload fine-tuning fileto Model Studio

  • DashScope API
For Windows CMD, replace ${DASHSCOPE_API_KEY} with %DASHSCOPE_API_KEY%. For PowerShell, replace it with $env:DASHSCOPE_API_KEY.
curl --request POST \
'https://dashscope.aliyuncs.com/api/v1/files' \
--header 'Authorization: Bearer '${DASHSCOPE_API_KEY} \
--form 'files=@"/path/to/your/file.jsonl"' \
--form 'purpose="fine-tune"' \
--form 'descriptions="a sample fine-tune data file for qwen"'
Limitations:
  • The maximum size of a single file is 300 MB.
  • The total size of all non-deleted files is limited to 5 GB.
  • You can store a maximum of 100 non-deleted files.
  • Files are stored indefinitely.
For more details, see model fine-tuning file management service.
Sample response:
{
  "request_id":"xx",
  "data":{
    "uploaded_files":[{
      "file_id":"976bd01a-f30b-4414-86fd-50c54486e3ef",
      "name":"qwen-fine-tune-sample.jsonl"}],
   "failed_uploads":[]}
 }

Model fine-tuning

Creating, querying, logging, checkpoint management, and listing/canceling/deleting fine-tuning jobs are all done via API. Each interface's complete request/response parameters and examples are on the corresponding API page; this section only outlines the workflow.

Create a fine-tuning job

Call the Create a fine-tuning job interface (POST /api/v1/fine-tunes) to start training. See that page for the request body (model / training_datasets / hyper_parameters / training_type, etc.) and response examples.

Retrieve a fine-tuning job

Use the job_id to query the task status. See Query fine-tuning task details.

Get fine-tuning job logs

Retrieve training logs via the Get fine-tuning task logs interface (GET /api/v1/fine-tunes/{job_id}/logs).

Query and publish model checkpoints

Checkpoints produced by fine-tuning can be listed and exported (published):
  • List checkpoints: see List Checkpoint (GET /api/v1/fine-tunes/{job_id}/checkpoints).
  • Export (publish) a specific checkpoint: see Export Checkpoint (GET /api/v1/fine-tunes/{job_id}/export/{checkpoint}?model_name=...), asynchronous; poll status via the list interface.

More fine-tuning operations

API reference

This topic provides a reference for DashScope command-line calls. For details on API calls, see API details.

Model deployment and invocation

Model deployment

For other deployment methods, see Deploy a model by using the API.
curl "https://dashscope.aliyuncs.com/api/v1/deployments" \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
    "model_name": "qwen3-8b-ft-202511132025-0260",
    "plan": "lora",
    "capacity": 1,
    "name": "qwen3-8b-ft"
}'

Query deployment status

Once the deployment status is RUNNING, you can invoke the model.
curl 'https://dashscope.aliyuncs.com/api/v1/deployments/<your_model_instance_id>' \
--header 'Authorization: Bearer '${DASHSCOPE_API_KEY}  \
--header 'Content-Type: application/json'
For other deployment operations, such as scaling and deprovisioning, see Model Deployment - API Details.

Model invocation

Once the model deployment status is RUNNING, you can invoke the fine-tuned model just like any other model. You can also get the Model Code from the Dedicated Deployment console. For details on usage and parameters, see the DashScope API Reference.
curl 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation' \
--header 'Authorization: Bearer '${DASHSCOPE_API_KEY}  \
--header 'Content-Type: application/json' \
--data '{
    "model": "<your_model_instance_id>",
    "input":{
        "messages":[
            {
                "role": "user",
                "content": "Who are you?"
            }
        ]
    },
    "parameters": {
        "result_format": "message"
    }
}'

Model evaluation

The model evaluation feature is exclusive to the Alibaba Cloud Model Studio console. Go to the Model Evaluation page to evaluate model performance. For more information, see Model Evaluation.
Token Plan
Model Playground
Model Compression
Statistics and Monitoring
Support