最新的Llama🦙(Large Language Model Meta AI) 3.1是由Meta AI开发的一个强大的人工智能模型,在自然语言处理(NLP)社区中引起了广泛关注。它是迄今为止最强大的开源大语言模型。在这篇博客中,我将指导您如何从Hugging Face🤗克隆Llama 3.1模型,并使用Python在本地机器上运行它。之后,您就可以将其集成到任何AI项目中。

Meta-llama-3.1-8b-Instruct hugging face模型

Meta-llama-3.1-8b-Instruct模型

访问meta llama 3.1模型

hugging face中的受限模型

hugging face设置

创建hugging face令牌

创建hugging face令牌

huggingface令牌
现在在您喜欢的终端上运行以下命令。
ACCESS_TOKEN是您复制的令牌,``是您Hugging Face账户的用户名。
git clone https://<huggingface-user-name>:<ACCESS_TOKEN>@huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct
这可能需要很长时间,取决于您的网络速度。
克隆完成后,进入克隆的文件夹并从requirements.txt安装所有依赖项。(您可以使用conda(推荐)或virtualenv创建虚拟环境) 您可以在下面资源部分提供的我的GitHub中找到requirements文件。
使用conda:
cd Meta-Llama-3.1-8B-Instruct
conda install --yes --file requirements.txt
使用pip:
cd Meta-Llama-3.1-8B-Instruct
pip install -r requirements.txt
创建一个新的Python文件(例如,test.py),并将您刚刚克隆的模型仓库的位置粘贴为model_id(例如,"D:\\Codes\\NLP\\Meta-Llama-3.1-8B-Instruct")。这里是一个例子:
import transformers
import torch
## 在这里粘贴您克隆的仓库位置
model_id = "D:\\Codes\\NLP\\Meta-Llama-3.1-8B-Instruct"
pipeline = transformers.pipeline(
"text-generation",
model=model_id,
model_kwargs={"torch_dtype": torch.bfloat16},
device_map="auto",
)
messages = [
{"role": "user", "content": "Who are you?"},
]
outputs = pipeline(
messages,
max_new_tokens=256,
)
print(outputs[0]["generated_text"][-1])
如果您想使用GPU,可以设置device_map=cuda。
步骤6:运行Python脚本
python test.py


