You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
prompt_style = """Below is an instruction that describes a task, paired with an input that provides further context.
Write a response that appropriately completes the request.
Before answering, think carefully about the question and create a step-by-step chain of thought to ensure a logical and accurate response.
Instruction:
You are a legal expert with advanced knowledge in legal reasoning, case analysis, and interpretation of laws.
Please answer the following legal question.
train_prompt_style = """Below is an instruction that describes a task, paired with an input that provides further context.
Write a response that appropriately completes the request.
Before answering, think carefully about the question and create a step-by-step chain of thought to ensure a logical and accurate response.
Instruction:
You are a legal expert with advanced knowledge in legal reasoning, case analysis, and interpretation of laws.
Please answer the following legal question.
question = "A contract was signed between two parties, but one party claims they were under duress. What legal principles apply to determine the contract’s validity?"
这些步骤旨在及时解决病情,防止病情恶化和并发症的发生。<|end▁of▁sentence|>
Map: 100%|██████████| 500/500 [00:00<00:00, 20348.06 examples/s]
Below is an instruction that describes a task, paired with an input that provides further context.
Write a response that appropriately completes the request.
Before answering, think carefully about the question and create a step-by-step chain of thought to ensure a logical and accurate response.
Instruction:
You are a legal expert with advanced knowledge in legal reasoning, case analysis, and interpretation of laws.
Please answer the following legal question.
从中医的角度来看,你所描述的症状符合“蝼蛄疖”的病症。这种病症通常发生在头皮,表现为多处结节,溃破流脓,形成空洞,患处皮肤增厚且长期不愈合。湿热较重的夏季更容易导致这种病症的发展,特别是在免疫力较弱的儿童身上。建议结合中医的清热解毒、祛湿消肿的治疗方法进行处理,并配合专业的医疗建议进行详细诊断和治疗。<|end▁of▁sentence|>
Unsloth 2025.2.12 patched 32 layers with 32 QKV layers, 32 O layers and 32 MLP layers.
Traceback (most recent call last):
File "/home/wb/dev/Unsloth/test1.py", line 107, in
trainer = SFTTrainer(
^^^^^^^^^^^
File "/root/anaconda3/envs/unsloth_env/lib/python3.11/site-packages/unsloth/trainer.py", line 203, in new_init
original_init(self, *args, **kwargs)
File "/home/wb/dev/Unsloth/unsloth_compiled_cache/UnslothSFTTrainer.py", line 952, in init
model.for_training()
File "/root/anaconda3/envs/unsloth_env/lib/python3.11/site-packages/unsloth/models/llama.py", line 2737, in for_training
del model._unwrapped_old_generate
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/anaconda3/envs/unsloth_env/lib/python3.11/site-packages/torch/nn/modules/module.py", line 2043, in delattr
super().delattr(name)
AttributeError: 'PeftModelForCausalLM' object has no attribute '_unwrapped_old_generate'
ERROR conda.cli.main_run:execute(124): conda run python /home/wb/dev/Unsloth/test1.py failed. (See above for error)
Process finished with exit code 1
The text was updated successfully, but these errors were encountered:
wsl install ubuntu
conda install unsloth
conda create --name unsloth_env
python=3.11
pytorch-cuda=12.1
pytorch cudatoolkit xformers -c pytorch -c nvidia -c xformers
-y
conda activate unsloth_env
pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
pip install --no-deps trl peft accelerate bitsandbytes
Package Version
accelerate 1.4.0
aiohappyeyeballs 2.4.6
aiohttp 3.11.12
aiosignal 1.3.2
attrs 25.1.0
bitsandbytes 0.45.2
certifi 2025.1.31
charset-normalizer 3.4.1
cut-cross-entropy 25.1.1
datasets 3.3.1
dill 0.3.8
docstring_parser 0.16
filelock 3.13.1
frozenlist 1.5.0
fsspec 2024.12.0
gmpy2 2.2.1
hf_transfer 0.1.9
huggingface-hub 0.28.1
idna 3.10
Jinja2 3.1.5
markdown-it-py 3.0.0
MarkupSafe 3.0.2
mdurl 0.1.2
mpmath 1.3.0
multidict 6.1.0
multiprocess 0.70.16
networkx 3.4.2
numpy 2.2.3
packaging 24.2
pandas 2.2.3
peft 0.14.0
pillow 11.1.0
pip 25.0
propcache 0.2.1
protobuf 3.20.3
psutil 7.0.0
pyarrow 19.0.0
Pygments 2.19.1
python-dateutil 2.9.0.post0
pytz 2025.1
PyYAML 6.0.2
regex 2024.11.6
requests 2.32.3
rich 13.9.4
safetensors 0.5.2
sentencepiece 0.2.0
setuptools 75.8.0
shtab 1.7.1
six 1.17.0
sympy 1.13.1
tokenizers 0.21.0
torch 2.5.1
tqdm 4.67.1
transformers 4.49.0
triton 3.1.0
trl 0.15.0
typeguard 4.4.2
typing_extensions 4.12.2
tyro 0.9.14
tzdata 2025.1
unsloth 2025.2.12
unsloth_zoo 2025.2.5
urllib3 2.3.0
wheel 0.45.1
xformers 0.0.28.post3
xxhash 3.5.0
yarl 1.18.3
加载 Model 和 Tokenizer
from unsloth import FastLanguageModel
max_seq_length = 2048
dtype = None
load_in_4bit = True
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="./model/unsloth/deepseek-r1-distill-llama-8b-unsloth-bnb-4bit",
max_seq_length=max_seq_length,
dtype=dtype,
load_in_4bit=load_in_4bit,
# token=hf_token,
)
定义 Prompt 并运行推理
prompt_style = """Below is an instruction that describes a task, paired with an input that provides further context.
Write a response that appropriately completes the request.
Before answering, think carefully about the question and create a step-by-step chain of thought to ensure a logical and accurate response.
Instruction:
You are a legal expert with advanced knowledge in legal reasoning, case analysis, and interpretation of laws.
Please answer the following legal question.
Question:
{}
Response:
{}"""
question = "一个患有急性阑尾炎的病人已经发病5天,腹痛稍有减轻但仍然发热,在体检时发现右下腹有压痛的包块,此时应如何处理?"
FastLanguageModel.for_inference(model)
inputs = tokenizer([prompt_style.format(question, "")], return_tensors="pt").to("cuda")
outputs = model.generate(
input_ids=inputs.input_ids,
attention_mask=inputs.attention_mask,
max_new_tokens=1200,
use_cache=True,
)
response = tokenizer.batch_decode(outputs)
print(response[0].split("### Response:")[1])
更新提示模板
train_prompt_style = """Below is an instruction that describes a task, paired with an input that provides further context.
Write a response that appropriately completes the request.
Before answering, think carefully about the question and create a step-by-step chain of thought to ensure a logical and accurate response.
Instruction:
You are a legal expert with advanced knowledge in legal reasoning, case analysis, and interpretation of laws.
Please answer the following legal question.
Question:
{}
Response:
{} {}""" EOS_TOKEN = tokenizer.eos_token定义 formatting 函数
def formatting_prompts_func(examples):
inputs = examples["Question"]
cots = examples["Complex_CoT"]
outputs = examples["Response"]
texts = []
for input_text, cot, output_text in zip(inputs, cots, outputs):
text = train_prompt_style.format(input_text, cot, output_text) + EOS_TOKEN
texts.append(text)
return {"text": texts}
加载和映射数据集
from datasets import load_dataset
dataset = load_dataset("./datasets/FreedomIntelligence", "zh", split="train[0:500]", trust_remote_code=True)
dataset = dataset.map(formatting_prompts_func, batched=True)
print(dataset["text"][0])
####Note: This is a pseudo dataset. Please create or use an appropriate dataset for your own use case.
设置LoRA以进行微调
model = FastLanguageModel.get_peft_model(
model,
r=16,
target_modules=[
"q_proj",
"k_proj",
"v_proj",
"o_proj",
"gate_proj",
"up_proj",
"down_proj",
],
lora_alpha=16,
lora_dropout=0,
bias="none",
use_gradient_checkpointing="unsloth",
random_state=3407,
use_rslora=False,
loftq_config=None,
)
配置和运行训练过程
from trl import SFTTrainer
from transformers import TrainingArguments
from unsloth import is_bfloat16_supported
trainer = SFTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=dataset,
dataset_text_field="text",
max_seq_length=max_seq_length,
dataset_num_proc=2,
args=TrainingArguments(
per_device_train_batch_size=2,
gradient_accumulation_steps=4,
warmup_steps=5,
max_steps=60,
learning_rate=2e-4,
fp16=not is_bfloat16_supported(),
bf16=is_bfloat16_supported(),
logging_steps=10,
optim="adamw_8bit",
weight_decay=0.01,
lr_scheduler_type="linear",
seed=3407,
output_dir="outputs",
),
)
开始训练
trainer_stats = trainer.train()
微调后的推理
question = "A contract was signed between two parties, but one party claims they were under duress. What legal principles apply to determine the contract’s validity?"
FastLanguageModel.for_inference(model)
inputs = tokenizer([prompt_style.format(question, "")], return_tensors="pt").to("cuda")
outputs = model.generate(
input_ids=inputs.input_ids,
attention_mask=inputs.attention_mask,
max_new_tokens=1200,
use_cache=True,
)
response = tokenizer.batch_decode(outputs)
print(response[0].split("### Response:")[1])
本地保存
new_model_local = "DeepSeek-R1-Legal-COT"
model.save_pretrained(new_model_local)
tokenizer.save_pretrained(new_model_local)
model.save_pretrained_merged(new_model_local, tokenizer, save_method="merged_16bit")
/root/anaconda3/bin/conda run -n unsloth_env --no-capture-output python /home/wb/dev/Unsloth/test1.py
首先,我需要分析患者的症状和体检结果。患者患有急性阑尾炎,已经发病5天,腹痛有所缓解但仍发热。在体检时发现右下腹有压痛的包块,这可能提示急性阑尾炎的病情发展。🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.
🦥 Unsloth Zoo will now patch everything to make training faster!
==((====))== Unsloth 2025.2.12: Fast Llama patching. Transformers: 4.49.0.
\ /| GPU: NVIDIA GeForce RTX 4060 Laptop GPU. Max memory: 7.996 GB. Platform: Linux.
O^O/ _/ \ Torch: 2.5.1. CUDA: 8.9. CUDA Toolkit: 12.1. Triton: 3.1.0
\ / Bfloat16 = TRUE. FA [Xformers = 0.0.28.post3. FA2 = False]
"-____-" Free Apache license: http://github.com/unslothai/unsloth
Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!
接下来,考虑到包块的存在,可能有扭转或者阻塞的情况,这可能导致急性阑尾炎的病情加重,甚至引发急性胰腺炎或其他并发症。因此,需要及时处理。
处理措施包括:
综上所述,正确的处理步骤是立即进行急性阑尾炎手术,并配合抗生素治疗和补液措施,以防止病情恶化。
在面对急性阑尾炎病情加重的情况时,正确的处理措施应包括:
这些步骤旨在及时解决病情,防止病情恶化和并发症的发生。<|end▁of▁sentence|>
Map: 100%|██████████| 500/500 [00:00<00:00, 20348.06 examples/s]
Below is an instruction that describes a task, paired with an input that provides further context.
Write a response that appropriately completes the request.
Before answering, think carefully about the question and create a step-by-step chain of thought to ensure a logical and accurate response.
Instruction:
You are a legal expert with advanced knowledge in legal reasoning, case analysis, and interpretation of laws.
Please answer the following legal question.
Question:
根据描述,一个1岁的孩子在夏季头皮出现多处小结节,长期不愈合,且现在疮大如梅,溃破流脓,口不收敛,头皮下有空洞,患处皮肤增厚。这种病症在中医中诊断为什么病?
Response:
这个小孩子在夏天头皮上长了些小结节,一直都没好,后来变成了脓包,流了好多脓。想想夏天那么热,可能和湿热有关。才一岁的小孩,免疫力本来就不强,夏天的湿热没准就侵袭了身体。用中医的角度来看,出现小结节、再加上长期不愈合,这些症状让我想到了头疮。小孩子最容易得这些皮肤病,主要因为湿热在体表郁结。
但再看看,头皮下还有空洞,这可能不止是简单的头疮。看起来病情挺严重的,也许是脓肿没治好。这样的情况中医中有时候叫做禿疮或者湿疮,也可能是另一种情况。
等一下,头皮上的空洞和皮肤增厚更像是疾病已经深入到头皮下,这是不是说明有可能是流注或瘰疬?这些名字常描述头部或颈部的严重感染,特别是有化脓不愈合,又形成通道或空洞的情况。
仔细想想,我怎么感觉这些症状更贴近瘰疬的表现?尤其考虑到孩子的年纪和夏天发生的季节性因素,湿热可能是主因,但可能也有火毒或者痰湿造成的滞留。
回到基本的症状描述上看,这种长期不愈合又复杂的状况,如果结合中医更偏重的病名,是不是有可能是涉及更深层次的感染?
再考虑一下,这应该不是单纯的瘰疬,得仔细分析头皮增厚并出现空洞这样的严重症状。中医里头,这样的表现可能更符合‘蚀疮’或‘头疽’。这些病名通常描述头部严重感染后的溃烂和组织坏死。
看看季节和孩子的体质,夏天又湿又热,外邪很容易侵入头部,对孩子这么弱的免疫系统简直就是挑战。头疽这个病名听起来真是切合,因为它描述的感染严重,溃烂到出现空洞。
不过,仔细琢磨后发现,还有个病名似乎更为合适,叫做‘蝼蛄疖’,这病在中医里专指像这种严重感染并伴有深部空洞的情况。它也涵盖了化脓和皮肤增厚这些症状。
哦,该不会是夏季湿热,导致湿毒入侵,孩子的体质不能御,其病情发展成这样的感染?综合分析后我觉得‘蝼蛄疖’这个病名真是相当符合。
从中医的角度来看,你所描述的症状符合“蝼蛄疖”的病症。这种病症通常发生在头皮,表现为多处结节,溃破流脓,形成空洞,患处皮肤增厚且长期不愈合。湿热较重的夏季更容易导致这种病症的发展,特别是在免疫力较弱的儿童身上。建议结合中医的清热解毒、祛湿消肿的治疗方法进行处理,并配合专业的医疗建议进行详细诊断和治疗。<|end▁of▁sentence|>
Unsloth 2025.2.12 patched 32 layers with 32 QKV layers, 32 O layers and 32 MLP layers.
Traceback (most recent call last):
File "/home/wb/dev/Unsloth/test1.py", line 107, in
trainer = SFTTrainer(
^^^^^^^^^^^
File "/root/anaconda3/envs/unsloth_env/lib/python3.11/site-packages/unsloth/trainer.py", line 203, in new_init
original_init(self, *args, **kwargs)
File "/home/wb/dev/Unsloth/unsloth_compiled_cache/UnslothSFTTrainer.py", line 952, in init
model.for_training()
File "/root/anaconda3/envs/unsloth_env/lib/python3.11/site-packages/unsloth/models/llama.py", line 2737, in for_training
del model._unwrapped_old_generate
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/anaconda3/envs/unsloth_env/lib/python3.11/site-packages/torch/nn/modules/module.py", line 2043, in delattr
super().delattr(name)
AttributeError: 'PeftModelForCausalLM' object has no attribute '_unwrapped_old_generate'
ERROR conda.cli.main_run:execute(124):
conda run python /home/wb/dev/Unsloth/test1.py
failed. (See above for error)Process finished with exit code 1
The text was updated successfully, but these errors were encountered: