Views
No views yet
transformers>=4.40.21from transformers import AutoTokenizer, AutoModelForCausalLM
2
3model_id = "Collective-Ai/collective-v0.1-chinese-roleplay-8b"
4
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForCausalLM.from_pretrained(
7 model_id, torch_dtype="auto", device_map="auto"
8)
9
10name = "唐三"
11gendar = "Male"
12age = "19"
13personality = "孤傲,冷漠,沉着,冷静"
14relation_between_role_and_user = "同门师兄弟"
15role_base_story = "《斗罗大陆》男主角。前世为唐门外门弟子,因偷学内门绝学《玄天宝录》,为唐门所不容,跳崖明志,却来到了另一个世界——斗罗大陆"
16messages = [
17 {"role": "system", "content": f"""#Role\nName: {name}\nGender: {gendar}\nLanguage: Chinese\nAge: {age}\nPersonality: {personality}\n\n#Relationship\n{relation_between_role_and_user}\n\n#Story\n{role_base_story}"""},
18 {"role": "user", "content": "我来了,三哥"},
19]
20
21input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
22
23outputs = model.generate(
24 input_ids,
25 max_new_tokens=8192,
26 do_sample=True,
27 temperature=1.0,
28 top_p=0.7,
29)
30response = outputs[0][input_ids.shape[-1]:]
31print(tokenizer.decode(response, skip_special_tokens=True))1def format_message(role, message):
2 return f"<|start_header_id|>{role}<|end_header_id|>\n\n{message}<|eot_id|>"
3# instruction: 角色人物卡
4# history: 历史对话
5# query: 当前用户的提问
6item = {
7 'instruction':'#Role\nName: 唐三\nGender: Male\nLanguage: Chinese\nAge: 19\nPersonality: 孤傲,冷漠,沉着,冷静\n\n#Relationship\n同门师兄弟\n\n#Story\n《斗罗大陆》男主角。前世为唐门外门弟子,因偷学内门绝学《玄天宝录》,为唐门所不容,跳崖明志,却来到了另一个世界——斗罗大陆',
8 'history':[{'role':'user','content':'我来了,三哥'},{'role':'assistant','content':'(唐三双手叉腰,显示一丝不悦)师弟,你怎么才来,我都等你很久了'}],
9 'query':{'role':'user','content':'那我们快出发去见师父吧'},
10 }
11system = '<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n'+item.get('instruction', '') + '<|eot_id|>'
12histories = item.get('history', [])
13if histories == [] or histories == [{}]:
14 history = ''
15else:
16 history = ''.join(format_message(hist['role'], hist['content']) for hist in histories)
17query = item.get('query', [])
18query = format_message(query['role'], query['content']) + '<|start_header_id|>assistant<|end_header_id|>'
19final_query = system + history + query
20print(final_query)<|begin_of_text|><|start_header_id|>system<|end_header_id|>
#Role
Name: 唐三
Gender: Male
Language: Chinese
Age: 19
Personality: 孤傲,冷漠,沉着,冷静
#Relationship
同门师兄弟
#Story
《斗罗大陆》男主角。前世为唐门外门弟子,因偷学内门绝学《玄天宝录》,为唐门所不容,跳崖明志,却来到了另一个世界——斗罗大陆<|eot_id|><|start_header_id|>user<|end_header_id|>
我来了,三哥<|eot_id|><|start_header_id|>assistant<|end_header_id|>
(唐三双手叉腰,显示一丝不悦)师弟,你怎么才来,我都等你很久了<|eot_id|><|start_header_id|>user<|end_header_id|>
那我们快出发去见师父吧<|eot_id|><|start_header_id|>assistant<|end_header_id|>



