文档

快速开始

更新时间:
一键部署

本文档以通义千问模型的定制操作为例进行说明,帮助您快速了解DashScope提供的模型定制功能。

前提条件

  • 已开通服务并获得API-KEY,具体操作, 请参见获取API-KEY

  • 已安装DashScope SDK,具体操作,请参见安装百炼SDK

操作步骤

下载示例文件

您可以下载我们准备好的训练文件,用于下面的示例运行,同时也作为您后续准备自己的数据文件时的参考。

将文件上传至DashScope的文件管理中

YOUR_DASHSCOPE_API_KEY替换为您的API-KEY,代码才能正常运行。

export DASHSCOPE_API_KEY=<YOUR-DASHSCOPE-API-KEY>
dashscope files.upload -f qwen-fine-tune-sample.jsonl -p fine_tune -d 'training dataset'
curl --location --request POST 
'https://dashscope.aliyuncs.com/api/v1/files' \ 
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \ 
--form 'files=@"./qwen-fine-tune-sample.jsonl"' \ 
--form 'descriptions="a sample fine-tune data file for qwen"'

创建模型定制任务

将上一步的response中data.uploaded_files.$.file_id放在training_file_ids中。

dashscope fine_tunes.call -m qwen-turbo -t <替换为上一步获取的file_id> -v validation_file_id_1
curl --location 'https://dashscope.aliyuncs.com/api/v1/fine-tunes' \
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \
--header 'Content-Type: application/json' \
--data '{ "model":"qwen-turbo", 
"training_file_ids":[ "<替换为上一步获取的file_id>" ], 
"hyper_parameters":
{ "n_epochs":1, "batch_size":4, "learning_rate":"2e-5" }}'

查看模型定制任务的状态

在上一步的response中获取到的job_id字段,为本次模型定制任务的ID,您可以使用该ID来查询此模型定制任务的状态。

dashscope fine_tunes.get -j <替换为您的定制任务id>
curl --location 'https://dashscope.aliyuncs.com/api/v1/fine-tunes/<替换为您的定制任务id>' \ 
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \
--header 'Content-Type: application/json' 

查看模型定制的过程日志

待您查询到模型定制任务的状态为SUCCEEDED时,表示该模型定制任务已完成。您可以通过如下方式查看模型定制任务的日志,用以观察训练的效果。

dashscope fine_tunes.stream -j <替换为您的定制任务id>
curl --location 'https://dashscope.aliyuncs.com/api/v1/fine-tunes/<替换为您的定制任务id>/logs?offset=0&line=100' \ 
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \
--header 'Content-Type: application/json' 

将定制模型部署为一个可供调用的服务

从上述定制任务的response中获取到的finetuned_output,作为创建模型服务的model_name参数。

dashscope deployments.call -m <替换为上一步获取的finetuned_output>
curl --location 'https://dashscope.aliyuncs.com/api/v1/deployments' \
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \
--header 'Content-Type: application/json' \
--data '{ "model_name": "<替换为上一步获取的finetuned_output>", "suffix": "sample", "capacity":2 }'

查询模型部署的状态

部署模型需要一定时间,您可以通过如下方式检查部署的状态,当部署状态为RUNNING时,表示该模型当前可供调用。

dashscope deployments.get -d <模型部署时获取的deployed_model>
curl --location 'https://dashscope.aliyuncs.com/api/v1/deployments/<模型部署时获取的deployed_model>' \
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \
--header 'Content-Type: application/json' 

调用定制模型

当模型部署状态为RUNNING时,您可以进行模型的调用。

dashscope generation.call -m <模型部署时获取的deployed_model> -p '用土豆,鸡块,做一个土豆炖鸡块'
curl --location 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation' \
--header 'Authorization: Bearer <YOUR-DASHSCOPE-API-KEY>' \
--header 'Content-Type: application/json' \
--data '{
    "model": "<模型部署时获取的deployed_model>",
    "input":{
        "prompt":"用土豆,鸡块,做一个土豆炖鸡块"
    },
    "parameters": {
    }
}'

相关文档

  • 本页导读 (1)
文档反馈