本文是 Meta 官网推出的 Llama2 使用教学博客,简单 5 步教会你如何使用 Llama2。
pip install transformers
pip install accelerate
-
访问 Meta 网站,接受许可并提交表格。请求通过后才能收到在电子邮件中的预签名 URL; -
克隆 Llama 2 知识库 到本地。
git clone https://github.com/facebookresearch/llama
-
选择要下载的模型版本,例如 7b-chat。然后就能下载 tokenizer.model 和包含 权重 的 llama-2-7b-chat 目录。
TRANSFORM=`python -c"import transformers;print ('/'.join (transformers.__file__.split ('/')[:-1])+'/models/llama/convert_llama_weights_to_hf.py')"`
pip install protobuf && python $TRANSFORM --input_dir ./llama-2-7b-chat --model_size 7B --output_dir ./llama-2-7b-chat-hf
import torch
import transformers
from transformers import LlamaForCausalLM, LlamaTokenizer
model_dir = "./llama-2-7b-chat-hf"
model = LlamaForCausalLM.from_pretrained (model_dir)
tokenizer = LlamaTokenizer.from_pretrained (model_dir)
pipeline = transformers.pipeline (
"text-generation",
model=model,
tokenizer=tokenizer,
torch_dtype=torch.float16,
device_map="auto",
)
sequences = pipeline (
'I have tomatoes, basil and cheese at home. What can I cook for dinner?\n',
do_sample=True,
top_k=10,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id,
max_length=400,