File size: 2,209 Bytes
0f4604b
 
 
5724f4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
894f8b3
5724f4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
---
pipeline_tag: text2text-generation
---
# Further instruct Tuning stanford alpaca based on <a href="https://huggingface.co/tloen/alpaca-lora-7" target="_blank">tloen/alpaca-lora-7b</a> on <a href="https://www.consumptionvoucher.gov.hk/public/pdf/2023cvs/FAQ-2023_en.pdf" target="_blank">Hong Kong 2023 Consumption Voucher Scheme Frequently Asked Questions</a>

## How to use it

```python

from transformers import LlamaForCausalLM, LlamaTokenizer,GenerationConfig
from peft import PeftModel


device_map = "auto"

tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
model = LlamaForCausalLM.from_pretrained(
    "decapoda-research/llama-7b-hf",
    load_in_8bit=True,
    device_map="auto",
)

### load model after fine tuned on alpaca datasets
model = PeftModel.from_pretrained(model, "Nelsonlin0321/alpaca-lora-7b-tuned-on-hk-cvs-fqa")

tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
tokenizer.pad_token_id = 0


def generate_prompt_eval(instruction):
    template =  f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Response:"""
    return template

eval_generation_config = GenerationConfig(
    temperature=0.1,
    top_p=0.75,
    num_beams=4,
)


def generate_answer(instruction):
    prompt = generate_prompt_eval(instruction)
    inputs = tokenizer(prompt, return_tensors="pt")
    input_ids = inputs["input_ids"].cuda()
    generation_output = model.generate(
        input_ids=input_ids,
        generation_config=eval_generation_config,
        return_dict_in_generate=True,
        output_scores=True,
        max_new_tokens=256
    )
    for s in generation_output.sequences:
        output = tokenizer.decode(s)
        # print(output)
        print("Response:", output.split("### Response:")[1].strip())


question = "Who are eligible to be disbursed with the first-instalment voucher of $1,500 on 16 April?"

generate_answer(question)
>> Response: All eligible people who have successfully registered under 2022 CVS and met the relevant eligibility criteria will be disbursed with the first-instalment voucher of $1,500 on 16 April.



```