Views
No views yet
1pip install bitsandbytes
2pip install -q datasets loralib sentencepiece
3pip install -q git+https://github.com/zphang/transformers@c3dc391 # this model uses a fork of transformers that provides LLaMA tokenizer; this wasn't merged yet
4pip install -q git+https://github.com/huggingface/peft.git1from peft import PeftModel
2from transformers import LLaMATokenizer, LLaMAForCausalLM, GenerationConfig
3
4tokenizer = LLaMATokenizer.from_pretrained("decapoda-research/llama-7b-hf")
5model = LLaMAForCausalLM.from_pretrained(
6 "decapoda-research/llama-7b-hf",
7 load_in_8bit=True,
8 device_map="auto",
9)
10model = PeftModel.from_pretrained(model, "robinhad/ualpaca-7b-llama")
11
12# convert input to correct prompt
13def generate_prompt(instruction, input=None):
14 if input:
15 return f"""Унизу надається інструкція, яка описує завдання разом із вхідними даними, які надають додатковий контекст. Напиши відповідь, яка правильно доповнює запит.
16
17### Інструкція:
18{instruction}
19
20### Вхідні дані:
21{input}
22
23### Відповідь:"""
24 else:
25 return f"""Унизу надається інструкція, яка описує завдання. Напиши відповідь, яка правильно доповнює запит.
26
27### Інструкція:
28{instruction}
29
30### Відповідь:"""
31
32# config and inference
33generation_config = GenerationConfig(
34 temperature=0.2,
35 top_p=0.75,
36 num_beams=4,
37)
38
39def evaluate(instruction, input=None):
40 prompt = generate_prompt(instruction, input)
41 inputs = tokenizer(prompt, return_tensors="pt")
42 input_ids = inputs["input_ids"].cuda()
43 generation_output = model.generate(
44 input_ids=input_ids,
45 generation_config=generation_config,
46 return_dict_in_generate=True,
47 output_scores=True,
48 max_new_tokens=256
49 )
50 for s in generation_output.sequences:
51 output = tokenizer.decode(s)
52 print("Відповідь:", output.split("### Відповідь:")[1].strip())
53
54input_data = "Як звали батька Тараса Григоровича Шевченка?"
55print("Інструкція:", input_data)
56evaluate("Інструкція: " + input_data)