Views
No views yet

Transformers:pip install transformers1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_name = "Inceptive/ROLEPL-AI-v2-Qwen2.5-7B"
4
5# Load the tokenizer and the model
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(
8 model_name,
9 torch_dtype="auto",
10 device_map="auto"
11)
12
13# Prepare the system prompt
14system_prompt = "You are a professional writer, writing educational content for students in tourism management." # You need to introduce the AI as an author/writer on whatever topic you wish to roleplay on (professional/fantasy setting etc.)
15
16# Prepare the role-play setting and append the history.
17# You may put your custom role-playing instructions and setting here as long as the general structure is the same.
18# Always make sure to keep the "Write the next answer from [CHARACTER]" instruction, and to include the whole conversation history within the <user> part of the prompt.
19def do_prompt(message_history):
20 return f"""<|im_start|>system
21{system_prompt}
22<|im_end|>
23<|im_start|>user
24[Dialogue completion]
25
26Mia Nielsen, an electrical engineer at WindTech Innovators, is attending the Career Connect Expo job fair as an exhibitor. She’s setting up the booth with her colleagues and preparing materials for the first visitors.
27
28Just as Mia is about to print more brochures, her portable printer won’t connect to her laptop. She checks the cables, but everything seems fine. The fair is about to start, and she doesn’t have time to troubleshoot. Annoyed, she asks a fair assistant for help.
29
30Write the next answer from Mia Nielsen. You may end your answer with one of the following emoticons based on the emotion of the character: (😠🤢😨😄😢😲).
31
32{"\n".join(message_history)}
33<|im_end|>
34<|im_start|>assistant
35"""
36user_input = ""
37user_name = "Lucas Wright"
38assistant_answer = "[Mia Nielsen]: Excuse me, do you have a moment?"
39history = [assistant_answer]
40
41while True:
42 print(assistant_answer)
43 user_input = input(f"[{user_name}]: ")
44 if user_input in ("quit", "exit"):
45 break
46 history.append(f"[{user_name}]: {user_input}")
47 final_input = do_prompt(history)
48 input_ids = tokenizer(final_input, return_tensors="pt")
49 input_ids.to("cuda")
50 outputs = model.generate(**input_ids, max_new_tokens=256)
51 out = tokenizer.decode(*outputs)
52 assistant_answer = out.split("<|im_start|>assistant")[1].split("<|im_end|>")[0].strip()
53 history.append(assistant_answer)