全部產品
Search
文件中心

:使用 API 或命令列進行模型調優

更新時間:Nov 23, 2026

本文檔以千問模型的調優操作為例進行說明,通過 API (HTTP)和 命令列(Shell)兩種方式,使用阿里雲百鍊提供的模型調優功能。模型調優包含模型微調(SFT)、繼續預訓練(CPT)、模型偏好訓練(DPO)三種模型訓練方式。

前提條件

說明API 僅支援按 Token 計費。如需使用模型訓練單元(預付費或後付費),請前往控制台建立任務。

上傳調優檔案

準備調優檔案

各訓練方式(SFT/DPO/CPT)的資料格式規範、打包規則、大小與數量限制及 API 上傳配額,請參見調優資料上傳規則。各訓練方式支援的檔案格式如下:

訓練方式

檔案格式

SFT 文字生成

jsonl(ChatML messages 多輪結構)

SFT 多模態(圖片/影片)

zip(data.jsonl + 圖片/影片檔案)

DPO

jsonl(chosen/rejected 偏好對)

CPT

jsonl(純文字 {text})

評測集

xlsx(prompt&completion)/ jsonl、xls、xlsx(ChatML-messages)

也可以前往Model Studio 控制台下載資料範本。

在建立資料集頁面的檔案上傳區域下方,可找到資料範本下載連結。

將調優檔案上傳至 Model Studio

OpenAI 相容 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"'

說明使用限制:

  • 單個檔案大小上限為 300 MB。
  • 所有有效(未刪除)檔案的總儲存空間配額為 100 GB。
  • 所有有效(未刪除)檔案的總數量配額為 10000 個。
  • 檔案儲存無時間限制。

透過 API 上傳的調優檔案,在 Model Studio 控制台的模型調優頁面與 API 呼叫中均可見、可用。

模型調優

調優任務的建立、查詢、日誌與快照(Checkpoint)管理,以及列舉/取消/刪除調優任務,均透過 API 完成。各介面完整的請求/回應參數與範例請見對應的 API 頁面;本節僅概述整體工作流程。

建立調優任務

調用建立調優任務介面(POST /api/v1/fine-tunes)啟動訓練。請求體(model / training_datasets / hyper_parameters / training_type 等)與回應範例請見該頁面。

查詢調優任務詳情

使用 job_id 查詢任務狀態。更多資訊請參見查詢調優任務狀態。

擷取調優任務日誌

透過擷取調優任務日誌介面(GET /api/v1/fine-tunes/{job_id}/logs)取得訓練日誌。

查詢與發布模型參數快照

調優產生的參數快照(Checkpoint)可列舉與匯出(發布):

  • 列舉快照:請參見查詢快照列表(GET /api/v1/fine-tunes/{job_id}/checkpoints)。
  • 匯出(發布)特定快照:請參見匯出快照(GET /api/v1/fine-tunes/{job_id}/export/{checkpoint}?model_name=...);該操作為非同步,可透過列舉介面輪詢狀態。

模型調優的更多操作

模型部署與呼叫

模型部署

如需部署模型,請前往專屬部署控制台。

模型呼叫

當部署狀態為 RUNNING 時,即可像呼叫其他模型一樣呼叫調優後的模型。

也可以前往專屬部署控制台取得模型代碼(Model Code)。

使用方法與參數請參見DashScope API 參考。

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"
    }
}'