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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
base_model: Qwen/Qwen2-7B-Instruct
|
3 |
+
library_name: peft
|
4 |
+
license: other
|
5 |
+
tags:
|
6 |
+
- llama-factory
|
7 |
+
- lora
|
8 |
+
- generated_from_trainer
|
9 |
+
model-index:
|
10 |
+
- name: train_2024-06-17-19-49-05
|
11 |
+
results: []
|
12 |
+
---
|
13 |
+
|
14 |
+
<!-- This model card has been generated automatically according to the information the Trainer had access to. You
|
15 |
+
should probably proofread and complete it, then remove this comment. -->
|
16 |
+
|
17 |
+
# Install some dependency
|
18 |
+
```bash
|
19 |
+
pip install peft transformers bitsandbytes
|
20 |
+
```
|
21 |
+
|
22 |
+
# Inference
|
23 |
+
```python
|
24 |
+
import json
|
25 |
+
import re
|
26 |
+
from abc import ABC, abstractmethod
|
27 |
+
from dataclasses import dataclass, field
|
28 |
+
from typing import Any, Dict, List, Literal, Optional, Sequence, Set, Tuple, Union
|
29 |
+
|
30 |
+
def calculate_gpa(grades: Sequence[str], hours: Sequence[int]) -> float:
|
31 |
+
grade_to_score = {"A": 4, "B": 3, "C": 2}
|
32 |
+
total_score, total_hour = 0, 0
|
33 |
+
for grade, hour in zip(grades, hours):
|
34 |
+
total_score += grade_to_score[grade] * hour
|
35 |
+
total_hour += hour
|
36 |
+
return round(total_score / total_hour, 2)
|
37 |
+
|
38 |
+
tool_map = {"calculate_gpa": calculate_gpa}
|
39 |
+
|
40 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
|
41 |
+
from peft import PeftModel
|
42 |
+
|
43 |
+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-7B-Instruct")
|
44 |
+
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-7B-Instruct",
|
45 |
+
torch_dtype="auto", device_map="auto", load_in_4bit = True)
|
46 |
+
|
47 |
+
model = PeftModel.from_pretrained(model, "svjack/Qwen2-7B_Function_Call_tiny_lora")
|
48 |
+
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
49 |
+
|
50 |
+
SLOTS = Sequence[Union[str, Set[str], Dict[str, str]]]
|
51 |
+
|
52 |
+
|
53 |
+
DEFAULT_TOOL_PROMPT = (
|
54 |
+
"You have access to the following tools:\n{tool_text}"
|
55 |
+
"Use the following format if using a tool:\n"
|
56 |
+
"```\n"
|
57 |
+
"Action: tool name (one of [{tool_names}]).\n"
|
58 |
+
"Action Input: the input to the tool, in a JSON format representing the kwargs "
|
59 |
+
"""(e.g. ```{{"input": "hello world", "num_beams": 5}}```).\n"""
|
60 |
+
"```\n"
|
61 |
+
)
|
62 |
+
|
63 |
+
def default_tool_formatter(tools: List[Dict[str, Any]]) -> str:
|
64 |
+
tool_text = ""
|
65 |
+
tool_names = []
|
66 |
+
for tool in tools:
|
67 |
+
param_text = ""
|
68 |
+
for name, param in tool["parameters"]["properties"].items():
|
69 |
+
required = ", required" if name in tool["parameters"].get("required", []) else ""
|
70 |
+
enum = ", should be one of [{}]".format(", ".join(param["enum"])) if param.get("enum", None) else ""
|
71 |
+
items = (
|
72 |
+
", where each item should be {}".format(param["items"].get("type", "")) if param.get("items") else ""
|
73 |
+
)
|
74 |
+
param_text += " - {name} ({type}{required}): {desc}{enum}{items}\n".format(
|
75 |
+
name=name,
|
76 |
+
type=param.get("type", ""),
|
77 |
+
required=required,
|
78 |
+
desc=param.get("description", ""),
|
79 |
+
enum=enum,
|
80 |
+
items=items,
|
81 |
+
)
|
82 |
+
|
83 |
+
tool_text += "> Tool Name: {name}\nTool Description: {desc}\nTool Args:\n{args}\n".format(
|
84 |
+
name=tool["name"], desc=tool.get("description", ""), args=param_text
|
85 |
+
)
|
86 |
+
tool_names.append(tool["name"])
|
87 |
+
|
88 |
+
return DEFAULT_TOOL_PROMPT.format(tool_text=tool_text, tool_names=", ".join(tool_names))
|
89 |
+
|
90 |
+
def default_tool_extractor(content: str) -> Union[str, List[Tuple[str, str]]]:
|
91 |
+
regex = re.compile(r"Action:\s*([a-zA-Z0-9_]+)\s*Action Input:\s*(.+?)(?=\s*Action:|\s*$)", re.DOTALL)
|
92 |
+
action_match: List[Tuple[str, str]] = re.findall(regex, content)
|
93 |
+
if not action_match:
|
94 |
+
return content
|
95 |
+
|
96 |
+
results = []
|
97 |
+
for match in action_match:
|
98 |
+
tool_name = match[0].strip()
|
99 |
+
tool_input = match[1].strip().strip('"').strip("```")
|
100 |
+
try:
|
101 |
+
arguments = json.loads(tool_input)
|
102 |
+
results.append((tool_name, json.dumps(arguments, ensure_ascii=False)))
|
103 |
+
except json.JSONDecodeError:
|
104 |
+
return content
|
105 |
+
|
106 |
+
return results
|
107 |
+
|
108 |
+
#### Function tool defination
|
109 |
+
tools = [
|
110 |
+
{
|
111 |
+
"type": "function",
|
112 |
+
"function": {
|
113 |
+
"name": "calculate_gpa",
|
114 |
+
"description": "Calculate the Grade Point Average (GPA) based on grades and credit hours",
|
115 |
+
"parameters": {
|
116 |
+
"type": "object",
|
117 |
+
"properties": {
|
118 |
+
"grades": {"type": "array", "items": {"type": "string"}, "description": "The grades"},
|
119 |
+
"hours": {"type": "array", "items": {"type": "integer"}, "description": "The credit hours"},
|
120 |
+
},
|
121 |
+
"required": ["grades", "hours"],
|
122 |
+
},
|
123 |
+
},
|
124 |
+
}
|
125 |
+
]
|
126 |
+
|
127 |
+
tools_input = list(map(lambda x: x["function"], tools))
|
128 |
+
system_tool_prompt = default_tool_formatter(tools_input)
|
129 |
+
#print(system_tool_prompt)
|
130 |
+
|
131 |
+
def qwen_hf_predict(messages, qw_model = model,
|
132 |
+
tokenizer = tokenizer, streamer = streamer,
|
133 |
+
do_sample = True,
|
134 |
+
top_p = 0.95,
|
135 |
+
top_k = 40,
|
136 |
+
max_new_tokens = 512,
|
137 |
+
max_input_length = 3500,
|
138 |
+
temperature = 0.9,
|
139 |
+
repetition_penalty = 1.0,
|
140 |
+
device = "cuda"):
|
141 |
+
|
142 |
+
encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt",
|
143 |
+
add_generation_prompt=True
|
144 |
+
)
|
145 |
+
model_inputs = encodeds.to(device)
|
146 |
+
|
147 |
+
generated_ids = qw_model.generate(model_inputs, max_new_tokens=max_new_tokens,
|
148 |
+
do_sample=do_sample,
|
149 |
+
streamer = streamer,
|
150 |
+
top_p = top_p,
|
151 |
+
top_k = top_k,
|
152 |
+
temperature = temperature,
|
153 |
+
repetition_penalty = repetition_penalty,
|
154 |
+
)
|
155 |
+
out = tokenizer.batch_decode(generated_ids)[0].split("<|im_start|>assistant")[-1].replace("<|im_end|>", "").strip()
|
156 |
+
return out
|
157 |
+
|
158 |
+
messages = [
|
159 |
+
{
|
160 |
+
"role" :"system",
|
161 |
+
"content": system_tool_prompt
|
162 |
+
},
|
163 |
+
{"role": "user", "content": "My grades are A, A, B, and C. The credit hours are 3, 4, 3, and 2."}
|
164 |
+
]
|
165 |
+
|
166 |
+
out = qwen_hf_predict(messages)
|
167 |
+
tool_out = default_tool_extractor(out)
|
168 |
+
print(tool_out)
|
169 |
+
|
170 |
+
name, arguments = tool_out[0][0], json.loads(tool_out[0][1])
|
171 |
+
tool_result = tool_map[name](**arguments)
|
172 |
+
print(tool_result)
|
173 |
+
|
174 |
+
messages.append(
|
175 |
+
{
|
176 |
+
"role" :"assistant",
|
177 |
+
"content": out
|
178 |
+
}
|
179 |
+
)
|
180 |
+
|
181 |
+
messages.append({"role": "tool", "content": json.dumps({"gpa": tool_result}, ensure_ascii=False)})
|
182 |
+
|
183 |
+
final_out = qwen_hf_predict(messages)
|
184 |
+
print(final_out)
|
185 |
+
```
|
186 |
+
|
187 |
+
# Output
|
188 |
+
```
|
189 |
+
Action: calculate_gpa
|
190 |
+
Action Input: {"grades": ["A", "A", "B", "C"], "hours": [3, 4, 3, 2]}
|
191 |
+
[('calculate_gpa', '{"grades": ["A", "A", "B", "C"], "hours": [3, 4, 3, 2]}')]
|
192 |
+
3.42
|
193 |
+
Based on the grades and credit hours you provided, your Grade Point Average (GPA) is 3.42.
|
194 |
+
```
|
195 |
+
|
196 |
+
# Inference
|
197 |
+
```python
|
198 |
+
messages = [
|
199 |
+
{
|
200 |
+
"role" :"system",
|
201 |
+
"content": system_tool_prompt
|
202 |
+
},
|
203 |
+
{"role": "user", "content": "我的成绩分别是A,A,B,C学分分别是3, 4, 3,和2"}
|
204 |
+
]
|
205 |
+
|
206 |
+
out = qwen_hf_predict(messages)
|
207 |
+
tool_out = default_tool_extractor(out)
|
208 |
+
print(tool_out)
|
209 |
+
|
210 |
+
name, arguments = tool_out[0][0], json.loads(tool_out[0][1])
|
211 |
+
tool_result = tool_map[name](**arguments)
|
212 |
+
print(tool_result)
|
213 |
+
|
214 |
+
messages.append(
|
215 |
+
{
|
216 |
+
"role" :"assistant",
|
217 |
+
"content": out
|
218 |
+
}
|
219 |
+
)
|
220 |
+
|
221 |
+
messages.append({"role": "tool", "content": json.dumps({"gpa": tool_result}, ensure_ascii=False)})
|
222 |
+
|
223 |
+
final_out = qwen_hf_predict(messages)
|
224 |
+
print(final_out)
|
225 |
+
```
|
226 |
+
|
227 |
+
# Output
|
228 |
+
```
|
229 |
+
Action: calculate_gpa
|
230 |
+
Action Input: {"grades": ["A", "A", "B", "C"], "hours": [3, 4, 3, 2]}
|
231 |
+
[('calculate_gpa', '{"grades": ["A", "A", "B", "C"], "hours": [3, 4, 3, 2]}')]
|
232 |
+
3.42
|
233 |
+
您的绩点(GPA)是3.42。
|
234 |
+
```
|
235 |
+
|
236 |
+
|
237 |
+
# train_2024-06-17-19-49-05
|
238 |
+
|
239 |
+
This model is a fine-tuned version of [Qwen/Qwen2-7B-Instruct](https://huggingface.co/Qwen/Qwen2-7B-Instruct) on the glaive_toolcall_zh and the glaive_toolcall_en datasets.
|
240 |
+
|
241 |
+
## Model description
|
242 |
+
|
243 |
+
More information needed
|
244 |
+
|
245 |
+
## Intended uses & limitations
|
246 |
+
|
247 |
+
More information needed
|
248 |
+
|
249 |
+
## Training and evaluation data
|
250 |
+
|
251 |
+
More information needed
|
252 |
+
|
253 |
+
## Training procedure
|
254 |
+
|
255 |
+
### Training hyperparameters
|
256 |
+
|
257 |
+
The following hyperparameters were used during training:
|
258 |
+
- learning_rate: 5e-05
|
259 |
+
- train_batch_size: 1
|
260 |
+
- eval_batch_size: 8
|
261 |
+
- seed: 42
|
262 |
+
- distributed_type: multi-GPU
|
263 |
+
- num_devices: 2
|
264 |
+
- gradient_accumulation_steps: 8
|
265 |
+
- total_train_batch_size: 16
|
266 |
+
- total_eval_batch_size: 16
|
267 |
+
- optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
|
268 |
+
- lr_scheduler_type: cosine
|
269 |
+
- num_epochs: 3.0
|
270 |
+
- mixed_precision_training: Native AMP
|
271 |
+
|
272 |
+
### Training results
|
273 |
+
|
274 |
+
|
275 |
+
|
276 |
+
### Framework versions
|
277 |
+
|
278 |
+
- PEFT 0.11.1
|
279 |
+
- Transformers 4.41.2
|
280 |
+
- Pytorch 2.3.1+cu121
|
281 |
+
- Datasets 2.20.0
|
282 |
+
- Tokenizers 0.19.1
|