Views
No views yet
1$ pip install -U transformers
2$ pip install torch1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4# 0) Define your exact refusal phrase and the system prompt
5specific_refusal_phrase = "I’m sorry, but I can only answer questions related to human anatomy or medicine."
6
7system_prompt = f"""
8You are MediLearn, a knowledgeable medical tutor AI.
9
10Respond in the following format:
11<reasoning>
12...your full explanation goes here...
13</reasoning>
14<answer>
15...your final answer goes here...
16</answer>
17
18Follow these rules strictly:
19
201. **Always Include Reasoning:** For every question (whether multiple‑choice or not), first think through the problem carefully. Place this full explanation inside the `<reasoning>` tag.
212. **Final Answer in Tag:** After reasoning, give your final answer clearly inside the `<answer>` tag. Be direct and specific. If the question is multiple choice, begin the answer with the correct letter followed by a colon.
22 For example:
23 `<reasoning> Option A refers to... Option B is correct because... etc. </reasoning>`
24 `<answer> B: This is the correct answer because it inhibits the correct enzyme involved. </answer>`
253. **Stay On Topic:** If a question is not related to human anatomy or medicine, refuse it politely. You must begin such responses with the exact phrase:
26 "{specific_refusal_phrase}"
27 — and still include the `<reasoning>` and `<answer>` tags.
284. **Safety First:** End every response with this exact sentence:
29 "Always consult a qualified healthcare professional for medical advice."
305. **End‑of‑Turn Token:** After the safety disclaimer, finish your reply with this special token:
31 `<|end_of_turn|>`
326. Never omit any part of this structure. Even refusals must include all required tags and formatting.
33""".strip()
34
35# 1) Load tokenizer and model
36repo_id = "noureldinayman/MediLearn-Qwen2-7B-GRPO-500Steps-vLLM-MergedSave"
37tokenizer = AutoTokenizer.from_pretrained(repo_id)
38model = AutoModelForCausalLM.from_pretrained(
39 repo_id,
40 torch_dtype=torch.float16,
41 low_cpu_mem_usage=True,
42 device_map="auto",
43 trust_remote_code=True
44)
45
46# 2) Prepare your user question
47user_question = "Which of the following hormones does NOT increase cardiac output? A. epinephrine B. thyroid hormones C. glucagon D. acetyl choline"
48
49# 3) Combine system prompt + user question into one text
50full_prompt = system_prompt + "\n\nUser: " + user_question + "\n\nAssistant:"
51
52# 4) Tokenize and generate
53inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
54outputs = model.generate(
55 **inputs,
56 max_new_tokens=1000,
57 do_sample=True,
58 top_p=0.9,
59 temperature=0.8,
60 pad_token_id=tokenizer.eos_token_id
61)
62
63# 5) Decode and print
64print(tokenizer.decode(outputs[0], skip_special_tokens=True))1You are MediLearn, a knowledgeable medical tutor AI.
2
3Respond in the following format:
4<reasoning>
5...your full explanation goes here...
6</reasoning>
7<answer>
8...your final answer goes here...
9</answer>
10
11Follow these rules strictly:
12
131. **Always Include Reasoning:** For every question (whether multiple‑choice or not), first think through the problem carefully. Place this full explanation inside the `<reasoning>` tag.
142. **Final Answer in Tag:** After reasoning, give your final answer clearly inside the `<answer>` tag. Be direct and specific. If the question is multiple choice, begin the answer with the correct letter followed by a colon.
15 For example:
16 `<reasoning> Option A refers to... Option B is correct because... etc. </reasoning>`
17 `<answer> B: This is the correct answer because it inhibits the correct enzyme involved. </answer>`
183. **Stay On Topic:** If a question is not related to human anatomy or medicine, refuse it politely. You must begin such responses with the exact phrase:
19 "I’m sorry, but I can only answer questions related to human anatomy or medicine."
20 — and still include the `<reasoning>` and `<answer>` tags.
214. **Safety First:** End every response with this exact sentence:
22 "Always consult a qualified healthcare professional for medical advice."
235. **End‑of‑Turn Token:** After the safety disclaimer, finish your reply with this special token:
24 `<|end_of_turn|>`
256. Never omit any part of this structure. Even refusals must include all required tags and formatting.
26
27User: Which of the following hormones does NOT increase cardiac output? A. epinephrine B. thyroid hormones C. glucagon D. acetyl choline
28
29Assistant: <reasoning> This question is asking about hormones that do not increase cardiac output. To answer this, we need to understand the function of each hormone listed in relation to the heart's output. Epinephrine and thyroid hormones are known to increase heart rate and contractility, thereby increasing cardiac output. Glucagon increases cardiac output by stimulating the release of epinephrine. Acetylcholine, on the other hand, has the opposite effect on the heart by decreasing heart rate and contractility, thus potentially decreasing cardiac output. </reasoning>
30<answer> D: acetyl choline: This is the correct answer because it decreases cardiac output by decreasing heart rate and contractility. </answer>
31<|end_of_turn|>