abdullahalzubaer commited on
Commit
55ca3e6
·
verified ·
1 Parent(s): 485709a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +41 -0
README.md CHANGED
@@ -33,4 +33,45 @@ parameters:
33
  - value: 0.5
34
  dtype: bfloat16
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  ```
 
33
  - value: 0.5
34
  dtype: bfloat16
35
 
36
+ ```
37
+
38
+
39
+
40
+ ## Quickstart
41
+
42
+ Here provides a code snippet with `apply_chat_template` to show you how to load the tokenizer and model and how to generate contents.
43
+
44
+ ```python
45
+ from transformers import AutoModelForCausalLM, AutoTokenizer
46
+
47
+ model_name = "Qwen/Qwen2.5-0.5B-Instruct"
48
+
49
+ model = AutoModelForCausalLM.from_pretrained(
50
+ model_name,
51
+ torch_dtype="auto",
52
+ device_map="auto"
53
+ )
54
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
55
+
56
+ prompt = "Give me a short introduction to large language model."
57
+ messages = [
58
+ {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
59
+ {"role": "user", "content": prompt}
60
+ ]
61
+ text = tokenizer.apply_chat_template(
62
+ messages,
63
+ tokenize=False,
64
+ add_generation_prompt=True
65
+ )
66
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
67
+
68
+ generated_ids = model.generate(
69
+ **model_inputs,
70
+ max_new_tokens=512
71
+ )
72
+ generated_ids = [
73
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
74
+ ]
75
+
76
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
77
  ```