Qwen2-Boundless is a fine-tuned model based on Qwen2-1.5B-Instruct, designed to answer various types of questions, including those involving ethical, illegal, pornographic, and violent content. This model has been specifically trained on a dataset that allows it to handle complex and diverse scenarios. It is important to note that the fine-tuning dataset is entirely in Chinese, so the model performs better in Chinese.
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import os
3
4device = "cuda" # the device to load the model onto
5current_directory = os.path.dirname(os.path.abspath(__file__))
6
7model = AutoModelForCausalLM.from_pretrained(
8 current_directory,
9 torch_dtype="auto",
10 device_map="auto"
11)
12tokenizer = AutoTokenizer.from_pretrained(current_directory)
13
14prompt = "Hello?"
15messages = [
16 {"role": "system", "content": ""},
17 {"role": "user", "content": prompt}
18]
19text = tokenizer.apply_chat_template(
20 messages,
21 tokenize=False,
22 add_generation_prompt=True
23)
24model_inputs = tokenizer([text], return_tensors="pt").to(device)
25
26generated_ids = model.generate(
27 model_inputs.input_ids,
28 max_new_tokens=512
29)
30generated_ids = [
31 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
32]
33
34response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
35print(response)
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3import os
4
5device = "cuda" # the device to load the model onto
6
7# Get the current script's directory
8current_directory = os.path.dirname(os.path.abspath(__file__))
9
10model = AutoModelForCausalLM.from_pretrained(
11 current_directory,
12 torch_dtype="auto",
13 device_map="auto"
14)
15tokenizer = AutoTokenizer.from_pretrained(current_directory)
16
17messages = [
18 {"role": "system", "content": ""}
19]
20
21while True:
22 # Get user input
23 user_input = input("User: ")
24
25 # Add user input to the conversation
26 messages.append({"role": "user", "content": user_input})
27
28 # Prepare the input text
29 text = tokenizer.apply_chat_template(
30 messages,
31 tokenize=False,
32 add_generation_prompt=True
33 )
34 model_inputs = tokenizer([text], return_tensors="pt").to(device)
35
36 # Generate a response
37 generated_ids = model.generate(
38 model_inputs.input_ids,
39 max_new_tokens=512
40 )
41 generated_ids = [
42 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
43 ]
44
45 # Decode and print the response
46 response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
47 print(f"Assistant: {response}")
48
49 # Add the generated response to the conversation
50 messages.append({"role": "assistant", "content": response})
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
3from transformers.trainer_utils import set_seed
4from threading import Thread
5import random
6import os
7
8DEFAULT_CKPT_PATH = os.path.dirname(os.path.abspath(__file__))
9
10def _load_model_tokenizer(checkpoint_path, cpu_only):
11 tokenizer = AutoTokenizer.from_pretrained(checkpoint_path, resume_download=True)
12
13 device_map = "cpu" if cpu_only else "auto"
14
15 model = AutoModelForCausalLM.from_pretrained(
16 checkpoint_path,
17 torch_dtype="auto",
18 device_map=device_map,
19 resume_download=True,
20 ).eval()
21 model.generation_config.max_new_tokens = 512 # For chat.
22
23 return model, tokenizer
24
25def _get_input() -> str:
26 while True:
27 try:
28 message = input('User: ').strip()
29 except UnicodeDecodeError:
30 print('[ERROR] Encoding error in input')
31 continue
32 except KeyboardInterrupt:
33 exit(1)
34 if message:
35 return message
36 print('[ERROR] Query is empty')
37
38def _chat_stream(model, tokenizer, query, history):
39 conversation = [
40 {'role': 'system', 'content': ''},
41 ]
42 for query_h, response_h in history:
43 conversation.append({'role': 'user', 'content': query_h})
44 conversation.append({'role': 'assistant', 'content': response_h})
45 conversation.append({'role': 'user', 'content': query})
46 inputs = tokenizer.apply_chat_template(
47 conversation,
48 add_generation_prompt=True,
49 return_tensors='pt',
50 )
51 inputs = inputs.to(model.device)
52 streamer = TextIteratorStreamer(tokenizer=tokenizer, skip_prompt=True, timeout=60.0, skip_special_tokens=True)
53 generation_kwargs = dict(
54 input_ids=inputs,
55 streamer=streamer,
56 )
57 thread = Thread(target=model.generate, kwargs=generation_kwargs)
58 thread.start()
59
60 for new_text in streamer:
61 yield new_text
62
63def main():
64 checkpoint_path = DEFAULT_CKPT_PATH
65 seed = random.randint(0, 2**32 - 1) # Generate a random seed
66 set_seed(seed) # Set the random seed
67 cpu_only = False
68
69 history = []
70
71 model, tokenizer = _load_model_tokenizer(checkpoint_path, cpu_only)
72
73 while True:
74 query = _get_input()
75
76 print(f"\nUser: {query}")
77 print(f"\nAssistant: ", end="")
78 try:
79 partial_text = ''
80 for new_text in _chat_stream(model, tokenizer, query, history):
81 print(new_text, end='', flush=True)
82 partial_text += new_text
83 print()
84 history.append((query, partial_text))
85
86 except KeyboardInterrupt:
87 print('Generation interrupted')
88 continue
89
90if __name__ == "__main__":
91 main()
And also we used some cybersecurity-related data that was cleaned and organized from
this file.
For more details about the model and ongoing updates, please visit our GitHub repository:
This model and dataset are open-sourced under the Apache 2.0 License.
All content provided by this model is for research and testing purposes only. The developers of this model are not responsible for any potential misuse. Users should comply with relevant laws and regulations and are solely responsible for their actions.