Tune Qwen models in Model Studio through the API (HTTP) or CLI (shell). Three tuning methods are supported: supervised fine-tuning (SFT), continual pre-training (CPT), and direct preference optimization (DPO).
Prerequisites
- Review Introduction to model fine-tuning to understand its concepts, process, and data requirements.
- Activate the service and obtain an API key. Get an API key.
- Grant the RAM user (RAM user) the necessary invocation, training, and deployment permissions.
NoteThe API supports only token-based billing. To use model training units (prepaid or postpaid), create the job in the console.
Upload tuning files
Preparing fine-tuning files
For the data format specifications, packaging rules, size and quantity limits, and API upload quotas of each training method (SFT/DPO/CPT), see Tuning data upload rules. The file formats supported by each training method:
Training method | File format |
|---|---|
SFT text generation | jsonl (ChatML messages multi-turn structure) |
SFT multimodal (images/videos) | zip (data.jsonl + image/video files) |
DPO | jsonl (chosen/rejected preference pairs) |
CPT | jsonl (plain text {text}) |
Evaluation set | xlsx (prompt&completion) / jsonl, xls, xlsx (ChatML-messages) |
You can also download a data template from the Model Studio console. | Below the file upload area on the Create Dataset page, you can find the data template download link. |
Upload fine-tuning files to Model Studio
OpenAI-compatible Files API
import os
from pathlib import Path
from openai import OpenAI
client = OpenAI(
# If you have not configured an environment variable, replace the following line with api_key="sk-xxx" and use your Model Studio API key.
# API keys for the Singapore and China (Beijing) regions are different. Get an API key: https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# The following is the URL for the Singapore region. If you use the service in the China (Beijing) region, replace the URL with: https://dashscope.aliyuncs.com/compatible-mode/v1
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
# test.jsonl is a local sample file.
file_object = client.files.create(file=Path("test.jsonl"), purpose="fine-tune")
print(file_object.model_dump_json())
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.files.*;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
// Create a client and use the API key from the environment variable.
OpenAIClient client = OpenAIOkHttpClient.builder()
// API keys for the Singapore and China (Beijing) regions are different. Get an API key: https://www.alibabacloud.com/help/model-studio/get-api-key
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
// The following is the URL for the Singapore region. If you use the service in the China (Beijing) region, replace the URL with: https://dashscope.aliyuncs.com/compatible-mode/v1
.baseUrl("https://dashscope-intl.aliyuncs.com/compatible-mode/v1")
.build();
// Set the file path. Modify the path and filename as needed.
Path filePath = Paths.get("src/main/java/org/example/test.txt");
// Create file upload parameters.
FileCreateParams params = FileCreateParams.builder()
.file(filePath)
.purpose(FilePurpose.of("fine-tune"))
.build();
// Upload the file.
FileObject fileObject = client.files().create(params);
System.out.println(fileObject);
}
}
# ======= Important =======
# API keys for the Singapore and China (Beijing) regions are different. Get an API key: https://www.alibabacloud.com/help/model-studio/get-api-key
# The following is the URL for the Singapore region. If you use the service in the China (Beijing) region, replace the URL with: https://dashscope.aliyuncs.com/compatible-mode/v1/files
# === Delete this comment before running ===
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/files \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
--form 'file=@"test.jsonl"' \
--form 'purpose="fine-tune"'
NoteLimitations:
- The maximum size of a single file is 300 MB.
- The total storage quota for all valid (non-deleted) files is 100 GB.
- The total count quota for all valid (non-deleted) files is 10000 files.
- File storage has no time limit.
Tuning files uploaded through the API are visible and usable both on the model fine-tuning page of the Model Studio console and in API calls.
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.
Query fine-tuning job details
Use the job_id to query the task status. For more information, see Query fine-tuning task status.
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
- List jobs: List fine-tuning jobs
- Cancel job: Cancel fine-tuning job
- Delete job: Delete fine-tuning job
Model deployment and invocation
Model deployment
To deploy the model, go to the Dedicated Deployment console.
Model invocation
Once deployment status is RUNNING, invoke the fine-tuned model like any other model.
You can also get the Model Code from the Dedicated Deployment console.
Usage and parameters: 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"
}
}'