|
|
--- |
|
|
base_model: Qwen/Qwen2.5-0.5B-Instruct |
|
|
library_name: peft |
|
|
tags: |
|
|
- lora |
|
|
- qwen |
|
|
- customer-service |
|
|
- chinese |
|
|
- conversational |
|
|
license: mit |
|
|
language: |
|
|
- zh |
|
|
--- |
|
|
|
|
|
# Qwen2.5-0.5B-Instruct 客服微调模型 |
|
|
|
|
|
这是一个基于 Qwen2.5-0.5B-Instruct 使用 LoRA 方法微调的客服对话模型。 |
|
|
|
|
|
## 模型详情 |
|
|
|
|
|
- **基础模型**: [Qwen/Qwen2.5-0.5B-Instruct](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct) |
|
|
- **微调方法**: LoRA (Low-Rank Adaptation) |
|
|
- **参数量**: 基础模型 0.5B + LoRA 适配器 ~2MB |
|
|
- **语言**: 中文 |
|
|
- **用途**: 客服对话、智能问答 |
|
|
|
|
|
## 使用方法 |
|
|
|
|
|
### 安装依赖 |
|
|
|
|
|
```bash |
|
|
pip install transformers peft torch |
|
|
``` |
|
|
|
|
|
### 加载和使用模型 |
|
|
|
|
|
```python |
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
from peft import PeftModel |
|
|
|
|
|
# 加载基础模型 |
|
|
base_model = AutoModelForCausalLM.from_pretrained( |
|
|
"Qwen/Qwen2.5-0.5B-Instruct", |
|
|
torch_dtype="auto", |
|
|
device_map="auto" |
|
|
) |
|
|
|
|
|
# 加载 LoRA 适配器 |
|
|
model = PeftModel.from_pretrained( |
|
|
base_model, |
|
|
"pplboy/test" |
|
|
) |
|
|
|
|
|
# 加载分词器 |
|
|
tokenizer = AutoTokenizer.from_pretrained("pplboy/test") |
|
|
|
|
|
# 使用模型 |
|
|
prompt = "你好,我想咨询一下产品" |
|
|
inputs = tokenizer(prompt, return_tensors="pt").to(model.device) |
|
|
|
|
|
outputs = model.generate( |
|
|
**inputs, |
|
|
max_new_tokens=100, |
|
|
temperature=0.7, |
|
|
top_p=0.9, |
|
|
do_sample=True |
|
|
) |
|
|
|
|
|
response = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
print(response) |
|
|
``` |
|
|
|
|
|
### 使用 Transformers Pipeline |
|
|
|
|
|
```python |
|
|
from transformers import pipeline |
|
|
from peft import PeftModel, PeftConfig |
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
|
|
# 加载模型 |
|
|
config = PeftConfig.from_pretrained("pplboy/test") |
|
|
base_model = AutoModelForCausalLM.from_pretrained( |
|
|
config.base_model_name_or_path, |
|
|
torch_dtype="auto", |
|
|
device_map="auto" |
|
|
) |
|
|
model = PeftModel.from_pretrained(base_model, "pplboy/test") |
|
|
tokenizer = AutoTokenizer.from_pretrained("pplboy/test") |
|
|
|
|
|
# 创建 pipeline |
|
|
pipe = pipeline( |
|
|
"text-generation", |
|
|
model=model, |
|
|
tokenizer=tokenizer, |
|
|
device_map="auto" |
|
|
) |
|
|
|
|
|
# 生成回复 |
|
|
result = pipe("你好,我想咨询一下产品", max_new_tokens=100) |
|
|
print(result[0]['generated_text']) |
|
|
``` |
|
|
|
|
|
## 模型信息 |
|
|
|
|
|
### LoRA 配置 |
|
|
|
|
|
- **LoRA rank (r)**: 8 |
|
|
- **LoRA alpha**: 16 |
|
|
- **LoRA dropout**: 0.1 |
|
|
- **Target modules**: q_proj, v_proj |
|
|
|
|
|
### 训练信息 |
|
|
|
|
|
- **训练框架**: PEFT 0.16.0 |
|
|
- **训练方法**: LoRA 微调 |
|
|
- **基础模型**: Qwen2.5-0.5B-Instruct |
|
|
|
|
|
## 使用场景 |
|
|
|
|
|
- 智能客服系统 |
|
|
- 自动问答 |
|
|
- 对话机器人 |
|
|
- 客户支持 |
|
|
|
|
|
## 限制和注意事项 |
|
|
|
|
|
1. **需要基础模型**: 这是一个 LoRA 适配器,使用前需要先加载基础模型 `Qwen/Qwen2.5-0.5B-Instruct` |
|
|
2. **模型大小**: 基础模型约 1GB,LoRA 适配器约 2MB |
|
|
3. **内存要求**: 建议至少 4GB 内存(使用 GPU 可减少内存占用) |
|
|
4. **语言支持**: 主要支持中文,英文能力有限 |
|
|
|
|
|
## 示例 |
|
|
|
|
|
```python |
|
|
# 完整示例 |
|
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
from peft import PeftModel |
|
|
|
|
|
# 加载 |
|
|
base_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct") |
|
|
model = PeftModel.from_pretrained(base_model, "pplboy/test") |
|
|
tokenizer = AutoTokenizer.from_pretrained("pplboy/test") |
|
|
|
|
|
# 测试对话 |
|
|
conversations = [ |
|
|
"你好,我想咨询一下产品", |
|
|
"这个产品有什么特点?", |
|
|
"如何退货?", |
|
|
"客服工作时间是什么时候?" |
|
|
] |
|
|
|
|
|
for prompt in conversations: |
|
|
inputs = tokenizer(prompt, return_tensors="pt").to(model.device) |
|
|
outputs = model.generate(**inputs, max_new_tokens=100, temperature=0.7) |
|
|
response = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
print(f"Q: {prompt}") |
|
|
print(f"A: {response}\n") |
|
|
``` |
|
|
|
|
|
## 引用 |
|
|
|
|
|
如果使用本模型,请引用: |
|
|
|
|
|
```bibtex |
|
|
@misc{pplboy-test-2024, |
|
|
title={Qwen2.5-0.5B-Instruct 客服微调模型}, |
|
|
author={pplboy}, |
|
|
year={2024}, |
|
|
howpublished={\url{https://huggingface.co/pplboy/test}} |
|
|
} |
|
|
``` |
|
|
|
|
|
## 许可证 |
|
|
|
|
|
本模型基于 Qwen2.5-0.5B-Instruct,遵循 MIT 许可证。 |
|
|
|
|
|
## 相关链接 |
|
|
|
|
|
- [基础模型](https://huggingface.co/Qwen/Qwen2.5-0.5B-Instruct) |
|
|
- [PEFT 文档](https://huggingface.co/docs/peft) |
|
|
- [Transformers 文档](https://huggingface.co/docs/transformers) |
|
|
|