langchain ``` ChatHuggingFace 请求 API Token 即使是本地下载的模型 ```

bq9c1y66  于 2个月前  发布在  其他
关注(0)|答案(2)|浏览(30)

检查其他资源

  • 为这个问题添加了一个非常描述性的标题。
  • 使用集成搜索在LangChain文档中进行了搜索。
  • 使用GitHub搜索找到了一个类似的问题,但没有找到。
  • 我确信这是LangChain中的一个bug,而不是我的代码。
  • 通过更新到LangChain的最新稳定版本(或特定集成包)无法解决此bug。

问题描述

问题
当使用来自langchain_huggingface库的ChatHuggingFace模型与本地下载的模型一起使用时,系统会提示需要Hugging Face API密钥。尽管模型是存储在本地并在没有互联网连接的情况下访问的,但这种行为仍然存在。当直接使用llm.invoke()方法与HuggingFacePipeline一起使用时,不会出现此问题。

示例代码

from transformers import BitsAndBytesConfig
from langchain_huggingface import HuggingFacePipeline
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

##### 定义量化

quantization_config = BitsAndBytesConfig(
 load_in_4bit=True,
 bnb_4bit_quant_type="nf4",
 bnb_4bit_compute_dtype="bfloat16",
 bnb_4bit_use_double_quant=True
)

##### 初始化LLM和参数

model_id = "microsoft/Phi-3-mini-4k-instruct"
tokenizer = AutoTokenizer.from_pretrained("../Phi-3-mini-4k-instruct/")
model = AutoModelForCausalLM.from_pretrained("../Phi-3-mini-4k-instruct/", quantization_config=quantization_config)

##### 初始化管道

pipe = pipeline(
 task="text-generation",
 model=model,
 tokenizer=tokenizer,
 max_new_tokens=1024,
 do_sample=False,
 repetition_penalty=1.03,
)

##### 初始化HuggingFace管道

llm = HuggingFacePipeline(
 pipeline=pipe,
)

##### 成功调用

llm.invoke("What is HuggingFace?")
(Output: "What is HuggingFace?\n\nHuggingFace is a company and an open-source community...")

##### 使用聊天模型

from langchain_huggingface import ChatHuggingFace
llm_engine_hf = ChatHuggingFace(llm=llm)
llm_engine_hf.invoke("Hugging Face is")
prdp8dxp

prdp8dxp1#

你是否在环境变量中设置了Hugging Face令牌?

xu3bshqb

xu3bshqb2#

你好@CrasCris,
我已经在本地下载了Phi-3模型,但想在一台不允许连接第三方API令牌(如Hugging Face)的机器上访问它。假设我想在没有互联网连接的机器上访问该模型。这就是为什么我一直没有使用Hugging Face API密钥的原因。在这种情况下,你有什么建议吗?

相关问题