Model Studio で、API (HTTP) または CLI (シェル) を使用して Qwen モデルをチューニングします。教師ありファインチューニング (SFT)、継続的事前学習 (CPT)、Direct Preference Optimization (DPO) の 3 つのチューニングメソッドがサポートされています。
前提条件
- モデルファインチューニングの概要をご覧になり、その概念、プロセス、データ要件を理解してください。
- サービスを有効化し、API キーを取得します。API キーの取得をご参照ください。
- RAM ユーザー (RAM ユーザー) に必要な呼び出し、トレーニング、デプロイの権限を付与します。
注記API はトークンベースの課金のみをサポートしています。モデルトレーニングユニット (前払いまたは後払い) を使用するには、コンソールでジョブを作成してください。
チューニングファイルのアップロード
ファインチューニングファイルの準備
各トレーニングメソッド (SFT/DPO/CPT) のデータフォーマットの仕様、パッケージ化ルール、サイズと数量の制限、API アップロードクォータについては、チューニングデータのアップロードルールをご参照ください。各トレーニングメソッドがサポートするファイルフォーマットは次のとおりです:
トレーニングメソッド | ファイルフォーマット |
|---|---|
SFT テキスト生成 | jsonl (ChatML メッセージのマルチターン構造) |
SFT マルチモーダル (画像/動画) | zip (data.jsonl + 画像/動画ファイル) |
DPO | jsonl (選択/拒否のプリファレンスペア) |
CPT | jsonl (プレーンテキスト {text}) |
評価セット | xlsx |
Model Studio コンソールからデータテンプレートをダウンロードすることもできます。 | Create Dataset ページのファイルアップロードエリアの下に、データテンプレートのダウンロードリンクがあります。 |
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 呼び出しの両方で表示および使用できます。
モデルファインチューニング
ファインチューニングジョブの作成、クエリ、ログの取得、チェックポイントの管理、およびリスト表示/キャンセル/削除は、すべて API を通じて行われます。各インターフェースの完全なリクエスト/レスポンスパラメーターと例は対応する API ページに記載されています。このセクションでは、ワークフローの概要のみを説明します。
ファインチューニングジョブの作成
ファインチューニングジョブの作成 インターフェース (POST /api/v1/fine-tunes) を呼び出してトレーニングを開始します。リクエストボディ (model / training_datasets / hyper_parameters / training_type など) とレスポンスの例については、該当ページをご参照ください。
ファインチューニングジョブの詳細のクエリ
job_id を使用してタスクのステータスをクエリします。詳細については、ファインチューニングタスクのステータスクエリをご参照ください。
ファインチューニングジョブログの取得
ファインチューニングタスクログの取得 インターフェース (GET /api/v1/fine-tunes/{job_id}/logs) を通じてトレーニングログを取得します。
モデルチェックポイントのクエリと公開
ファインチューニングで生成されたチェックポイントは、リスト表示およびエクスポート (公開) できます:
- チェックポイントのリスト表示:チェックポイントのリスト表示 (
GET /api/v1/fine-tunes/{job_id}/checkpoints) をご参照ください。 - 特定のチェックポイントのエクスポート (公開):チェックポイントのエクスポート (
GET /api/v1/fine-tunes/{job_id}/export/{checkpoint}?model_name=...) をご参照ください。非同期処理のため、リストインターフェースでステータスをポーリングしてください。
その他のファインチューニング操作
- ジョブのリスト表示:ファインチューニングジョブのリスト表示
- ジョブのキャンセル:ファインチューニングジョブのキャンセル
- ジョブの削除:ファインチューニングジョブの削除
モデルデプロイと呼び出し
モデルデプロイ
モデルをデプロイするには、専用デプロイコンソールに移動します。
モデルの呼び出し
デプロイステータスが RUNNING になったら、他のモデルと同様にファインチューニングされたモデルを呼び出せます。
専用デプロイコンソールからモデルコードを取得することもできます。
使用方法とパラメーター: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"
}
}'