Thinkless 项目最佳实践教程
1. 项目介绍
Thinkless 是一个开源项目,旨在通过强化学习使大型语言模型(LLM)能够自适应地在简短回答和详细推理之间进行选择。该项目由 National University of Singapore 的 xML Lab 提出,核心是一个名为 Decoupled Group Relative Policy Optimization(DeGRPO)的算法。通过训练,Thinkless 可以在多个基准测试中显著提高推理语言模型的计算效率。
2. 项目快速启动
在开始之前,请确保您的系统中已安装了以下依赖:
- Python 3.10
- PyTorch
- lm_eval
- Ray
以下是快速启动项目的步骤:
# 创建并激活虚拟环境
conda create -n thinkless python==3.10
conda activate thinkless
# 克隆项目仓库
git clone https://github.com/VainF/Thinkless.git
cd Thinkless
# 安装依赖
pip install torch==2.4.0 lm_eval==0.4.8 ray==2.45.0
pip install -e ./verl
pip install -e .
# 加载模型和分词器
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Vinnnf/Thinkless-1.5B-RL-DeepScaleR"
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_name)
# 使用模型生成回答
instruction = "Please reason step by step, and put your final answer within \\boxed{}."
prompt = "The arithmetic mean of 7, 2, $x$ and 10 is 9. What is the value of $x$?"
messages = [{"role": "user", "content": f"{instruction}\n{prompt}"}]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(**model_inputs, max_new_tokens=16384, do_sample=True, temperature=0.6, top_p=0.95)
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(text + response)
3. 应用案例和最佳实践
应用案例
一个简单的应用案例是使用 Thinkless 来解答数学问题。例如,对于问题 "The arithmetic mean of 7, 2, $x$ and 10 is 9. What is the value of $x$?",Thinkless 会逐步推理并给出答案。
最佳实践
- 在训练模型之前,确保数据集的质量和多样性。
- 使用 DeGRPO 算法进行训练,以优化推理模式的选择。
- 根据任务复杂性调整模型的推理模式。
4. 典型生态项目
Thinkless 可以与其他开源项目结合,形成一个强大的推理生态系统。例如,可以与数据预处理和后处理工具集成,以提高整个推理流程的效率。此外,结合自动评估工具可以自动化模型的性能评估过程。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考