[25.06.10] update code
This commit is contained in:
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
+4
-9
@@ -1,8 +1,5 @@
|
|||||||
from transformers import AutoModelForCausalLM, AutoTokenizer
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||||
|
model_name = ""
|
||||||
model_name = "Qwen/Qwen3-8B"
|
|
||||||
|
|
||||||
# load the tokenizer and the model
|
|
||||||
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
||||||
model = AutoModelForCausalLM.from_pretrained(
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
model_name,
|
model_name,
|
||||||
@@ -10,7 +7,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|||||||
device_map="auto"
|
device_map="auto"
|
||||||
)
|
)
|
||||||
|
|
||||||
# prepare the model input
|
|
||||||
prompt = "Give me a short introduction to large language model."
|
prompt = "Give me a short introduction to large language model."
|
||||||
messages = [
|
messages = [
|
||||||
{"role": "user", "content": prompt}
|
{"role": "user", "content": prompt}
|
||||||
@@ -19,20 +16,18 @@ text = tokenizer.apply_chat_template(
|
|||||||
messages,
|
messages,
|
||||||
tokenize=False,
|
tokenize=False,
|
||||||
add_generation_prompt=True,
|
add_generation_prompt=True,
|
||||||
enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
|
enable_thinking=True
|
||||||
)
|
)
|
||||||
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
||||||
|
|
||||||
# conduct text completion
|
|
||||||
generated_ids = model.generate(
|
generated_ids = model.generate(
|
||||||
**model_inputs,
|
**model_inputs,
|
||||||
max_new_tokens=32768
|
max_new_tokens=32768
|
||||||
)
|
)
|
||||||
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
||||||
|
|
||||||
# parsing thinking content
|
|
||||||
try:
|
try:
|
||||||
# rindex finding 151668 (</think>)
|
|
||||||
index = len(output_ids) - output_ids[::-1].index(151668)
|
index = len(output_ids) - output_ids[::-1].index(151668)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
index = 0
|
index = 0
|
||||||
|
|||||||
Regular → Executable
Binary file not shown.
Binary file not shown.
Binary file not shown.
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Reference in New Issue
Block a user