You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行meta-llama/Llama-2-7b-chat-hf模型遇OSError,求解决方法

问题:加载Llama-2-7b-chat-hf模型时触发OSError

参考官方博客代码实践,已安装transformers(版本4.33.0.dev0)、通过huggingface-cli完成登录,代码中也设置了use_auth_token=True,但运行以下代码时出现OSError:

from transformers import AutoTokenizer
import transformers
import torch

model = "meta-llama/Llama-2-7b-chat-hf"

tokenizer = AutoTokenizer.from_pretrained(model, use_auth_token=True)
pipeline = transformers.pipeline(
    "text-generation",
    model=model,
    torch_dtype=torch.float16,
    device_map="auto",
)

sequences = pipeline(
    'I liked "Breaking Bad" and "Band of Brothers". Do you have any recommendations of other shows I might like?\n',
    do_sample=True,
    top_k=10,
    num_return_sequences=1,
    eos_token_id=tokenizer.eos_token_id,
    max_length=200,
)
for seq in sequences:
    print(f"Result: {seq['generated_text']}")

错误信息如下:

OSError: meta-llama/Llama-2-7b-chat-hf is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'
If this is a private repository, make sure to pass a token having permission to this repo with `use_auth_token` or log in with `huggingface-cli login` and pass `use_auth_token=True`.

该模型已在模型库中存在,询问解决方法。

解决方法

  • 确认Llama-2访问权限:Llama-2是受限模型,需先在模型页面提交申请并获得Meta授权,同时在Hugging Face账号中接受模型使用协议。未通过授权的话,即便登录也无法访问模型仓库。
  • 检查token权限与有效性:
    1. 登录个人设置页面生成带读写权限的新token(避免使用只读token)。
    2. 代码中直接传入token字符串替代use_auth_token=True,示例:AutoTokenizer.from_pretrained(model, use_auth_token="你的token字符串"),避免本地缓存登录状态失效。
  • 升级transformers到稳定版:当前使用的是开发版(4.33.0.dev0),可能存在兼容性问题。执行pip install --upgrade transformers升级至最新稳定版,稳定版对Llama-2的支持更成熟。
  • 验证本地登录状态:执行huggingface-cli whoami命令,确认当前登录账号为已获Llama-2授权的账号。若输出不符,重新执行huggingface-cli login并输入正确token。
  • 手动指定模型缓存路径(可选):若本地缓存异常,可在from_pretrained中添加cache_dir参数指定新缓存目录,示例:AutoTokenizer.from_pretrained(model, use_auth_token=True, cache_dir="./llama_cache"),规避旧缓存干扰。

内容的提问来源于stack exchange,提问作者Quinten

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 05:45:02