migtissera commited on
Commit
fdd4714
·
verified ·
1 Parent(s): 9a038bb

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +128 -3
README.md CHANGED
@@ -1,3 +1,128 @@
1
- ---
2
- license: llama3.1
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: llama3.1
3
+ base_model: meta-llama/Llama-3.1-70B
4
+ model-index:
5
+ - name: Tess-R1-Llama-3.1-70B
6
+ results: []
7
+ ---
8
+
9
+ <br>
10
+
11
+ ![Tess-R1-Llama-3.1-70B](https://huggingface.co/migtissera/Tess-R1-Llama-3.1-70B/resolve/main/Tess-R1-2.jpg)
12
+
13
+ <br>
14
+
15
+
16
+ Welcome to the Tess-Reasoning-1 (Tess-R1) series of models. Tess-R1 is designed with test-time compute in mind, and has the capabilities to produce a Chain-of-Thought (CoT) reasoning before producing the final output.
17
+
18
+ The model is trained to first think step-by-step, and contemplate on its answers. It can also write alternatives after contemplating. Once all the steps have been thought through, it writes the final output.
19
+
20
+ 1. Step-by-step, Chain-of-Thought thinking process. Uses `<thinking>` `</thinking>` tags to indicate when the model is performing CoT.
21
+ 2. `<contemplation>` `</contemplation>` tags are used when the model contemplate on its answers.
22
+ 3. `<alternatively>` `</alternatively>` tags are used for alternate suggestions.
23
+ 4. Finally, `<output>` `</output>` tags are used for the final output
24
+
25
+ # Important Note:
26
+ In a multi-turn conversation, only the contents between the `<output>` `</output>` tags (discarding the tags) should be carried forward. Otherwise the model will see out of distribution input data and will fail.
27
+
28
+
29
+ # Prompt Format
30
+
31
+ The model uses ChatML prompt format.
32
+
33
+ # System Message
34
+
35
+ The system message *must* be the following:
36
+
37
+ ```You are Tess-R1, an advanced AI that was created for complex reasoning. Given a user query, you are able to first create a Chain-of-Thought (CoT) reasoning. Once the CoT is devised, you then proceed to first think about how to answer. While doing this, you have the capability to contemplate on the thought, and also provide alternatives. Once the CoT steps have been thought through, you then respond by creating the final output.```
38
+
39
+ # Inference
40
+
41
+ The model was trained mostly with Chain-of-Thought reasoning data, including the XML tags. However, to generalize model generations, some single-turn and multi-turn data without XML tags were also included. Due to this, in some instances the model does not produce XML tags and does not fully utilize test-time compute capabilities. Therefore you should include a try statement in your inference script, and only pass on the contents between the `<output>` `</output>` tags if it's available.
42
+
43
+ I have included a sample Python script below.
44
+
45
+ ```python
46
+ import torch, json
47
+ from transformers import AutoModelForCausalLM, AutoTokenizer
48
+ import re
49
+
50
+
51
+ class LLM(object):
52
+ def __init__(self, model_path):
53
+ self.model = AutoModelForCausalLM.from_pretrained(
54
+ model_path,
55
+ torch_dtype=torch.bfloat16,
56
+ device_map="auto",
57
+ load_in_4bit=True,
58
+ trust_remote_code=False,
59
+ )
60
+
61
+ self.tokenizer = AutoTokenizer.from_pretrained(
62
+ model_path, trust_remote_code=False
63
+ )
64
+
65
+ self.terminators = [
66
+ self.tokenizer.convert_tokens_to_ids("<|im_end|>"),
67
+ self.tokenizer.convert_tokens_to_ids("<|end_of_text|>"),
68
+ ]
69
+
70
+ def generate_text(self, instruction):
71
+ tokens = self.tokenizer.encode(instruction)
72
+ tokens = torch.LongTensor(tokens).unsqueeze(0)
73
+ tokens = tokens.to("cuda")
74
+
75
+ instance = {
76
+ "input_ids": tokens,
77
+ "top_p": 1.0,
78
+ "temperature": 0.75,
79
+ "generate_len": 1024,
80
+ "top_k": 50,
81
+ }
82
+
83
+ length = len(tokens[0])
84
+ with torch.no_grad():
85
+ rest = self.model.generate(
86
+ input_ids=tokens,
87
+ max_length=length + instance["generate_len"],
88
+ use_cache=True,
89
+ do_sample=True,
90
+ top_p=instance["top_p"],
91
+ temperature=instance["temperature"],
92
+ top_k=instance["top_k"],
93
+ num_return_sequences=1,
94
+ pad_token_id=self.tokenizer.eos_token_id,
95
+ eos_token_id=self.terminators,
96
+ )
97
+ output = rest[0][length:]
98
+ string = self.tokenizer.decode(output, skip_special_tokens=True)
99
+ return f"{string}"
100
+
101
+ def extract_output(self, text):
102
+ pattern = r"<output>(.*?)</output>"
103
+ match = re.search(pattern, text, re.DOTALL)
104
+ content = match.group(1).strip()
105
+ return content
106
+
107
+
108
+ model_path = "migtissera/Tess-R1-12B"
109
+
110
+ llm = LLM(model_path)
111
+
112
+ conversation = f"""<|im_start|>system\nYou are Tess-R1, an advanced AI that was created for complex reasoning. Given a user query, you are able to first create a Chain-of-Thought (CoT) reasoning. Once the CoT is devised, you then proceed to first think about how to answer. While doing this, you have the capability to contemplate on the thought, and also provide alternatives. Once the CoT steps have been thought through, you then respond by creating the final output.<|im_end|>\n<|im_start|>user\n"""
113
+
114
+ while True:
115
+ user_input = input("You: ")
116
+ llm_prompt = f"{conversation}{user_input}<|im_end|>\n<|im_start|>assistant\n"
117
+ answer = llm.generate_text(llm_prompt)
118
+
119
+ try:
120
+ answer_output = llm.extract_output(answer)
121
+ print("=" * 132)
122
+ print(answer_output)
123
+ conversation = f"{llm_prompt}{answer_output}<|im_end|>\n<|im_start|>user\n"
124
+ except:
125
+ print("=" * 132)
126
+ print(answer)
127
+ conversation = f"{llm_prompt}{answer}<|im_end|>\n<|im_start|>user\n"
128
+ ```