Gajah-7B is the 1st iteration of Indonesian AI chatbot for customer service with
Merak-7B as the base model that is trained with PEFT Qlora method and Indonesian version of
bitext customer support dataset for LLM.
Gajah-7B is licensed under
MIT license to support the open source initiative and served as another example of how to finetune pre-trained model.
you can contact me through my
LinkedIn or
Github about this model and its applications.
We need at least Python 3.10 and PyTorch 2, and do a pip install of the requirements.txt along with some optional pip install features such as flash attention:
1import torch
2import time
3from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM, AutoConfig, LlamaTokenizer, BitsAndBytesConfig
4from peft import PeftModel, PeftConfig
5
6#BNB_CONFIG = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4")
7model_chat = "audichandra/Gajah-7B"
8model1 = AutoModelForCausalLM.from_pretrained(model_chat
9 , torch_dtype=torch.bfloat16, device_map="auto", pad_token_id=0
10 , attn_implementation="flash_attention_2"
11 , cache_dir="/workspace"
12 #, quantization_config=BNB_CONFIG
13 )
14
15tokenizer = LlamaTokenizer.from_pretrained(model_chat)
16
17def generate_response(question: str) -> str:
18 chat = [
19 {"role": "system", "content": "Ada yang bisa saya bantu?"},
20 {"role": "user", "content": question},
21 ]
22
23 prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
24
25 inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=True)
26
27 with torch.no_grad():
28 outputs = model1.generate(input_ids=inputs["input_ids"].to("cuda"),
29 attention_mask=inputs.attention_mask,
30 eos_token_id=tokenizer.eos_token_id,
31 pad_token_id=tokenizer.eos_token_id,
32 max_new_tokens=512)
33 response = tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True)[0]
34
35 assistant_start = f'''{question} \n assistant\n '''
36 response_start = response.find(assistant_start)
37 return response[response_start + len(assistant_start) :].strip()
38
39start_time = time.time()
40prompt = "bagaimana saya dapat membatalkan pembelian saya?"
41print(generate_response(prompt))
42
43end_time = time.time()
44elapsed_time = end_time - start_time
45print(f"Elapsed time: {elapsed_time} seconds")
1import torch
2import time
3from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM, AutoConfig, LlamaTokenizer, BitsAndBytesConfig
4from peft import PeftModel, PeftConfig
5
6#BNB_CONFIG = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4")
7#model_save_path1 = "/workspace/axolotl/merged_model"
8model_chat = "audichandra/Gajah-7B"
9device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10model1 = AutoModelForCausalLM.from_pretrained(model_chat
11 , torch_dtype=torch.bfloat16
12 #, device_map="auto", pad_token_id=0
13 #, attn_implementation="flash_attention_2"
14 , cache_dir="/workspace"
15 #, quantization_config=BNB_CONFIG
16 ).to(device)
17tokenizer = LlamaTokenizer.from_pretrained(model_chat)
18
19def generate_response(question: str) -> str:
20 chat = [
21 {"role": "system", "content": "Ada yang bisa saya bantu?"},
22 {"role": "user", "content": question},
23 ]
24
25 prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
26 inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=True)
27
28 inputs = inputs.to(device) # Ensure inputs are on the same device as the model
29
30 with torch.no_grad():
31 outputs = model1.generate(**inputs, max_new_tokens=512)
32 response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
33
34 assistant_start = f'''{question} \n assistant\n '''
35 response_start = response.find(assistant_start)
36 return response[response_start + len(assistant_start) :].strip()
37
38
39# Use the functions together
40start_time = time.time()
41prompt = "bagaimana saya dapat membatalkan pembelian saya?"
42print(generate_response(prompt))
43
44end_time = time.time()
45elapsed_time = end_time - start_time
46print(f"Elapsed time: {elapsed_time} seconds")
47
1@article{Merak,
2 title={Merak-7B: The LLM for Bahasa Indonesia},
3 author={Muhammad Ichsan},
4 publisher={Hugging Face}
5 journal={Hugging Face Repository},
6 year={2023}
7}
8
9@article{dettmers2023qlora,
10 title = {QLoRA: Efficient Finetuning of Quantized LLMs},
11 author = {Dettmers, Tim and Pagnoni, Artidoro and Holtzman, Ari and Zettlemoyer, Luke},
12 journal = {arXiv preprint arXiv:2305.14314},
13 year = {2023}
14}
15
16@article{axolotl,
17 author = {{OpenAccess AI Collective}},
18 title = {Axolotl: A Repository for AI Research and Development},
19 year = {2023},
20 publisher = {GitHub},
21 journal = {GitHub repository},
22 howpublished = {\url{https://github.com/OpenAccess-AI-Collective/axolotl}}
23}