weiren119 commited on
Commit
c910b0b
1 Parent(s): 72152ee

docs: append usage

Browse files
Files changed (1) hide show
  1. README.md +60 -0
README.md CHANGED
@@ -23,3 +23,63 @@ The following `bitsandbytes` quantization config was used during training:
23
 
24
 
25
  - PEFT 0.4.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
 
25
  - PEFT 0.4.0
26
+
27
+ ## Usage
28
+ ### Installation dependencies
29
+ ```
30
+ $pip install transformers torch peft
31
+ ```
32
+ #### Run the inference
33
+ ```
34
+ import transformers
35
+ import torch
36
+ from transformers import AutoTokenizer, TextStreamer
37
+ from peft import AutoPeftModelForCausalLM
38
+
39
+ # Use the same tokenizer from the source model
40
+ original_model_path="NousResearch/Llama-2-7b-chat-hf"
41
+ tokenizer = AutoTokenizer.from_pretrained(original_model_path, use_fast=False)
42
+
43
+ # Load qlora fine-tuned model, you can replace this with your own model
44
+ qlora_model_path = "weiren119/traditional_chinese_qlora_llama2"
45
+
46
+ model = AutoPeftModelForCausalLM.from_pretrained(
47
+ qlora_model_path,
48
+ load_in_4bit=qlora_model_path.endswith("4bit"),
49
+ torch_dtype=torch.float16,
50
+ device_map='auto'
51
+ )
52
+
53
+ system_prompt = """You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
54
+
55
+ If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."""
56
+
57
+
58
+
59
+
60
+ def get_prompt(message: str, chat_history: list[tuple[str, str]]) -> str:
61
+ texts = [f'[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n']
62
+ for user_input, response in chat_history:
63
+ texts.append(f'{user_input.strip()} [/INST] {response.strip()} </s><s> [INST] ')
64
+ texts.append(f'{message.strip()} [/INST]')
65
+ return ''.join(texts)
66
+
67
+
68
+ print ("="*100)
69
+ print ("-"*80)
70
+ print ("Have a try!")
71
+
72
+ s = ''
73
+ chat_history = []
74
+ while True:
75
+ s = input("User: ")
76
+ if s != '':
77
+ prompt = get_prompt(s, chat_history)
78
+ print ('Answer:')
79
+ tokens = tokenizer(prompt, return_tensors='pt').input_ids
80
+ #generate_ids = model.generate(tokens.cuda(), max_new_tokens=4096, streamer=streamer)
81
+ generate_ids = model.generate(input_ids=tokens.cuda(), max_new_tokens=4096, streamer=streamer)
82
+ output = tokenizer.decode(generate_ids[0, len(tokens[0]):-1]).strip()
83
+ chat_history.append([s, output])
84
+ print ('-'*80)
85
+ ```