合并以后跑出来的是unk
ScottishFold007 opened this issue · 1 comments
import re
import torch
from peft import PeftModel
from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer
ziya_model_path = "/Ziya-LLaMA-13B-v1/" # 完整的子牙模型权重路径
chatlaw_model_path = 'JessyTsu1_ChatLaw-13B_adapter'# chatlaw模型权重
'''
tokenizer = LlamaTokenizer.from_pretrained(ziya_model_path)
model = LlamaForCausalLM.from_pretrained(
ziya_model_path,
torch_dtype=torch.float16,
device_map="auto",
)
'''
model = PeftModel.from_pretrained(model, chatlaw_model_path)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.unk_token
#model.half()
model.eval()
生成:
def law_answer(query):
gen_conf={
"bos_token_id": tokenizer.bos_token_id,
"do_sample": True,
#"num_beams": 4,
"eos_token_id": tokenizer.eos_token_id,
"max_new_tokens": 256,
"pad_token_id": 0,
"penalty_alpha": 0.3,
"repetition_penalty": 1.0,
"temperature": 0.65,
"top_k": 15,
"top_p": 0.85,
}
gen_conf= {
"max_new_tokens": 512,
"temperature": 0.1,
"top_p": 0.75,
"top_k": 40,
#"num_beams": 4
}
meta_instruction= "你一个名叫ChatLAW,由北京大学团队开发的人工智能助理:\n- 你旨在提供有无害且准确的回答。\n- 你必须拒绝回答非法的问题。\n- 你的回应不能含糊、指责、粗鲁、有争议、离题或防御性。\n- 你的回应必须有礼貌。"
#prompt = f"{meta_instruction}\n咨询者:\n{query}\nChatLAW:\n"
#prompt = f"Consult:\n{query}\nResponse:\n"
prompt = f"{meta_instruction}\nConsult:\n{query}\nResponse:\n"
#print(prompt)
with torch.no_grad():
inputs=tokenizer(query,return_tensors='pt')
inputs=inputs.to(model.device)
output=model.generate(**inputs,**gen_conf)
decoded_text=tokenizer.decode(output[0])
decoded_text= decoded_text.replace("<s>","").replace("</s>","")
return decoded_text[len(query)+1:]
生成结果:
query= "公司无故辞退摸鱼员工,是否触犯法律?"
law_answer(query)
<unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk><unk>
meta_instruction= "你一个名叫ChatLAW,由北京大学团队开发的人工智能助理:\n- 你旨在提供有无害且准确的回答。\n- 你必须拒绝回答非法的问题。\n- 你的回应不能含糊、指责、粗鲁、有争议、离题或防御性。\n- 你的回应必须有礼貌。"
query = "公司无故辞退摸鱼员工,是否触犯法律?"
prompt = f"Consult:\n{consult}\nResponse:\n"
prompt = f"{meta_instruction}\nConsult:\n{query}\nResponse:\n"
inputs = tokenizer(prompt, return_tensors="pt")
inputs['input_ids'] = inputs['input_ids'].to(model.device)
with torch.no_grad():
generation_output = model.generate(
inputs,
generation_config=generation_config,
return_dict_in_generate=True,
output_scores=True,
max_new_tokens=2048,
repetition_penalty=1.2,
)
s = generation_output.sequences[0]
output = tokenizer.decode(s)
if search_result := re.search("Response\s:\s([\s\S]+?)", output):
output = search_result.group(1)
print(output)
根据《中华人民共和国劳动合同法》规定,用人单位应当依法与劳动者订立书面劳动合同,并按照合同约定支付劳动报酬。如果用人单位无故解除劳动合同,应当依法向劳动者支付经济补偿金。因此,如果公司无故辞退摸鱼员工,可能会触犯法律。建议您咨询专业律师以获取更详细的法律建议
我这边倒是没问题,但在司法考试测试集上感觉并不强