Recently I've been interested in LLMs and wanted to train my own from scratch using the Qwen2 architecture provided through the Hugging Face transformers library. This was created locally on my personal laptop and is not powerful enough to be useful in any way, but it can respond to simple queries. I would recommend using a better-trained lightweight model instead of this one, as I've observed that although explicit in your queries, it often hallucinates data such as fictional U.S. Presidents or starts ranting about Chicago when told "Hey". The only advantage I can point out is its small size, weighing in at only 203 MB.
A 28.4 GB subset of the
AllenAI C4 English dataset was used for pre-training as well as for generating the tokenizer. However, the model was only trained up to an epoch of 0.77 (77% complete) because the loss was very stable at 3.5, and I didn't see any reason to continue training. Pre-training took about 18.5 hours with the GPU overclocked to its maximum capacity. Post-training involved 6 epochs of
databricks/databricks-dolly-15k formatted in ChatML with 50 random possible system prompts.
Here below I created a simple python script you can use. The model should be usable directly through the transformers library but you can change the model path to point to a directory containing the model too.
1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3model_path = "TheOneWhoWill/makeshift-qwen2"
4tokenizer = AutoTokenizer.from_pretrained(model_path)
5model = AutoModelForCausalLM.from_pretrained(
6 model_path,
7 torch_dtype="auto",
8 device_map="auto"
9)
10
11from transformers import pipeline
12
13pipe = pipeline(
14 "text-generation",
15 model=model,
16 tokenizer=tokenizer
17)
18
19messages = [
20 {"role": "system", "content": "You are a helpful AI assistant. Always provide clear, accurate, and concise answers."}
21]
22
23while True:
24 user_input = input("User: ")
25 if user_input.lower() in ["exit", "quit"]:
26 print("Exiting the chat.")
27 break
28 messages.append({"role": "user", "content": user_input})
29 # Generate and print
30 response = pipe(
31 messages,
32 max_new_tokens=256,
33 do_sample=True,
34 temperature=0.7,
35 top_k=50,
36 top_p=0.95
37 )
38 response = response[0]['generated_text'][-1]["content"]
39 messages.append({"role": "assistant", "content": response})
40 print("Assistant:", response)