justtherightsize commited on
Commit
277c3a7
1 Parent(s): d21b521

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +57 -2
README.md CHANGED
@@ -80,8 +80,63 @@ This model paricipated in multi-turn dialogues and responses empathetically.
80
  - **Repository:** <https://github.com/justtherightsize/empo>
81
  - **(*non-anonymized*) Paper preprint:** <https://arxiv.org/abs/2406.19071>
82
 
83
- ## Usage
84
- TODO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
 
87
  ## Out-of-Scope Usage
 
80
  - **Repository:** <https://github.com/justtherightsize/empo>
81
  - **(*non-anonymized*) Paper preprint:** <https://arxiv.org/abs/2406.19071>
82
 
83
+ ## Usage - Generate a response in a dialogue
84
+ ```python
85
+ from peft import PeftModel
86
+ from transformers import BitsAndBytesConfig, AutoModelForCausalLM, AutoTokenizer, pipeline
87
+ import torch
88
+ from huggingface_hub import login
89
+
90
+ # HF login: you have to be logged in and agree to the license of the base
91
+ # model: https://huggingface.co/alignment-handbook/zephyr-7b-sft-full
92
+ hf_key = "Your key here"
93
+ login(hf_key)
94
+
95
+ # Load tokenizer either from remote
96
+ adapter_id = "justtherightsize/zephyr-7b-sft-full124"
97
+ base_model_id = "alignment-handbook/zephyr-7b-sft-full"
98
+ tokenizer = AutoTokenizer.from_pretrained(adapter_id)
99
+
100
+ # Prepare dialog and convert to chat template
101
+ sys_msg = "You are a friendly assistant, who provides empathetic responses to the user. " \
102
+ "The input contains previous turn of the dialog, where each utterance is prefaced " \
103
+ "with tags <|user|>, or <|assistant|>. Be empathetic and precise. " \
104
+ "Make sure to give responses that make dialogue flow. Avoid repeating the prompt. " \
105
+ "Please respond creatively and expressively to make the responses longer. You can offer advice."
106
+
107
+ dialog = ["Yeah about 10 years ago I had a horrifying experience. It was 100% their fault but they hit the water barrels and survived. They had no injuries but they almost ran me off the road.",
108
+ "Did you suffer any injuries?",
109
+ "No I wasn't hit. It turned out they were drunk. I felt guilty but realized it was his fault."]
110
+
111
+ dwroles = [{"role": "system", "content": sys_msg}]
112
+ for j in range(len(dialog)):
113
+ dwroles.append(
114
+ {"role": "user", "content": dialog[j]} if j % 2 == 0 else
115
+ {"role": "assistant", "content": dialog[j]})
116
+ template = tokenizer.apply_chat_template(dwroles, tokenize=False, add_generation_prompt=True)
117
+
118
+ # Load the big model first & resize embeds, load PEFT model
119
+ quantization_config = BitsAndBytesConfig(
120
+ load_in_4bit=True,
121
+ bnb_4bit_quant_type="nf4",
122
+ bnb_4bit_compute_dtype=torch.bfloat16
123
+ )
124
+ model = AutoModelForCausalLM.from_pretrained(
125
+ base_model_id,
126
+ quantization_config=quantization_config,
127
+ trust_remote_code=True
128
+ )
129
+ model.resize_token_embeddings(len(tokenizer))
130
+ model.config.use_cache = False
131
+ model = PeftModel.from_pretrained(model, adapter_id)
132
+
133
+ # Instantiate generation pipeline
134
+ pipe_gen = pipeline("text-generation", model=model, tokenizer=tokenizer)
135
+
136
+ # Generate the response
137
+ out = pipe_gen(template, return_full_text=False, max_new_tokens=500)[0]['generated_text']
138
+ print(out)
139
+ ```
140
 
141
 
142
  ## Out-of-Scope Usage