Laurie commited on
Commit
9d7391f
1 Parent(s): 741b989

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +98 -3
README.md CHANGED
@@ -1,3 +1,98 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ datasets:
4
+ - KingNish/reasoning-base-20k
5
+ language:
6
+ - en
7
+ - zh
8
+ base_model:
9
+ - Qwen/Qwen2.5-1.5B
10
+ ---
11
+
12
+ ## Uses
13
+
14
+ from transformers import AutoModelForCausalLM, AutoTokenizer
15
+
16
+ model_name = "/root/app/Reason/checkpoints"
17
+
18
+ model = AutoModelForCausalLM.from_pretrained(
19
+ model_name,
20
+ torch_dtype="auto",
21
+ device_map="auto"
22
+ )
23
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
24
+
25
+ from typing import List, Dict
26
+ def new_apply_chat_template(history:List[Dict[str, str]], add_reasoning_generation_prompt:bool=True, add_assistant_generation_prompt:bool=False):
27
+ if add_reasoning_generation_prompt:
28
+ return "".join([f"<|im_start|>{i['role']}\n{i['content']}<|im_end|>\n" for i in history]) + "<|im_start|><|reasoning|>\n"
29
+ if add_assistant_generation_prompt:
30
+ return "".join([f"<|im_start|>{i['role']}\n{i['content']}<|im_end|>\n" for i in history]) + "<|im_start|>assistant\n"
31
+
32
+
33
+ from IPython.display import Markdown, display
34
+ device = "cuda"
35
+ history = []
36
+ history.append({"role": "system", "content": "You are a helpful assistant"})
37
+ while True:
38
+ question = input('User:' + '\n')
39
+ print(question)
40
+ print('\n')
41
+ history.append({"role": "user", "content": question})
42
+
43
+ input_text = new_apply_chat_template(
44
+ history,
45
+ add_reasoning_generation_prompt=True
46
+ )
47
+ model_inputs = tokenizer([input_text], return_tensors="pt").to(device)
48
+
49
+ if model_inputs.input_ids.size()[1]>32000:
50
+ break
51
+
52
+ generated_ids = model.generate(
53
+ model_inputs.input_ids,
54
+ max_new_tokens=3000
55
+ )
56
+
57
+ if len(generated_ids)>32000:
58
+ break
59
+
60
+ generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
61
+
62
+ reasoning_response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
63
+ history.append({"role": "<|reasoning|>", "content": reasoning_response})
64
+ print('reasoning:\n')
65
+ #print(response)
66
+ display(Markdown(reasoning_response))
67
+ print("------------")
68
+ print('\n')
69
+
70
+ input_text = new_apply_chat_template(
71
+ history,
72
+ add_assistant_generation_prompt=True
73
+ )
74
+ model_inputs = tokenizer([input_text], return_tensors="pt").to(device)
75
+
76
+ if model_inputs.input_ids.size()[1]>32000:
77
+ break
78
+
79
+ generated_ids = model.generate(
80
+ model_inputs.input_ids,
81
+ max_new_tokens=3000
82
+ )
83
+
84
+ if len(generated_ids)>32000:
85
+ break
86
+
87
+ generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
88
+
89
+ assistant_response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
90
+ history.append({"role": "assistant", "content": assistant_response})
91
+ print('assistant:\n')
92
+ display(Markdown(assistant_response))
93
+ print("------------")
94
+
95
+ print("超过模型字数上线,已退出")
96
+
97
+
98
+