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

LangChain配置HuggingFaceEndpoint遇model_kwargs报错求解决

解决Flan-T5-Large调用时的模型参数不兼容报错

问题原因

HuggingFaceEndpoint默认会向Hugging Face Inference API传递一些通用生成参数,但google/flan-t5-large属于T5系列encoder-decoder模型,不支持return_full_text、watermark、stop_sequences、stop这些参数,因此触发报错。

解决步骤

初始化HuggingFaceEndpoint时,通过model_kwargs显式覆盖默认参数,只传递模型兼容的配置:

  • 设置return_full_text=False:T5模型默认只返回生成的新文本,无需返回输入+输出的完整内容
  • 将stop、stop_sequences设为None:告知LangChain不要传递这些模型不支持的停止序列参数
  • 禁用watermark:Flan-T5不支持该水印生成参数

修改后的完整代码

from langchain_huggingface import HuggingFaceEndpoint
from langchain.prompts import PromptTemplate

repo_id="google/flan-t5-large"
huggingface_llm = HuggingFaceEndpoint(
    huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
    repo_id=repo_id,
    temperature=0,
    max_new_tokens=200,
    model_kwargs={
        "return_full_text": False,
        "stop": None,
        "stop_sequences": None,
        "watermark": False
    }
)

def flan_process(tema, pregunta):
    template = "Eres un experto asistente en {tema}. Responde a la siguiente pregunta: {pregunta}"
    prompt=PromptTemplate(template=template,input_variables=["tema","pregunta"])

    flan_chain = prompt | huggingface_llm
    respuesta=flan_chain.invoke({"tema":tema, "pregunta":pregunta})

    return respuesta

tema=input("Ingrese el tema: ")
pregunta=input("Ingrese la pregunta: ")

flan_reply=flan_process(tema, pregunta)
print(f"Respuesta Flan: {flan_reply}")

补充说明

这样修改后,LangChain只会向Inference API传递模型支持的参数,避免了不兼容参数引发的报错。如果后续出现其他未列出的不兼容参数,也可以在model_kwargs中显式设置为模型支持的值或None。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 13:26:10