1from transformers import GenerationConfig, LlamaForCausalLM, LlamaTokenizer
2from peft import PeftModel
3
4device = "cuda"
5base_model_name = "decapoda-research/llama-7b-hf"
6lora_weights = "AGI-Edgerunners/RecAlpaca-lora-7b-v1"
7
8tokenizer = LlamaTokenizer.from_pretrained(base_model_name)
9model = PeftModel.from_pretrained(LlamaForCausalLM.from_pretrained(base_model_name), lora_weights).to(device)
10
11generation_config = GenerationConfig(
12 temperature=0.1,
13 top_p=0.73,
14 top_k=40,
15 num_beams=4
16)
17max_new_tokens = 128
18
19instruction = "Based on the movies that I've watched before, could you suggest some similar movies for me to watch next? Please use the MovieLens 100K dataset to recommend movies that you think would appeal to my tastes."
20inputs = "The Long Kiss Goodnight, French Kiss, The Maltese Falcon, Dazed and Confused, and Strange Days"
21
22prompt = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. # noqa: E501
23
24### Instruction:
25{instruction}
26
27### Input:
28{inputs}
29
30### Response:
31"""
32
33inputs = tokenizer(prompt, return_tensors="pt")
34input_ids = inputs["input_ids"].to(device)
35generation_output = model.generate(input_ids=input_ids,
36 generation_config=generation_config,
37 return_dict_in_generate=True,
38 output_scores=True,
39 max_new_tokens=max_new_tokens,
40 )
41print(tokenizer.decode(generation_output.sequences[0]))
42
see our github repository:
RecAlpaca