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

使用LangChain+HuggingFace翻译模型时model_kwargs报错求助

解决LangChain+HuggingFace翻译模型的ValueError问题

问题分析

你遇到的ValueError是因为LangChain的HuggingFacePipeline类默认会向模型传递一些针对大语言模型设计的参数(比如return_full_text、pipeline_kwargs),但Helsinki的翻译模型属于Seq2Seq模型,不支持这些参数,因此触发了未使用参数的错误。

解决方案

修改HuggingFacePipeline的初始化代码,显式过滤不必要的参数,并适配翻译模型的输出格式:

修改后的完整代码

from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms.huggingface_pipeline import HuggingFacePipeline
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline

def get_translation_chains():
    _de_en_translation_prompt = PromptTemplate.from_template(
        """Translate the following text from German to English:
        {text}
        """
    )

    _en_de_translation_prompt = PromptTemplate.from_template(
        """Translate the following text from English to German:
        {text}
        """
    )

    _en_to_de_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-de")
    _en_to_de_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-de")
    _de_to_en_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-de-en")
    _de_to_en_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-de-en")

    # 明确指定翻译任务方向
    _en_to_de_pipeline = pipeline(
        model=_en_to_de_model,
        tokenizer=_en_to_de_tokenizer,
        task="translation_en_to_de",
    )

    _de_to_en_pipeline = pipeline(
        model=_de_to_en_model,
        tokenizer=_de_to_en_tokenizer,
        task="translation_de_to_en",
    )

    # 初始化HuggingFacePipeline时过滤不必要参数,并适配输出格式
    _de_to_en_llm = HuggingFacePipeline(
        pipeline=_de_to_en_pipeline,
        return_full_text=False,
        model_kwargs={},
        transform_output=lambda x: x[0]['translation_text']
    )

    _en_to_de_llm = HuggingFacePipeline(
        pipeline=_en_to_de_pipeline,
        return_full_text=False,
        model_kwargs={},
        transform_output=lambda x: x[0]['translation_text']
    )

    _de_to_en_chain = LLMChain(
        prompt=_de_en_translation_prompt,
        llm=_de_to_en_llm,
    )

    _en_to_de_chain = LLMChain(
        prompt=_en_de_translation_prompt,
        llm=_en_to_de_llm,
    )

    return _en_to_de_chain, _de_to_en_chain

# 测试代码
en_to_de_chain, de_to_en_chain = get_translation_chains()
print(en_to_de_chain.invoke({"text": "Hello, how are you?"}))

关键修改点说明

  1. 明确翻译任务方向:在创建HuggingFace Pipeline时,使用translation_en_to_de和translation_de_to_en作为task值,让模型明确翻译方向,避免歧义。
  2. 过滤不必要参数:
    • 设置return_full_text=False:阻止LangChain传递该参数给翻译模型
    • 设置model_kwargs={}:清空默认添加的LLM生成参数,避免传递不支持的参数
  3. 适配输出格式:通过transform_outputlambda函数,将翻译pipeline返回的字典列表(如[{"translation_text": "Hallo, wie geht es dir?"}])提取为纯文本,符合LLMChain对输出格式的要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 05:53:24