Views
No views yet
transformers + trl]1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "eth-nlped/MathDial-SFT-Qwen2.5-1.5B-Instruct"
4tokenizer = AutoTokenizer.from_pretrained(model_name)
5model = AutoModelForCausalLM.from_pretrained(model_name)
6
7# The model was trained with conversations that include:
8# The System prompt with the student's name (in this example "Mariana"), A math word problem with the correct solution and the student's incorrect solution.
9# Then the Tutor (assistant) asks the student (user) to explain their solution
10# Followed by the student's explanation
11# The conversation can be extended by adding another tutor response and the student's next message.
12# For more conversations, check out the MathDial dataset, linked above
13messages = [
14 {"content": "You are a friendly and supportive teacher.\nThe student, with the name Mariana, is trying to solve the following problem: Julia was preparing for a dinner party at her house, where she intended to serve stew. She noticed that she was out of plastic spoons, so she bought a new package of spoons. Later, her husband also bought a package of 5 new spoons and gave them to Julia. While Julia was making the stew, she used three of the spoons to sample her stew. Later, when she went to set the table, she had a total of 12 spoons. How many spoons were in the package that Julia bought?.\n\nThe correct solution is as follows:\nThe total number of spoons from Julia and her husband was 12+3=15 spoons.\nSince the husband bought a package of five spoons, then Julia's package contained 15-5=10 spoons.\n 10\n","role": "system",},
15 {"content": "Let's call the number of spoons Julia bought \"x\". \nHer husband bought 5 more spoons, so the total number of spoons is now x + 5. \nJulia used 3 spoons to sample her stew, so she had 12 - 3 = 9 spoons left. \nWe know that the total number of spoons is x + 5, so we can set up an equation: \n\nx + 5 = 9 \n\nSubtracting 5 from both sides: \n\nx = 4 \n\nSo Julia bought a package of 4 spoons. \n 4","role": "user",},
16 {"content": "Hi Mariana, please talk me through your solution","role": "assistant",},
17 {"content": "Sure. I started by letting x be the number of spoons Julia bought. Then I added 5 to x to get the total number of spoons. Next, I subtracted 3 from the total number of spoons to get the number of spoons left. Finally, I set up an equation and solved for x, which was 4. So Julia bought a package of 4 spoons.","role": "user",},
18]
19#apply chat template
20chat_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
21inputs = tokenizer(chat_text, return_tensors="pt").to(model.device)
22outputs = model.generate(**inputs, max_new_tokens=512)
23print(tokenizer.decode(outputs[0], skip_special_tokens=True))
24#Example output: excellent start. lets work from the top. if we know she has 12 spoons left, and already used 3. how many did she start with?