Views
No views yet
1import torch
2from unsloth import FastLanguageModel
3from transformers import AutoModelForCausalLM, AutoTokenizer
4
5model, tokenizer = FastLanguageModel.from_pretrained(model_name="amjad-awad/mistral-7b-instruct-v0.2-bnb-4bit-books-21K-lr-1e5",max_seq_length=2048,load_in_4bit=True)
6
7model = FastLanguageModel.get_peft_model(
8 model,
9 r=16,
10 target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
11 lora_alpha=16,
12 lora_dropout=0,
13 bias="none",
14 use_gradient_checkpointing=True,
15 random_state=3407,
16)
17
18user_content = (
19 "Provide both a score and a rationale by evaluating the student's answer strictly within the mark scheme range, "
20 "grading based on how well it meets the question's requirements by comparing the student answer to the reference answer.\n"
21 "Question: What is photosynthesis?\n"
22 "Reference Answer: Photosynthesis is the process by which green plants and some other organisms use sunlight to synthesize nutrients from carbon dioxide and water. It generally involves the green pigment chlorophyll and generates oxygen as a by-product.\n"
23 "Student Answer: Photosynthesis is how plants make their food using sunlight and carbon dioxide. It also gives off oxygen.\n"
24 "Mark Scheme: {'1':'Mentions use of sunlight', '2':'Mentions carbon dioxide and water', '3':'Mentions production of oxygen', '4':'Explains synthesis of nutrients or food', '5':'Mentions chlorophyll or green pigment'}"
25)
26
27user = [
28 {"role":"system", "content": "You are a grading assistant. Evaluate student answers based on the mark scheme. Respond only in JSON format with keys 'score' (int) and 'rationale' (string)."},
29 {"role":"user", "content": user_content},
30 ]
31
32inputs = tokenizer.apply_chat_template(user, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to(model.device)
33generated_ids = model.generate(**inputs, max_new_tokens=128, temperature=0.2, top_k=5, do_sample=False)[0]
34new_generated_ids = generated_ids[inputs["input_ids"].shape[1]:]
35generated_text = tokenizer.decode(new_generated_ids, skip_special_tokens=True)
36
37print(generated_text){"score": 5, "rationale": "Your answer is correct. You have accurately described the process of photosynthesis, mentioning the use of sunlight, carbon dioxide, and water, and the production of food and oxygen as by-products. Keep up the good work!"}| Step | Training Loss | Validation Loss |
|---|---|---|
| 10 | 3.211500 | 3.098564 |
| 20 | 3.247900 | 3.055659 |
| 30 | 3.115500 | 2.974921 |
| 40 | 3.028900 | 2.855265 |
| 50 | 2.839600 | 2.685857 |
| 60 | 2.640400 | 2.468288 |
| 70 | 2.391700 | 2.217547 |