Uses a pure self-play approach: the rejected samples are outputs from an intermediate checkpoint of the previous training stage, teaching the model to consistently surpass its own prior output.
1from unsloth import FastLanguageModel
2from peft import PeftModel
3
4base_model, proc = FastLanguageModel.from_pretrained(
5 "unsloth/Qwen3.5-9B", max_seq_length=2048, load_in_4bit=False,
6)
7tokenizer = proc.tokenizer if hasattr(proc, "tokenizer") else proc
8
9model = PeftModel.from_pretrained(
10 base_model, "XiangJinYu/Qwen3.5-9B-Humanize-DPO-Round2", is_trainable=False,
11)
12if hasattr(model, "config") and getattr(model.config, "model_type", "") == "qwen3_5":
13 model.config.model_type = "qwen3"
14FastLanguageModel.for_inference(model)
15
16instruction = "请将下面文本改写得更像自然人写作,保持原意与事实,不要加标题或说明。"
17text = "本文提出了一种基于U-Net改进的医学影像分割方法,Dice系数达到0.923,较基线方法提升了4.7个百分点,推理速度提升约30%。"
18messages = [{"role": "user", "content": [{"type": "text", "text": f"{instruction}\n\n原文:{text}"}]}]
19prompt = tokenizer.apply_chat_template(
20 messages, tokenize=False, add_generation_prompt=True, enable_thinking=False
21)
22inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
23# Recommended: temperature 0.60-0.65 for academic texts
24outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.65,
25 top_p=0.9, do_sample=True, repetition_penalty=1.1)
26gen = outputs[0][inputs["input_ids"].shape[1]:]
27print(tokenizer.decode(gen, skip_special_tokens=True))