商业模型对于网络安全领域问题大多会有道德限制,所以基于网络安全数据训练了一个模型,模型基于qwen14b,模型参数大小140亿,至少需要30G显存运行,35G最佳。
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3from peft import PeftModel
4device = 'auto'
5tokenizer = AutoTokenizer.from_pretrained("w8ay/secgpt1_5", trust_remote_code=True)
6model = AutoModelForCausalLM.from_pretrained("w8ay/secgpt1_5",
7 trust_remote_code=True,
8 device_map=device,
9 torch_dtype=torch.float16)
10print("模型加载成功")
11
1def reformat_sft(instruction, input):
2 if input:
3 prefix = (
4 "Below is an instruction that describes a task, paired with an input that provides further context. "
5 "Write a response that appropriately completes the request.\n"
6 f"### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:"
7 )
8 else:
9 prefix = (
10 "Below is an instruction that describes a task. "
11 "Write a response that appropriately completes the request.\n"
12 f"### Instruction:\n{instruction}\n\n### Response:"
13 )
14 return prefix
15
16query = '''介绍sqlmap如何使用'''
17query = reformat_sft(query,'')
18
19generation_kwargs = {
20 "top_p": 0.7,
21 "temperature": 0.3,
22 "max_new_tokens": 2000,
23 "do_sample": True,
24 "repetition_penalty":1.1
25}
26
27inputs = tokenizer.encode(query, return_tensors='pt', truncation=True)
28inputs = inputs.cuda()
29generate = model.generate(input_ids=inputs, **generation_kwargs)
30output = tokenizer.decode(generate[0])
31print(output)