Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,88 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
language:
|
4 |
+
- en
|
5 |
+
base_model:
|
6 |
+
- Qwen/Qwen2.5-1.5B-Instruct
|
7 |
+
pipeline_tag: text-generation
|
8 |
+
---
|
9 |
+
|
10 |
+
# A language model with calculator-like functionality
|
11 |
+
|
12 |
+
* Supports up to 10 digit calculations
|
13 |
+
* Nearly 100% accuracy
|
14 |
+
* It use CoT to calculate, so the calculation process may be lengthy
|
15 |
+
* v0.1 only support addition, subtraction and multiplication.
|
16 |
+
* Addition supports the addition of multiple numbers, while subtraction and multiplication currently only supports operations with two numbers
|
17 |
+
|
18 |
+
## Quickstart
|
19 |
+
|
20 |
+
|
21 |
+
```python
|
22 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
23 |
+
model_name = "yuyijiong/llm_calculator_v0.1"
|
24 |
+
model = AutoModelForCausalLM.from_pretrained(
|
25 |
+
model_name,
|
26 |
+
torch_dtype="auto",
|
27 |
+
device_map="auto"
|
28 |
+
)
|
29 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
30 |
+
|
31 |
+
#addition
|
32 |
+
prompt = "1234+12345+123456=?"
|
33 |
+
|
34 |
+
#subtraction
|
35 |
+
prompt="1234-12345=?"
|
36 |
+
|
37 |
+
#multiply
|
38 |
+
prompt="1234*12345=?"
|
39 |
+
|
40 |
+
messages = [
|
41 |
+
{"role": "user", "content": prompt}
|
42 |
+
]
|
43 |
+
text = tokenizer.apply_chat_template(
|
44 |
+
messages,
|
45 |
+
tokenize=False,
|
46 |
+
add_generation_prompt=True
|
47 |
+
)
|
48 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
49 |
+
generated_ids = model.generate(
|
50 |
+
**model_inputs,
|
51 |
+
max_new_tokens=4096,
|
52 |
+
do_sample=False,
|
53 |
+
)
|
54 |
+
generated_ids = [
|
55 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
56 |
+
]
|
57 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
58 |
+
```
|
59 |
+
|
60 |
+
## Example
|
61 |
+
|
62 |
+
```
|
63 |
+
Q: 3563+123=?
|
64 |
+
|
65 |
+
A:
|
66 |
+
calculate 23 * 541:
|
67 |
+
(1, 1) 3 * 1 -> 3 + carry -> 3 -> [3] & carry 0 -> [3]
|
68 |
+
(1, 2) 3 * 4 -> 12 + carry -> 12 -> [2] & carry 1 -> [20]
|
69 |
+
(1, 3) 3 * 5 -> 15 + carry -> 16 -> [6] & carry 1 -> [1600]
|
70 |
+
temp result: 1623
|
71 |
+
(2, 1) 2 * 1 -> 2 + carry -> 2 -> [2] & carry 0 -> [20]
|
72 |
+
(2, 2) 2 * 4 -> 8 + carry -> 8 -> [8] & carry 0 -> [800]
|
73 |
+
(2, 3) 2 * 5 -> 10 + carry -> 10 -> [0] & carry 1 -> [10000]
|
74 |
+
temp result: 10820
|
75 |
+
gather temp results: 1623 + 10820
|
76 |
+
|
77 |
+
calculate 1623 + 10820:
|
78 |
+
|
79 |
+
calculate 1623 + 10820:
|
80 |
+
(1) 3 + 0 + carry -> 3 -> [3] & carry 0
|
81 |
+
(2) 2 + 2 + carry -> 4 -> [4] & carry 0
|
82 |
+
(3) 6 + 8 + carry -> 14 -> [4] & carry 1
|
83 |
+
(4) 1 + 0 + carry -> 2 -> [2] & carry 0
|
84 |
+
(5) 0 + 1 + carry -> 1 -> [1] & carry 0
|
85 |
+
gather results: 12443
|
86 |
+
final answer: 12443
|
87 |
+
|
88 |
+
```
|