Views
No views yet
apply_chat_template to show you how to load the tokenizer and model and how to generate contents.1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "prithivMLmods/FastThink-0.5B-Tiny"
4
5model = AutoModelForCausalLM.from_pretrained(
6 model_name,
7 torch_dtype="auto",
8 device_map="auto"
9)
10tokenizer = AutoTokenizer.from_pretrained(model_name)
11
12prompt = "Give me a short introduction to large language model."
13messages = [
14 {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
15 {"role": "user", "content": prompt}
16]
17text = tokenizer.apply_chat_template(
18 messages,
19 tokenize=False,
20 add_generation_prompt=True
21)
22model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
23
24generated_ids = model.generate(
25 **model_inputs,
26 max_new_tokens=512
27)
28generated_ids = [
29 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
30]
31
32response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]datasets library to load and manipulate the datasets, and the chat_templates library to standardize the conversation format.1# Load the initial three datasets
2dataset1 = load_dataset("PowerInfer/LONGCOT-Refine-500K", split="train")
3dataset2 = load_dataset("amphora/QwQ-LongCoT-130K", split="train")
4dataset3 = load_dataset("AI-MO/NuminaMath-CoT", split="train")
5
6# Map conversation columns for all datasets
7dataset1 = dataset1.map(add_conversations_column, batched=False)
8dataset2 = dataset2.map(add_conversations_column_prompt_qwq, batched=False)
9dataset3 = dataset3.map(add_conversations_column_prompt_solution, batched=False)
10
11# Combine all datasets
12combined_dataset = concatenate_datasets([dataset1, dataset2, dataset3])
13
14# Standardize using the ShareGPT format
15combined_dataset = standardize_sharegpt(combined_dataset)
16
17# Initialize the tokenizer with a specific chat template
18tokenizer = get_chat_template(tokenizer, chat_template="qwen-2.5")
19
20# Apply formatting function to the combined dataset
21combined_dataset = combined_dataset.map(formatting_prompts_func, batched=True)
22
23# Print the first few examples to verify the output
24print(combined_dataset[:50000])