1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3# Define the model path
4model_path = "/your/path/to/USP"
5
6# Initialize tokenizer with specific configurations
7tokenizer = AutoTokenizer.from_pretrained(model_path, padding_side="left", trust_remote_code=True)
8tokenizer.pad_token = tokenizer.eos_token
9
10# Define model configuration
11model_config = {
12 "pretrained_model_name_or_path": model_path,
13 "trust_remote_code": True,
14 "torch_dtype": torch.bfloat16,
15 "device_map": "cuda",
16 "attn_implementation": "flash_attention_2"
17}
18
19# Load the pre-trained model with specified configurations
20model = AutoModelForCausalLM.from_pretrained(**model_config)
21model.eval()
22
23# Define conversation messages
24user_profile = """
25You have a knack for planning exciting adventures, particularly when it comes to exploring new destinations on a budget. Your current focus is on organizing a cost-effective vacation to a warm, beach-filled location. You're actively seeking recommendations for such a getaway, with a particular interest in places like Phuket, Thailand. You're keen on discovering the must-visit spots in Phuket without breaking the bank, and you're looking for advice on how to make the most of your trip within your budget constraints. Your love for travel is evident in your habit of meticulously planning vacations in advance, ensuring you maximize both the experience and the value for money.
26
27Your personality shines through in your conscientious approach to planning, where every detail is considered and nothing is left to chance. You're open-minded and adventurous, always eager to dive into new experiences and embrace what each destination has to offer. Your inquisitive nature means you're always asking questions, seeking out the best advice to enhance your journeys. You communicate with an informal and friendly style, making it easy for others to share their knowledge and insights with you. This combination of traits makes you not only a savvy traveler but also a delightful companion on any adventure.
28""".strip()
29messages = [
30 [
31 {"role": "system", "content": f"You are engaging in a conversation with an AI assistant. Your profile is:
32{user_profile}
33 You can say anything you want, either based on the profile or something brand new.
34
35"},
36 ],
37 [
38 {"role": "system", "content": f"You are engaging in a conversation with an AI assistant. Your profile is:
39{user_profile}
40 You can say anything you want, either based on the profile or something brand new.
41
42"},
43 {"role": "user", "content": "I want to go on vacation to a warm place. Do you have any recommendations?"},
44 {"role": "assistant", "content": "Sure! If you like beaches, Maldives or Bali are great options. If you're into culture, consider Tuscany in Italy or Santorini in Greece. Which type of destination do you prefer?"},
45 ]
46
47]
48
49
50def generated_user_utt(msgs, model, tokenizer):
51 with torch.no_grad():
52 input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
53 inputs = tokenizer.batch_encode_plus(input_text, return_tensors="pt", truncation=True, padding=True, max_length=4096).to(model.device)
54 input_ids = inputs["input_ids"]
55 outputs = model.generate(
56 **inputs,
57 pad_token_id=tokenizer.eos_token_id,
58 eos_token_id=tokenizer.eos_token_id,
59 max_new_tokens=4096,
60 repetition_penalty=1.2)
61 generated_ids = [
62 output[len(input_):]
63 for input_, output in zip(input_ids, outputs)
64 ]
65 response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
66 return response
67
68# Generate responses for the provided messages
69responses = generated_user_utt(messages, model, tokenizer)
70print(f"Generated response: {responses}")
71
72# >>> Expected Output:
73# >>> ['I am thinking about going away for vacation', 'How about phucket thailand']
74# >>> The first output initiates a topic based on the system-defined user profile.
75# >>> The second output extends the conversation based on the given profile and context to mimic the user's behavior.