Mehdi-Zogh commited on
Commit
641ddc5
verified
1 Parent(s): ff6116b

doc: update the usage code snippet

Browse files
Files changed (1) hide show
  1. README.md +44 -8
README.md CHANGED
@@ -54,15 +54,51 @@ It can serve as a base model for further alignment, personalization, or integrat
54
  ## Get Started with the Model
55
 
56
  ```python
57
- from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- tokenizer = AutoTokenizer.from_pretrained("Mehdi-Zogh/MNLP_M2_dpo_model")
60
- model = AutoModelForCausalLM.from_pretrained("Mehdi-Zogh/MNLP_M2_dpo_model")
61
-
62
- prompt = "Explain how gradient descent works in simple terms."
63
- inputs = tokenizer(prompt, return_tensors="pt")
64
- outputs = model.generate(**inputs, max_new_tokens=200)
65
- print(tokenizer.decode(outputs[0], skip_special_tokens=True))
66
  ```
67
 
68
 
 
54
  ## Get Started with the Model
55
 
56
  ```python
57
+ from transformers import AutoModelForCausalLM, AutoTokenizer
58
+
59
+ model_name = "Mehdi-Zogh/MNLP_M2_dpo_model"
60
+
61
+ # load the tokenizer and the model
62
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
63
+ model = AutoModelForCausalLM.from_pretrained(
64
+ model_name,
65
+ torch_dtype="auto",
66
+ device_map="auto"
67
+ )
68
+
69
+ # prepare the model input
70
+ prompt = "explain gradient descent in simple terms."
71
+ messages = [
72
+ {"role": "user", "content": prompt}
73
+ ]
74
+ text = tokenizer.apply_chat_template(
75
+ messages,
76
+ tokenize=False,
77
+ add_generation_prompt=True,
78
+ enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
79
+ )
80
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
81
+
82
+ # conduct text completion
83
+ generated_ids = model.generate(
84
+ **model_inputs,
85
+ max_new_tokens=32768
86
+ )
87
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
88
+
89
+ # parsing thinking content
90
+ try:
91
+ # rindex finding 151668 (</think>)
92
+ index = len(output_ids) - output_ids[::-1].index(151668)
93
+ except ValueError:
94
+ index = 0
95
+
96
+ thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
97
+ content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
98
+
99
+ print("thinking content:", thinking_content)
100
+ print("content:", content)
101
 
 
 
 
 
 
 
 
102
  ```
103
 
104