[Beta] 微调API
这是一个仅限企业使用的端点 在这里开始使用企业版
支持的提供商
- Azure OpenAI
- OpenAI
- Vertex AI
在您的 litellm config.yaml 中添加 finetune_settings 和 files_settings 以使用微调端点。
finetune_settings 和 files_settings 的示例 config.yaml
model_list:
- model_name: gpt-4
litellm_params:
model: openai/fake
api_key: fake-key
api_base: https://exampleopenaiendpoint-production.up.railway.app/
# 用于 /fine_tuning/jobs 端点
finetune_settings:
- custom_llm_provider: azure
api_base: https://exampleopenaiendpoint-production.up.railway.app
api_key: os.environ/AZURE_API_KEY
api_version: "2023-03-15-preview"
- custom_llm_provider: openai
api_key: os.environ/OPENAI_API_KEY
- custom_llm_provider: "vertex_ai"
vertex_project: "adroit-crow-413218"
vertex_location: "us-central1"
vertex_credentials: "/Users/ishaanjaffer/Downloads/adroit-crow-413218-a956eef1a2a8.json"
# 用于 /files 端点
files_settings:
- custom_llm_provider: azure
api_base: https://exampleopenaiendpoint-production.up.railway.app
api_key: fake-key
api_version: "2023-03-15-preview"
- custom_llm_provider: openai
api_key: os.environ/OPENAI_API_KEY
创建用于微调的文件
client = AsyncOpenAI(api_key="sk-1234", base_url="http://0.0.0.0:4000") # base_url 是您的 litellm 代理 URL
file_name = "openai_batch_completions.jsonl"
response = await client.files.create(
extra_body={"custom_llm_provider": "azure"}, # 告诉 litellm 代理使用哪个提供商
file=open(file_name, "rb"),
purpose="fine-tune",
)
curl http://localhost:4000/v1/files \
-H "Authorization: Bearer sk-1234" \
-F purpose="batch" \
-F custom_llm_provider="azure"\
-F file="@mydata.jsonl"
创建微调任务
ft_job = await client.fine_tuning.jobs.create(
model="gpt-35-turbo-1106", # 您想要微调的 Azure OpenAI 模型
training_file="file-abc123", # 从创建文件响应中获取的 file_id
extra_body={"custom_llm_provider": "azure"}, # 告诉 litellm 代理使用哪个提供商
)
curl http://localhost:4000/v1/fine_tuning/jobs \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"custom_llm_provider": "azure",
"model": "gpt-35-turbo-1106",
"training_file": "file-abc123"
}'
ft_job = await client.fine_tuning.jobs.create(
model="gemini-1.0-pro-002", # 您想要微调的 Vertex 模型
training_file="gs://cloud-samples-data/ai-platform/generative_ai/sft_train_data.jsonl", # 从创建文件响应中获取的 file_id
extra_body={"custom_llm_provider": "vertex_ai"}, # 告诉 litellm 代理使用哪个提供商
)
curl http://localhost:4000/v1/fine_tuning/jobs \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"custom_llm_provider": "vertex_ai",
"model": "gemini-1.0-pro-002",
"training_file": "gs://cloud-samples-data/ai-platform/generative_ai/sft_train_data.jsonl"
}'
使用此方法在 Vertex AI API 格式 中创建微调任务
curl http://localhost:4000/v1/projects/tuningJobs \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"baseModel": "gemini-1.0-pro-002",
"supervisedTuningSpec" : {
"training_dataset_uri": "gs://cloud-samples-data/ai-platform/generative_ai/sft_train_data.jsonl"
}
}'
请求体
model类型: string
必需: 是
要微调的模型名称custom_llm_provider类型:
Literal["azure", "openai", "vertex_ai"]必需: 是 要微调的模型名称。您可以选择以下 支持的提供商 之一
training_file类型: string
必需: 是
包含训练数据的上传文件的 ID- 请参阅上传文件了解如何上传文件。
- 您的数据集必须格式化为JSONL文件。
hyperparameters类型: 对象
必需: 否
用于微调任务的超参数。支持的
hyperparametersbatch_size
类型: 字符串或整数
必需: 否
每个批次中的示例数量。较大的批次大小意味着模型参数更新频率较低,但方差较小。learning_rate_multiplier
类型: 字符串或数字
必需: 否
学习率的缩放因子。较小的学习率可能有助于避免过拟合。n_epochs
类型: 字符串或整数
必需: 否
模型训练的轮数。一轮指的是对训练数据集进行一次完整的循环。suffix类型: 字符串或null
必需: 否
默认: null
一个最多18个字符的字符串,将添加到您的微调模型名称中。 示例:suffix为 "custom-model-name" 将生成一个类似ft:gpt-4o-mini:openai:custom-model-name:7p4lURel的模型名称。validation_file类型: 字符串或null
必需: 否
包含验证数据的已上传文件的ID。- 如果提供,此数据将在微调期间定期用于生成验证指标。
integrations类型: 数组或null
必需: 否
为您的微调任务启用的集成列表。seed类型: 整数或null
必需: 否
种子控制任务的可重复性。传入相同的种子和任务参数应产生相同的结果,但在极少数情况下可能会有所不同。如果未指定种子,将为您生成一个。
{
"model": "gpt-4o-mini",
"training_file": "file-abcde12345",
"hyperparameters": {
"batch_size": 4,
"learning_rate_multiplier": 0.1,
"n_epochs": 3
},
"suffix": "custom-model-v1",
"validation_file": "file-fghij67890",
"seed": 42
}
取消微调任务
# 取消特定的微调任务
cancel_ft_job = await client.fine_tuning.jobs.cancel(
fine_tuning_job_id="123", # 微调任务ID
extra_body={"custom_llm_provider": "azure"}, # 告诉litellm代理使用哪个提供商
)
print("取消微调任务的响应={}".format(cancel_ft_job))
curl -X POST http://localhost:4000/v1/fine_tuning/jobs/ftjob-abc123/cancel \
-H "Authorization: Bearer sk-1234" \
-H "Content-Type: application/json" \
-d '{"custom_llm_provider": "azure"}'
列出微调任务
list_ft_jobs = await client.fine_tuning.jobs.list(
extra_query={"custom_llm_provider": "azure"} # 告诉litellm代理使用哪个提供商
)
print("微调任务列表={}".format(list_ft_jobs))
curl -X GET 'http://localhost:4000/v1/fine_tuning/jobs?custom_llm_provider=azure' \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234"