Downloads · 30 days
14
4% of all-time downloads
mojoz/qwen2.5-7b-instruct-lora-finetuned
qwen2.5-7b-instruct-lora-finetuned is a text generation model from mojoz. Use it when you need the model to write or continue text. It is set up for peft. The card lists the license as apache-2.0.
基于应用层工具调用架构的 LoRA 微调模型,模型原生具备"工具调用完成后的结果输出"能力
Downloads · 30 days
14
4% of all-time downloads
All-time downloads
383
Public
Repo size
92.2 MB
Likes
0
Public
Click a slice to open those files.
.safetensors80.8 MB · 73%
From the Hugging Face model README
基于应用层工具调用架构的 LoRA 微调模型,模型原生具备"工具调用完成后的结果输出"能力
本模型使用自定义应用层工具调用架构,与 Qwen 官方的 <tool_call=> 格式不同。请务必阅读 TOOL_ARCHITECTURE.md 了解完整的架构说明和使用方法。
本模型是在 Qwen/Qwen2.5-7B-Instruct 基础上,使用 LoRA 微调的东山论知识助手。它通过"应用层工具调用架构"实现了四种工具的调用能力:
| 工具 | 标签格式 | 功能 |
|---|---|---|
| 数学计算 | <calculator>表达式</calculator> | 精确数学运算 |
| 获取时间 | <clock/> | 当前日期和时间 |
| 网页搜索 | <search>查询内容</search> | 互联网信息检索 |
| 知识检索 | <lookup>关键词</lookup> | 专业知识库查询 |
此外,模型还支持推理框架标签(<focus/>, <step/>, <verify/>, <reflect/>, <correct/> 等),用于结构化推理过程。
| 对比维度 | Qwen 官方工具调用 | 本模型(应用层方式) |
|---|---|---|
| 调用格式 | <tool_call={"name":"...","arguments":{...}}> | <calculator>...</calculator> 等XML标签 |
| 执行方式 | 外部框架执行 | 应用层包裹模型,拦截标签并执行 |
| 训练方式 | 仅训练请求格式 | 应用层与模型一起训练 |
| 最终效果 | 模型能"请求"使用工具 | 模型能"带着工具结果回答问题" |
用户输入 → 应用层 → 调用模型生成
↓
解析输出中的工具标签
↓
有工具标签?──→ 执行工具 → 注入结果 → 继续调用模型
↓(无)
输出最终回答
核心:应用层与模型是一体的。应用层负责执行,模型负责综合,两者协作训练。
from peft import PeftModel
from unsloth import FastLanguageModel
# 1. 加载基础模型
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="Qwen/Qwen2.5-7B-Instruct",
max_seq_length=2048,
load_in_4bit=True,
)
# 2. 加载 LoRA 适配器
model = PeftModel.from_pretrained(model, "mojoz/qwen2.5-7b-instruct-lora-finetuned")
FastLanguageModel.for_inference(model)
# 3. 使用应用层(推荐)
from app_layer import DongshanAppLayer
app = DongshanAppLayer(model, tokenizer)
result = app.chat("计算 123 * 456")
print(result) # 根据计算结果,123 * 456 = 56088。
import re
SYSTEM_PROMPT = """你是东山论知识助手。你有以下工具能力:
1. 数学计算:使用 <calculator>表达式</calculator> 获取精确计算结果
2. 获取时间:使用 <clock/> 获取当前时间日期
3. 网页搜索:使用 <search>查询内容</search> 搜索互联网信息
4. 知识检索:使用 <lookup>关键词</lookup> 检索专业知识库
使用规则:
- 简单问题直接回答,不使用工具
- 需要精确计算时使用 calculator
- 需要当前时间信息时使用 clock
- 需要实时或未知信息时使用 search
- 需要专业知识时使用 lookup
- 工具可以组合使用"""
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": "计算 123 * 456"},
]
# 第一轮:模型输出工具调用
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=256, temperature=0.3, do_sample=True)
response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
print(response) # 让我来计算一下。<calculator>123 * 456</calculator>
# 应用层执行工具并注入结果
tool_result = "[工具执行结果 | calculator(123 * 456)]\n结果: 56088\n\n请结合以上工具执行结果,给出最终回答。"
messages.append({"role": "assistant", "content": response})
messages.append({"role": "user", "content": tool_result})
# 第二轮:模型综合工具结果给出最终回答
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=256, temperature=0.3, do_sample=True)
final = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
print(final) # 根据计算结果,123 * 456 = 56088。
| 参数 | 值 |
|---|---|
| 基础模型 | Qwen/Qwen2.5-7B-Instruct |
| 微调方法 | LoRA (PEFT) |
| 量化 | 4-bit NF4 (bitsandbytes) |
| LoRA Rank | 16 |
| LoRA Alpha | 32 |
| 目标模块 | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
| 训练轮数 | 3 |
| 学习率 | 2e-4 |
| 批次大小 | 2 (梯度累积: 4) |
| GPU | Tesla T4 (14.6 GB VRAM) |
| 可训练参数 | 40,370,176 / 7,655,986,688 (0.53%) |
训练数据来源于 ctz168/ds_qwen_colab,共 1516 条多轮工具调用对话,包括:
<calculator> 工具的精确计算题<search> 工具的信息检索题<lookup> 工具的东山论专业知识题<clock/> 工具的时间查询├── README.md # 本文件
├── TOOL_ARCHITECTURE.md # 应用层工具调用架构详细说明
├── adapter_config.json # LoRA 适配器配置
├── adapter_model.safetensors # LoRA 适配器权重
├── tokenizer.json # 分词器
├── tokenizer_config.json # 分词器配置(含 chat_template)
├── tools/
│ ├── app_layer.py # 应用层运行时代码
│ ├── generate_final.py # 训练数据生成脚本
│ └── data_examples/ # 训练数据示例
│ ├── train_dongshan_v9_extra.jsonl
│ ├── train_focus_v9.jsonl
│ └── train_multi_search_v9.jsonl
└── benchmarks/
├── BENCHMARK.md # 基准测试报告
└── benchmark_results.json # 基准测试数据