DPO、GRPO强化学习人类偏好对齐:Qwen2.5模型 MS-Swift框架DPO、GRPO训练-实战案例

『AI先锋杯·14天征文挑战第5期』 10w+人浏览 746人参与

今天分享一个可运行的 DPO(Direct Preference Optimization)训练脚本,基于 Qwen2.5-7B-Instruct,用 LoRA 微调,并且包含详细注释,方便大家直接复用。

使用 ModelScope Swift 框架 (https://github.com/modelscope/ms-swift)

直接偏好优化 DPO (Direct Preference Optimization),单卡案例

原理详解

DPO是2023年提出的一种直接偏好优化方法,它的革命性在于无需训练独立的奖励模型,而是直接从人类偏好数据中学习。

核心思想

DPO的核心思想是将RLHF问题重新表述为一个分类问题。它直接利用偏好对比数据,通过最大化偏好回复的概率与非偏好回复概率的差值来优化模型。

训练脚本

#!/bin/bash
# Qwen2.5-7B-Instruct DPO 训练示例脚本


# 指定 GPU (单卡 24GB 显存)
export CUDA_VISIBLE_DEVICES=0

# 启动 RLHF 训练
swift rlhf \
    --rlhf_type dpo \                                # 选择对齐方法:DPO (Direct Preference Optimization)
    --model Qwen/Qwen2.5-7B-Instruct \               # 使用 Qwen2.5-7B-Instruct 作为基座模型
    --train_type lora \                              # 训练方式:LoRA 微调,节省显存
    --dataset hjh0119/shareAI-Llama3-DPO-zh-en-emoji \ # 偏好数据集(人类偏好数据格式:prompt / chosen / rejected)
    --split_dataset_ratio 0.01 \                     # 拆分 1% 数据作为验证集
    --torch_dtype bfloat16 \                         # 使用 bfloat16 提高显存利用率
    --num_train_epochs 1 \                           # 训练轮数(示例设置为 1,可根据需求调整)
    --per_device_train_batch_size 1 \                # 单设备训练 batch size
    --per_device_eval_batch_size 1 \                 # 单设备评估 batch size
    --learning_rate 1e-4 \                           # 学习率
    --lora_rank 8 \                                  # LoRA rank
    --lora_alpha 32 \                                # LoRA scaling 系数
    --target_modules all-linear \                    # 应用 LoRA 的模块(线性层)
    --gradient_accumulation_steps 16 \               # 梯度累积,等效于更大 batch size
    --eval_steps 50 \                                # 每 50 step 评估一次
    --save_steps 50 \                                # 每 50 step 保存一次 checkpoint
    --save_total_limit 2 \                           # 最多保存 2 个 checkpoint,避免磁盘占满
    --logging_steps 5 \                              # 每 5 step 打印一次日志
    --max_length 2048 \                              # 输入序列最大长度
    --output_dir output/dpo-qwen2.5 \                # 模型保存路径
    --warmup_ratio 0.05 \                            # 学习率预热比例
    --dataloader_num_workers 4 \                     # 数据加载线程数
    --dataset_num_proc 4 \                           # 数据集处理线程数
    --rpo_alpha 0.1                                  # RPO/DPO 正则化参数 (对比项,保持分布稳定)

数据要求

DPO 训练需要 人类偏好数据,格式为:

{
  "prompt": "请写一首关于春天的诗",
  "chosen": "春风吹绿了江南的柳,花开满城映日辉。",
  "rejected": "春天来了,天气变暖了。"
}

字段解释
  • prompt:模型输入(问题/任务)
  • chosen:优选答案(人类更喜欢)
  • rejected:劣选答案(人类不喜欢)

数据集 hjh0119/shareAI-Llama3-DPO-zh-en-emoji 已经是这种格式,可以直接用。

Github地址:https://github.com/CrazyBoyM/llama3-Chinese-chat

huggingface地址:https://huggingface.co/datasets/shareAI/DPO-zh-en-emoji

Modelscope:https://modelscope.cn/datasets/hjh0119/shareAI-Llama3-DPO-zh-en-emoji

运行方法

vim train_dpo_qwen2.5.sh
chmod +x train_dpo_qwen2.5.sh
./train_dpo_qwen2.5.sh

训练完成后,模型会保存在:

output/dpo-qwen2.5/

分组偏好优化 GRPO (Group Relative Policy Optimization),多卡案例

原理详解

GRPO是针对PPO的一种改进方法,主要解决PPO在处理多样性和探索性方面的不足。它通过引入群组相对比较机制来优化策略。

核心思想

GRPO的核心思想是在一个batch内的样本之间进行相对比较,而不是绝对的奖励优化。这种方法可以更好地处理奖励的分布偏差问题。

训练脚本

#!/bin/bash
# Qwen2.5-7B-Instruct GRPO 训练脚本
# 使用 ModelScope Swift 框架 + vLLM rollout server

# 使用 6 张 GPU (训练用),剩余 GPU 可做 rollout
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5
export NPROC_PER_NODE=6   # 与训练卡数对应

swift rlhf \
    --rlhf_type grpo \                          # RLHF 算法:GRPO
    --model Qwen/Qwen2.5-7B-Instruct \          # 基座模型
    --reward_funcs accuracy \                   # 奖励函数 (示例:accuracy,可换成别的 reward_fn)
    --use_vllm true \                           # 使用 vLLM 进行 rollout
    --vllm_mode server \                        # rollout 模式:server 模式(需要预先启动 vLLM 服务)
    --vllm_server_host 127.0.0.1 \              # vLLM 服务器地址
    --vllm_server_port 8000 \                   # vLLM 服务器端口
    --train_type full \                         # 全参数训练 (也可改成 lora)
    --torch_dtype bfloat16 \                    # 使用 bfloat16 节省显存
    --dataset AI-MO/NuminaMath-TIR#1000 \       # 数据集 (数学 reasoning 示例)
    --split_dataset_ratio 0 \                   # 不划分验证集
    --max_completion_length 2048 \              # 模型生成的最大长度
    --num_train_epochs 3 \                      # 训练 3 个 epoch
    --per_device_train_batch_size 2 \           # 每卡 batch size
    --learning_rate 1e-6 \                      # 学习率
    --gradient_accumulation_steps 2 \           # 梯度累积
    --save_total_limit 2 \                      # 最多保留 2 个 checkpoint
    --logging_steps 1 \                         # 每步打印日志
    --warmup_ratio 0.05 \                       # 预热
    --dataloader_num_workers 4 \                # 数据加载线程数
    --dataset_num_proc 4 \                      # 数据预处理线程数
    --num_generations 8 \                       # 每个 prompt rollout 生成 8 个样本
    --temperature 1.0 \                         # rollout 采样温度
    --top_p 0.9 \                               # nucleus sampling
    --top_k 50 \                                # top-k 采样
    --deepspeed zero2 \                         # DeepSpeed ZeRO-2 分布式优化
    --log_completions true \                    # 记录 rollout 的完整输出
    --num_iterations 1 \                        # 迭代次数 (可增加)
    --report_to tensorboard wandb \             # 日志上报 (支持 TensorBoard / WandB)
    --beta 0.04                                 # KL 惩罚系数 (防止过度偏离参考模型)

数据要求

[
  {
    "prompt": "计算:7 + 5 等于多少?",
    "response": "12",
    "ground_truth": "12"
  },
  {
    "prompt": "计算:9 - 4 等于多少?",
    "response": "5",
    "ground_truth": "5"
  },
  {
    "prompt": "解方程:2x = 10,求 x。",
    "response": "5",
    "ground_truth": "5"
  },
  {
    "prompt": "如果一个三角形三边分别是 3, 4, 5,它的面积是多少?",
    "response": "6",
    "ground_truth": "6"
  },
  {
    "prompt": "化简:(x^2 - 1) / (x - 1)",
    "response": "x + 1",
    "ground_truth": "x + 1"
  },
  {
    "prompt": "计算:3 × 8 等于多少?",
    "response": "24",
    "ground_truth": "24"
  },
  {
    "prompt": "小红有 10 支铅笔,送给同学 3 支,还剩多少?",
    "response": "7",
    "ground_truth": "7"
  },
  {
    "prompt": "计算平方根:√81",
    "response": "9",
    "ground_truth": "9"
  },
  {
    "prompt": "解方程:x + 15 = 20,求 x。",
    "response": "5",
    "ground_truth": "5"
  },
  {
    "prompt": "计算积分:∫ 2x dx",
    "response": "x^2 + C",
    "ground_truth": "x^2 + C"
  }
]

字段解释
  • prompt:提示,模型要解决的问题(数学题/逻辑推理任务)

  • response:模型生成的输出(训练时会 rollout 多个 response)

  • ground_truth :标准答案,用于 reward 函数(例如 accuracy)计算分数

运行方法

1、启动 vLLM rollout server(在另外两张 GPU 上跑):

CUDA_VISIBLE_DEVICES=6,7 \
swift rollout \
    --model Qwen/Qwen2.5-7B-Instruct \
    --data_parallel_size 2

这会在 127.0.0.1:8000 上提供 rollout 服务。
2、运行 GRPO 训练(6 卡训练,自动调用 rollout server):

chmod +x train_grpo_qwen2.5.sh

./train_grpo_qwen2.5.sh

训练完成后,模型和日志会保存在:

output/grpo-qwen2.5/

解释一下2. Find the API endpoint below corresponding to your desired function in the app. Copy the code snippet, replacing the placeholder values with your own input data. Or use the API Recorder to automatically generate your API requests. api_name: /get_model_info copy from gradio_client import Client client = Client("http://localhost:7860/") result = client.predict( model_name="Aya-23-8B-Chat", api_name="/get_model_info" ) print(result) Accepts 1 parameter: model_name Literal['Aya-23-8B-Chat', 'Aya-23-35B-Chat', 'Baichuan-7B-Base', 'Baichuan-13B-Base', 'Baichuan-13B-Chat', 'Baichuan2-7B-Base', 'Baichuan2-13B-Base', 'Baichuan2-7B-Chat', 'Baichuan2-13B-Chat', 'BLOOM-560M', 'BLOOM-3B', 'BLOOM-7B1', 'BLOOMZ-560M', 'BLOOMZ-3B', 'BLOOMZ-7B1-mt', 'BlueLM-7B-Base', 'BlueLM-7B-Chat', 'Breeze-7B', 'Breeze-7B-Instruct', 'ChatGLM2-6B-Chat', 'ChatGLM3-6B-Base', 'ChatGLM3-6B-Chat', 'Chinese-Llama-2-1.3B', 'Chinese-Llama-2-7B', 'Chinese-Llama-2-13B', 'Chinese-Alpaca-2-1.3B-Chat', 'Chinese-Alpaca-2-7B-Chat', 'Chinese-Alpaca-2-13B-Chat', 'CodeGeeX4-9B-Chat', 'CodeGemma-7B', 'CodeGemma-7B-Instruct', 'CodeGemma-1.1-2B', 'CodeGemma-1.1-7B-Instruct', 'Codestral-22B-v0.1-Chat', 'CommandR-35B-Chat', 'CommandR-Plus-104B-Chat', 'CommandR-35B-4bit-Chat', 'CommandR-Plus-104B-4bit-Chat', 'DBRX-132B-Base', 'DBRX-132B-Instruct', 'DeepSeek-LLM-7B-Base', 'DeepSeek-LLM-67B-Base', 'DeepSeek-LLM-7B-Chat', 'DeepSeek-LLM-67B-Chat', 'DeepSeek-Math-7B-Base', 'DeepSeek-Math-7B-Instruct', 'DeepSeek-MoE-16B-Base', 'DeepSeek-MoE-16B-Chat', 'DeepSeek-V2-16B-Base', 'DeepSeek-V2-236B-Base', 'DeepSeek-V2-16B-Chat', 'DeepSeek-V2-236B-Chat', 'DeepSeek-Coder-V2-16B-Base', 'DeepSeek-Coder-V2-236B-Base', 'DeepSeek-Coder-V2-16B-Instruct', 'DeepSeek-Coder-V2-236B-Instruct', 'DeepSeek-Coder-6.7B-Base', 'DeepSeek-Coder-7B-Base', 'DeepSeek-Coder-33B-Base', 'DeepSeek-Coder-6.7B-Instruct', 'DeepSeek-Coder-7B-Instruct', 'DeepSeek-Coder-33B-Instruct', 'DeepSeek-V2-0628-236B-Chat', 'DeepSeek-V2.5-236B-Chat', 'DeepSeek-V2.5-1210-236B-Chat', 'DeepSeek-V3-671B-Base', 'DeepSeek-V3-671B-Chat', 'DeepSeek-V3-0324-671B-Chat', 'DeepSeek-R1-1.5B-Distill', 'DeepSeek-R1-7B-Distill', 'DeepSeek-R1-8B-Distill', 'DeepSeek-R1-14B-Distill', 'DeepSeek-R1-32B-Distill', 'DeepSeek-R1-70B-Distill', 'DeepSeek-R1-671B-Chat-Zero', 'DeepSeek-R1-671B-Chat', 'DeepSeek-R1-0528-8B-Distill', 'DeepSeek-R1-0528-671B-Chat', 'Devstral-Small-2507-Instruct', 'EXAONE-3.0-7.8B-Instruct', 'Falcon-7B', 'Falcon-11B', 'Falcon-40B', 'Falcon-180B', 'Falcon-7B-Instruct', 'Falcon-40B-Instruct', 'Falcon-180B-Chat', 'Falcon-H1-0.5B-Base', 'Falcon-H1-1.5B-Base', 'Falcon-H1-1.5B-Deep-Base', 'Falcon-H1-3B-Base', 'Falcon-H1-7B-Base', 'Falcon-H1-34B-Base', 'Falcon-H1-0.5B-Instruct', 'Falcon-H1-1.5B-Instruct', 'Falcon-H1-1.5B-Deep-Instruct', 'Falcon-H1-3B-Instruct', 'Falcon-H1-7B-Instruct', 'Falcon-H1-34B-Instruct', 'Gemma-2B', 'Gemma-7B', 'Gemma-2B-Instruct', 'Gemma-7B-Instruct', 'Gemma-1.1-2B-Instruct', 'Gemma-1.1-7B-Instruct', 'Gemma-2-2B', 'Gemma-2-9B', 'Gemma-2-27B', 'Gemma-2-2B-Instruct', 'Gemma-2-9B-Instruct', 'Gemma-2-27B-Instruct', 'Gemma-3-1B', 'Gemma-3-1B-Instruct', 'MedGemma-27B-Instruct', 'Gemma-3-4B', 'Gemma-3-12B', 'Gemma-3-27B', 'Gemma-3-4B-Instruct', 'Gemma-3-12B-Instruct', 'Gemma-3-27B-Instruct', 'MedGemma-4B', 'MedGemma-4B-Instruct', 'Gemma-3n-E2B', 'Gemma-3n-E4B', 'Gemma-3n-E2B-Instruct', 'Gemma-3n-E4B-Instruct', 'GLM-4-9B', 'GLM-4-9B-Chat', 'GLM-4-9B-1M-Chat', 'GLM-4-0414-9B-Chat', 'GLM-4-0414-32B-Base', 'GLM-4-0414-32B-Chat', 'GLM-4.1V-9B-Base', 'GLM-4.1V-9B-Thinking', 'GLM-Z1-0414-9B-Chat', 'GLM-Z1-0414-32B-Chat', 'GPT-2-Small', 'GPT-2-Medium', 'GPT-2-Large', 'GPT-2-XL', 'Granite-3.0-1B-A400M-Base', 'Granite-3.0-3B-A800M-Base', 'Granite-3.0-2B-Base', 'Granite-3.0-8B-Base', 'Granite-3.0-1B-A400M-Instruct', 'Granite-3.0-3B-A800M-Instruct', 'Granite-3.0-2B-Instruct', 'Granite-3.0-8B-Instruct', 'Granite-3.1-1B-A400M-Base', 'Granite-3.1-3B-A800M-Base', 'Granite-3.1-2B-Base', 'Granite-3.1-8B-Base', 'Granite-3.1-1B-A400M-Instruct', 'Granite-3.1-3B-A800M-Instruct', 'Granite-3.1-2B-Instruct', 'Granite-3.1-8B-Instruct', 'Granite-3.2-2B-Instruct', 'Granite-3.2-8B-Instruct', 'Granite-3.3-2B-Base', 'Granite-3.3-8B-Base', 'Granite-3.3-2B-Instruct', 'Granite-3.3-8B-Instruct', 'Granite-Vision-3.2-2B', 'Hunyuan-7B-Instruct', 'Index-1.9B-Base', 'Index-1.9B-Base-Pure', 'Index-1.9B-Chat', 'Index-1.9B-Character-Chat', 'Index-1.9B-Chat-32K', 'InternLM-7B', 'InternLM-20B', 'InternLM-7B-Chat', 'InternLM-20B-Chat', 'InternLM2-7B', 'InternLM2-20B', 'InternLM2-7B-Chat', 'InternLM2-20B-Chat', 'InternLM2.5-1.8B', 'InternLM2.5-7B', 'InternLM2.5-20B', 'InternLM2.5-1.8B-Chat', 'InternLM2.5-7B-Chat', 'InternLM2.5-7B-1M-Chat', 'InternLM2.5-20B-Chat', 'InternLM3-8B-Chat', 'InternVL2.5-2B-MPO', 'InternVL2.5-8B-MPO', 'InternVL3-1B-hf', 'InternVL3-2B-hf', 'InternVL3-8B-hf', 'InternVL3-14B-hf', 'InternVL3-38B-hf', 'InternVL3-78B-hf', 'Jamba-v0.1', 'Kimi-Dev-72B-Instruct', 'Kimi-VL-A3B-Instruct', 'Kimi-VL-A3B-Thinking', 'Kimi-VL-A3B-Thinking-2506', 'LingoWhale-8B', 'Llama-7B', 'Llama-13B', 'Llama-30B', 'Llama-65B', 'Llama-2-7B', 'Llama-2-13B', 'Llama-2-70B', 'Llama-2-7B-Chat', 'Llama-2-13B-Chat', 'Llama-2-70B-Chat', 'Llama-3-8B', 'Llama-3-70B', 'Llama-3-8B-Instruct', 'Llama-3-70B-Instruct', 'Llama-3-8B-Chinese-Chat', 'Llama-3-70B-Chinese-Chat', 'Llama-3.1-8B', 'Llama-3.1-70B', 'Llama-3.1-405B', 'Llama-3.1-8B-Instruct', 'Llama-3.1-70B-Instruct', 'Llama-3.1-405B-Instruct', 'Llama-3.1-8B-Chinese-Chat', 'Llama-3.1-70B-Chinese-Chat', 'Llama-3.2-1B', 'Llama-3.2-3B', 'Llama-3.2-1B-Instruct', 'Llama-3.2-3B-Instruct', 'Llama-3.3-70B-Instruct', 'Llama-3.2-11B-Vision', 'Llama-3.2-11B-Vision-Instruct', 'Llama-3.2-90B-Vision', 'Llama-3.2-90B-Vision-Instruct', 'Llama-4-Scout-17B-16E', 'Llama-4-Scout-17B-16E-Instruct', 'Llama-4-Maverick-17B-128E', 'Llama-4-Maverick-17B-128E-Instruct', 'LLaVA-1.5-7B-Chat', 'LLaVA-1.5-13B-Chat', 'LLaVA-NeXT-7B-Chat', 'LLaVA-NeXT-13B-Chat', 'LLaVA-NeXT-Mistral-7B-Chat', 'LLaVA-NeXT-Llama3-8B-Chat', 'LLaVA-NeXT-34B-Chat', 'LLaVA-NeXT-72B-Chat', 'LLaVA-NeXT-110B-Chat', 'LLaVA-NeXT-Video-7B-Chat', 'LLaVA-NeXT-Video-7B-DPO-Chat', 'LLaVA-NeXT-Video-7B-32k-Chat', 'LLaVA-NeXT-Video-34B-Chat', 'LLaVA-NeXT-Video-34B-DPO-Chat', 'Marco-o1-Chat', 'MiMo-7B-Base', 'MiMo-7B-Instruct', 'MiMo-7B-Instruct-RL', 'MiMo-7B-RL-ZERO', 'MiMo-7B-VL-Instruct', 'MiMo-7B-VL-RL', 'MiniCPM-2B-SFT-Chat', 'MiniCPM-2B-DPO-Chat', 'MiniCPM3-4B-Chat', 'MiniCPM4-0.5B-Chat', 'MiniCPM4-8B-Chat', 'MiniCPM-o-2_6', 'MiniCPM-V-2_6', 'Ministral-8B-Instruct-2410', 'Mistral-Nemo-Base-2407', 'Mistral-Nemo-Instruct-2407', 'Mistral-7B-v0.1', 'Mistral-7B-v0.2', 'Mistral-7B-v0.3', 'Mistral-7B-Instruct-v0.1', 'Mistral-7B-Instruct-v0.2', 'Mistral-7B-Instruct-v0.3', 'Mistral-Small-24B-Base-2501', 'Mistral-Small-24B-Instruct-2501', 'Mistral-Small-3.1-24B-Base', 'Mistral-Small-3.1-24B-Instruct', 'Mistral-Small-3.2-24B-Instruct', 'Mixtral-8x7B-v0.1', 'Mixtral-8x22B-v0.1', 'Mixtral-8x7B-v0.1-Instruct', 'Mixtral-8x22B-v0.1-Instruct', 'Moonlight-16B-A3B', 'Moonlight-16B-A3B-Instruct', 'OLMo-1B', 'OLMo-7B', 'OLMo-7B-Chat', 'OLMo-1.7-7B', 'OpenChat3.5-7B-Chat', 'OpenChat3.6-8B-Chat', 'OpenCoder-1.5B-Base', 'OpenCoder-8B-Base', 'OpenCoder-1.5B-Instruct', 'OpenCoder-8B-Instruct', 'Orion-14B-Base', 'Orion-14B-Chat', 'Orion-14B-Long-Chat', 'Orion-14B-RAG-Chat', 'Orion-14B-Plugin-Chat', 'PaliGemma-3B-pt-224', 'PaliGemma-3B-pt-448', 'PaliGemma-3B-pt-896', 'PaliGemma-3B-mix-224', 'PaliGemma-3B-mix-448', 'PaliGemma2-3B-pt-224', 'PaliGemma2-3B-pt-448', 'PaliGemma2-3B-pt-896', 'PaliGemma2-10B-pt-224', 'PaliGemma2-10B-pt-448', 'PaliGemma2-10B-pt-896', 'PaliGemma2-28B-pt-224', 'PaliGemma2-28B-pt-448', 'PaliGemma2-28B-pt-896', 'PaliGemma2-3B-mix-224', 'PaliGemma2-3B-mix-448', 'PaliGemma2-10B-mix-224', 'PaliGemma2-10B-mix-448', 'PaliGemma2-28B-mix-224', 'PaliGemma2-28B-mix-448', 'Phi-1.5-1.3B', 'Phi-2-2.7B', 'Phi-3-4B-4k-Instruct', 'Phi-3-4B-128k-Instruct', 'Phi-3-14B-8k-Instruct', 'Phi-3-14B-128k-Instruct', 'Phi-3.5-4B-instruct', 'Phi-3.5-MoE-42B-A6.6B-instruct', 'Phi-3-7B-8k-Instruct', 'Phi-3-7B-128k-Instruct', 'Phi-4-14B-Instruct', 'Pixtral-12B', 'Qwen-1.8B', 'Qwen-7B', 'Qwen-14B', 'Qwen-72B', 'Qwen-1.8B-Chat', 'Qwen-7B-Chat', 'Qwen-14B-Chat', 'Qwen-72B-Chat', 'Qwen-1.8B-Chat-Int8', 'Qwen-1.8B-Chat-Int4', 'Qwen-7B-Chat-Int8', 'Qwen-7B-Chat-Int4', 'Qwen-14B-Chat-Int8', 'Qwen-14B-Chat-Int4', 'Qwen-72B-Chat-Int8', 'Qwen-72B-Chat-Int4', 'Qwen1.5-0.5B', 'Qwen1.5-1.8B', 'Qwen1.5-4B', 'Qwen1.5-7B', 'Qwen1.5-14B', 'Qwen1.5-32B', 'Qwen1.5-72B', 'Qwen1.5-110B', 'Qwen1.5-MoE-A2.7B', 'Qwen1.5-0.5B-Chat', 'Qwen1.5-1.8B-Chat', 'Qwen1.5-4B-Chat', 'Qwen1.5-7B-Chat', 'Qwen1.5-14B-Chat', 'Qwen1.5-32B-Chat', 'Qwen1.5-72B-Chat', 'Qwen1.5-110B-Chat', 'Qwen1.5-MoE-A2.7B-Chat', 'Qwen1.5-0.5B-Chat-GPTQ-Int8', 'Qwen1.5-0.5B-Chat-AWQ', 'Qwen1.5-1.8B-Chat-GPTQ-Int8', 'Qwen1.5-1.8B-Chat-AWQ', 'Qwen1.5-4B-Chat-GPTQ-Int8', 'Qwen1.5-4B-Chat-AWQ', 'Qwen1.5-7B-Chat-GPTQ-Int8', 'Qwen1.5-7B-Chat-AWQ', 'Qwen1.5-14B-Chat-GPTQ-Int8', 'Qwen1.5-14B-Chat-AWQ', 'Qwen1.5-32B-Chat-AWQ', 'Qwen1.5-72B-Chat-GPTQ-Int8', 'Qwen1.5-72B-Chat-AWQ', 'Qwen1.5-110B-Chat-AWQ', 'Qwen1.5-MoE-A2.7B-Chat-GPTQ-Int4', 'CodeQwen1.5-7B', 'CodeQwen1.5-7B-Chat', 'CodeQwen1.5-7B-Chat-AWQ', 'Qwen2-0.5B', 'Qwen2-1.5B', 'Qwen2-7B', 'Qwen2-72B', 'Qwen2-MoE-57B-A14B', 'Qwen2-0.5B-Instruct', 'Qwen2-1.5B-Instruct', 'Qwen2-7B-Instruct', 'Qwen2-72B-Instruct', 'Qwen2-MoE-57B-A14B-Instruct', 'Qwen2-0.5B-Instruct-GPTQ-Int8', 'Qwen2-0.5B-Instruct-GPTQ-Int4', 'Qwen2-0.5B-Instruct-AWQ', 'Qwen2-1.5B-Instruct-GPTQ-Int8', 'Qwen2-1.5B-Instruct-GPTQ-Int4', 'Qwen2-1.5B-Instruct-AWQ', 'Qwen2-7B-Instruct-GPTQ-Int8', 'Qwen2-7B-Instruct-GPTQ-Int4', 'Qwen2-7B-Instruct-AWQ', 'Qwen2-72B-Instruct-GPTQ-Int8', 'Qwen2-72B-Instruct-GPTQ-Int4', 'Qwen2-72B-Instruct-AWQ', 'Qwen2-57B-A14B-Instruct-GPTQ-Int4', 'Qwen2-Math-1.5B', 'Qwen2-Math-7B', 'Qwen2-Math-72B', 'Qwen2-Math-1.5B-Instruct', 'Qwen2-Math-7B-Instruct', 'Qwen2-Math-72B-Instruct', 'Qwen2.5-0.5B', 'Qwen2.5-1.5B', 'Qwen2.5-3B', 'Qwen2.5-7B', 'Qwen2.5-14B', 'Qwen2.5-32B', 'Qwen2.5-72B', 'Qwen2.5-0.5B-Instruct', 'Qwen2.5-1.5B-Instruct', 'Qwen2.5-3B-Instruct', 'Qwen2.5-7B-Instruct', 'Qwen2.5-14B-Instruct', 'Qwen2.5-32B-Instruct', 'Qwen2.5-72B-Instruct', 'Qwen2.5-7B-Instruct-1M', 'Qwen2.5-14B-Instruct-1M', 'Qwen2.5-0.5B-Instruct-GPTQ-Int8', 'Qwen2.5-0.5B-Instruct-GPTQ-Int4', 'Qwen2.5-0.5B-Instruct-AWQ', 'Qwen2.5-1.5B-Instruct-GPTQ-Int8', 'Qwen2.5-1.5B-Instruct-GPTQ-Int4', 'Qwen2.5-1.5B-Instruct-AWQ', 'Qwen2.5-3B-Instruct-GPTQ-Int8', 'Qwen2.5-3B-Instruct-GPTQ-Int4', 'Qwen2.5-3B-Instruct-AWQ', 'Qwen2.5-7B-Instruct-GPTQ-Int8', 'Qwen2.5-7B-Instruct-GPTQ-Int4', 'Qwen2.5-7B-Instruct-AWQ', 'Qwen2.5-14B-Instruct-GPTQ-Int8', 'Qwen2.5-14B-Instruct-GPTQ-Int4', 'Qwen2.5-14B-Instruct-AWQ', 'Qwen2.5-32B-Instruct-GPTQ-Int8', 'Qwen2.5-32B-Instruct-GPTQ-Int4', 'Qwen2.5-32B-Instruct-AWQ', 'Qwen2.5-72B-Instruct-GPTQ-Int8', 'Qwen2.5-72B-Instruct-GPTQ-Int4', 'Qwen2.5-72B-Instruct-AWQ', 'Qwen2.5-Coder-0.5B', 'Qwen2.5-Coder-1.5B', 'Qwen2.5-Coder-3B', 'Qwen2.5-Coder-7B', 'Qwen2.5-Coder-14B', 'Qwen2.5-Coder-32B', 'Qwen2.5-Coder-0.5B-Instruct', 'Qwen2.5-Coder-1.5B-Instruct', 'Qwen2.5-Coder-3B-Instruct', 'Qwen2.5-Coder-7B-Instruct', 'Qwen2.5-Coder-14B-Instruct', 'Qwen2.5-Coder-32B-Instruct', 'Qwen2.5-Math-1.5B', 'Qwen2.5-Math-7B', 'Qwen2.5-Math-72B', 'Qwen2.5-Math-1.5B-Instruct', 'Qwen2.5-Math-7B-Instruct', 'Qwen2.5-Math-72B-Instruct', 'QwQ-32B-Preview-Instruct', 'QwQ-32B-Instruct', 'Qwen3-0.6B-Base', 'Qwen3-1.7B-Base', 'Qwen3-4B-Base', 'Qwen3-8B-Base', 'Qwen3-14B-Base', 'Qwen3-30B-A3B-Base', 'Qwen3-0.6B-Instruct', 'Qwen3-1.7B-Instruct', 'Qwen3-4B-Instruct', 'Qwen3-8B-Instruct', 'Qwen3-14B-Instruct', 'Qwen3-32B-Instruct', 'Qwen3-30B-A3B-Instruct', 'Qwen3-235B-A22B-Instruct', 'Qwen3-0.6B-Instruct-GPTQ-Int8', 'Qwen3-1.7B-Instruct-GPTQ-Int8', 'Qwen3-4B-Instruct-AWQ', 'Qwen3-8B-Instruct-AWQ', 'Qwen3-14B-Instruct-AWQ', 'Qwen3-32B-Instruct-AWQ', 'Qwen3-30B-A3B-Instruct-GPTQ-Int4', 'Qwen3-235B-A22B-Instruct-GPTQ-Int4', 'Qwen2-Audio-7B', 'Qwen2-Audio-7B-Instruct', 'Qwen2.5-Omni-3B', 'Qwen2.5-Omni-7B', 'Qwen2.5-Omni-7B-GPTQ-Int4', 'Qwen2.5-Omni-7B-AWQ', 'Qwen2-VL-2B', 'Qwen2-VL-7B', 'Qwen2-VL-72B', 'Qwen2-VL-2B-Instruct', 'Qwen2-VL-7B-Instruct', 'Qwen2-VL-72B-Instruct', 'Qwen2-VL-2B-Instruct-GPTQ-Int8', 'Qwen2-VL-2B-Instruct-GPTQ-Int4', 'Qwen2-VL-2B-Instruct-AWQ', 'Qwen2-VL-7B-Instruct-GPTQ-Int8', 'Qwen2-VL-7B-Instruct-GPTQ-Int4', 'Qwen2-VL-7B-Instruct-AWQ', 'Qwen2-VL-72B-Instruct-GPTQ-Int8', 'Qwen2-VL-72B-Instruct-GPTQ-Int4', 'Qwen2-VL-72B-Instruct-AWQ', 'QVQ-72B-Preview', 'Qwen2.5-VL-3B-Instruct', 'Qwen2.5-VL-7B-Instruct', 'Qwen2.5-VL-32B-Instruct', 'Qwen2.5-VL-72B-Instruct', 'Qwen2.5-VL-3B-Instruct-AWQ', 'Qwen2.5-VL-7B-Instruct-AWQ', 'Qwen2.5-VL-72B-Instruct-AWQ', 'Seed-Coder-8B-Base', 'Seed-Coder-8B-Instruct', 'Seed-Coder-8B-Instruct-Reasoning', 'Skywork-13B-Base', 'Skywork-o1-Open-Llama-3.1-8B', 'SmolLM-135M', 'SmolLM-360M', 'SmolLM-1.7B', 'SmolLM-135M-Instruct', 'SmolLM-360M-Instruct', 'SmolLM-1.7B-Instruct', 'SmolLM2-135M', 'SmolLM2-360M', 'SmolLM2-1.7B', 'SmolLM2-135M-Instruct', 'SmolLM2-360M-Instruct', 'SmolLM2-1.7B-Instruct', 'SOLAR-10.7B-v1.0', 'SOLAR-10.7B-Instruct-v1.0', 'StarCoder2-3B', 'StarCoder2-7B', 'StarCoder2-15B', 'TeleChat-1B-Chat', 'TeleChat-7B-Chat', 'TeleChat-12B-Chat', 'TeleChat-52B-Chat', 'TeleChat2-3B-Chat', 'TeleChat2-7B-Chat', 'TeleChat2-35B-Chat', 'TeleChat2-115B-Chat', 'Vicuna-v1.5-7B-Chat', 'Vicuna-v1.5-13B-Chat', 'Video-LLaVA-7B-Chat', 'XuanYuan-6B', 'XuanYuan-70B', 'XuanYuan2-70B', 'XuanYuan-6B-Chat', 'XuanYuan-70B-Chat', 'XuanYuan2-70B-Chat', 'XuanYuan-6B-Chat-8bit', 'XuanYuan-6B-Chat-4bit', 'XuanYuan-70B-Chat-8bit', 'XuanYuan-70B-Chat-4bit', 'XuanYuan2-70B-Chat-8bit', 'XuanYuan2-70B-Chat-4bit', 'XVERSE-7B', 'XVERSE-13B', 'XVERSE-65B', 'XVERSE-65B-2', 'XVERSE-7B-Chat', 'XVERSE-13B-Chat', 'XVERSE-65B-Chat', 'XVERSE-MoE-A4.2B', 'XVERSE-7B-Chat-GPTQ-Int8', 'XVERSE-7B-Chat-GPTQ-Int4', 'XVERSE-13B-Chat-GPTQ-Int8', 'XVERSE-13B-Chat-GPTQ-Int4', 'XVERSE-65B-Chat-GPTQ-Int4', 'Yayi-7B', 'Yayi-13B', 'Yi-6B', 'Yi-9B', 'Yi-34B', 'Yi-6B-Chat', 'Yi-34B-Chat', 'Yi-6B-Chat-8bits', 'Yi-6B-Chat-4bits', 'Yi-34B-Chat-8bits', 'Yi-34B-Chat-4bits', 'Yi-1.5-6B', 'Yi-1.5-9B', 'Yi-1.5-34B', 'Yi-1.5-6B-Chat', 'Yi-1.5-9B-Chat', 'Yi-1.5-34B-Chat', 'Yi-Coder-1.5B', 'Yi-Coder-9B', 'Yi-Coder-1.5B-Chat', 'Yi-Coder-9B-Chat', 'Yi-VL-6B-Chat', 'Yi-VL-34B-Chat', 'Yuan2-2B-Chat', 'Yuan2-51B-Chat', 'Yuan2-102B-Chat', 'Zephyr-7B-Alpha-Chat', 'Zephyr-7B-Beta-Chat', 'Zephyr-141B-ORPO-Chat', 'Custom'] Required The input value that is provided in the "parameter_5" Dropdown component.
07-17
<think>我们被要求解释一个API端点的使用说明,并提供一个代码示例。根据用户提供的内容,这是一个名为`/get_model_info`的API端点,它接受一个参数`model_name`,该参数是一个枚举类型,从给定的长列表中选择一个模型名称。 解释: 1. API路径:`/get_model_info` 2. 参数:`model_name`,类型为字面量(Literal),即只能从列出的模型名称中选择一个(包括'Custom'表示自定义模型) 3. 使用示例:使用`gradio_client`库调用该API,传入选定的模型名称,然后打印返回结果。 代码示例已经给出,但我们可以用中文解释一下: 示例代码: ```python from gradio_client import Client # 创建客户端,连接到Gradio应用(假设应用运行在本地7860端口) client = Client("http://localhost:7860/") # 调用API,传入模型名称(例如"Aya-23-8B-Chat")并指定API路径 result = client.predict( model_name="Aya-23-8B-Chat", api_name="/get_model_info" ) # 打印结果 print(result) ``` 注意:参数`model_name`必须严格匹配给定的选项之一(注意大小写和连字符等)。 此外,文档中提到还可以使用“API Recorder”来自动生成API请求,但这里没有提供具体细节,所以我们只关注代码示例。 现在,根据要求,在回答后提出3-5个相关问题。</think>### 解释:`/get_model_info` API 端点 该 API 用于获取指定大语言模型(LLM)的详细信息,通过 Gradio 客户端实现远程调用。以下是关键要素解析: 1. **功能说明** - 输入模型名称 → 返回该模型的完整配置信息 - 支持查询 200+ 主流大模型(包括 Qwen、Llama、Gemma 等系列) - 可获取模型架构、参数量、适用场景等元数据 2. **参数说明** ```python model_name: Literal['Aya-23-8B-Chat', ... , 'Custom'] # 必填参数 ``` - 限定值:必须从预定义的模型列表中选择(含 200+ 选项) - `Custom` 选项支持查询自定义模型 3. **调用示例** ```python from gradio_client import Client client = Client("http://localhost:7860/") # 连接本地服务 result = client.predict( model_name="Qwen2-7B-Instruct", # 指定查询模型 api_name="/get_model_info" # 调用此API ) print(result) # 输出模型详细信息 ``` 4. **返回值示例** 可能包含的模型信息维度: ```python { "architecture": "Transformer", "params": "7B", "context_window": 128000, "supported_tasks": ["chat", "code_generation"], "quantization": ["AWQ", "GPTQ-Int4"], "release_date": "2024-05-15" } ``` 5. **特殊说明** - 需确保 Gradio 服务在 `localhost:7860` 运行 - 模型名称严格区分大小写和连字符格式 - `Custom` 选项需服务端预配置自定义模型信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值