File size: 2,265 Bytes
8a26651
 
 
 
 
 
 
fbc7ace
 
8a26651
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbc7ace
8a26651
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbc7ace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
license: apache-2.0
language:
- en
base_model:
- Qwen/Qwen2.5-1.5B-Instruct
pipeline_tag: text-generation
datasets:
- yuyijiong/llm_calculator_data
---

# A language model with calculator-like functionality

* Supports up to 10 digit calculations
* Nearly 100% accuracy
* It use CoT to calculate, so the calculation process may be lengthy
* v0.1 only support addition, subtraction and multiplication.
* Addition supports the addition of multiple numbers, while subtraction and multiplication currently only supports operations with two numbers

## Quickstart


```python
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "yuyijiong/llm_calculator_v0.1"
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)

#addition
prompt = "1234+12345+123456=?"

#subtraction
prompt="1234-12345=?"

#multiply
prompt="1234*12345=?"

messages = [

    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=4096,
    do_sample=False,
)
generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
```

## Example

```
Q: 3563+123=?

A:
calculate 23 * 541:
(1, 1) 3 * 1 -> 3 + carry -> 3 -> [3] & carry 0 -> [3]
(1, 2) 3 * 4 -> 12 + carry -> 12 -> [2] & carry 1 -> [20]
(1, 3) 3 * 5 -> 15 + carry -> 16 -> [6] & carry 1 -> [1600]
temp result: 1623
(2, 1) 2 * 1 -> 2 + carry -> 2 -> [2] & carry 0 -> [20]
(2, 2) 2 * 4 -> 8 + carry -> 8 -> [8] & carry 0 -> [800]
(2, 3) 2 * 5 -> 10 + carry -> 10 -> [0] & carry 1 -> [10000]
temp result: 10820
gather temp results: 1623 + 10820

calculate 1623 + 10820:

calculate 1623 + 10820:
(1) 3 + 0 + carry -> 3 -> [3] & carry 0
(2) 2 + 2 + carry -> 4 -> [4] & carry 0
(3) 6 + 8 + carry -> 14 -> [4] & carry 1
(4) 1 + 0 + carry -> 2 -> [2] & carry 0
(5) 0 + 1 + carry -> 1 -> [1] & carry 0
gather results: 12443
final answer: 12443

```