Views
No views yet
1# test the model
2import torch
3from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
4
5def main():
6 model_id = "CreitinGameplays/Llama-3.1-8b-reasoning-test"
7
8 # Load the tokenizer.
9 tokenizer = AutoTokenizer.from_pretrained(model_id, add_eos_token=True)
10
11 # Load the model using bitsandbytes 8-bit quantization if CUDA is available.
12 if torch.cuda.is_available():
13 model = AutoModelForCausalLM.from_pretrained(
14 model_id,
15 load_in_8bit=True,
16 device_map="auto"
17 )
18 device = torch.device("cuda")
19 else:
20 model = AutoModelForCausalLM.from_pretrained(model_id)
21 device = torch.device("cpu")
22
23 # Define the generation parameters.
24 generation_kwargs = {
25 "max_new_tokens": 2048,
26 "do_sample": True,
27 "temperature": 0.4,
28 "top_k": 50,
29 "top_p": 0.95,
30 "repetition_penalty": 1.0,
31 "num_return_sequences": 1,
32 "forced_eos_token_id": tokenizer.eos_token_id,
33 "pad_token_id": tokenizer.eos_token_id
34 }
35
36 print("Enter your prompt (type 'exit' to quit):")
37 while True:
38 # Get user input.
39 user_input = input("Input> ")
40 if user_input.lower().strip() in ("exit", "quit"):
41 break
42
43 # Construct the prompt in your desired format.
44 prompt = f"""
45<|begin_of_text|><|start_header_id|>system<|end_header_id|>
46
47You are a helpful assistant named Llama, made by Meta AI. Always use your <|end_reasoning|> token when ending a reason step, without text formatting.<|eot_id|><|start_header_id|>user<|end_header_id|>
48
49{user_input}<|eot_id|><|start_header_id|>assistant<|end_header_id|> <|reasoning|>
50"""
51
52 # Tokenize the prompt and send to the selected device.
53 input_ids = tokenizer.encode(prompt, return_tensors="pt", add_special_tokens=True).to(device)
54
55 # Create a new TextStreamer instance for streaming responses.
56 streamer = TextStreamer(tokenizer)
57 generation_kwargs["streamer"] = streamer
58
59 print("\nAssistant Response:")
60 # Generate the text (tokens will stream to stdout via the streamer).
61 outputs = model.generate(input_ids, **generation_kwargs)
62
63if __name__ == "__main__":
64 main()<|begin_of_text|>
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are a helpful assistant named Llama, made by Meta AI. Always use your <|end_reasoning|> when ending a reason step.<|eot_id|><|start_header_id|>user<|end_header_id|>
how many r's are in strawberry?<|eot_id|><|start_header_id|>assistant<|end_header_id|><|reasoning|>
To determine the number of R's in the word "strawberry", we need to break down the word into its individual letters.
The word "strawberry" can be spelled out as S-T-R-A-W-B-E-R-R-Y. Now, let's examine each letter to count the number of R's.
1. S: There is no R in the letter S.
2. T: There is no R in the letter T.
3. R: Ah, we have our first R!
4. A: There is no R in the letter A.
5. W: There is no R in the letter W.
6. B: There is no R in the letter B.
7. E: There is no R in the letter E.
8. R: Another R! We now have two R's.
9. R: And another R! We now have three R's.
10. Y: There is no R in the letter Y.
So, after counting all the letters, we can see that the word "strawberry" contains three R's.
Therefore, the answer is 3.
<|end_reasoning|>
3. The word "strawberry" contains 3 R's.<|eot_id|>