Langchain/Huggingface 管道关于我没有包含的 model_kwargs 的错误

问题描述 投票:0回答:1

我目前正在尝试使用 Helsinki-NLP/opus-mt-en-de 和 de-en 模型。我试图设置一个管道并将两者用作 LLMChain 但我不断收到相同的错误:

ValueError: The following `model_kwargs` are not used by the model: ['pipeline_kwargs', 'return_full_text'] (note: typos in the generate arguments will also show up in this list)

我使用以下代码片段来初始化两个模型,并在测试输出后运行该代码片段:

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",
    )

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

    _de_to_en_llm = HuggingFacePipeline(pipeline=_de_to_en_pipeline)
    _en_to_de_llm = HuggingFacePipeline(pipeline=_en_to_de_pipeline)

    _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_pipeline = get_translation_chains()

print(en_to_de_chain.invoke({"text": "Hello, how are you?"}))

我对使用 LLM 以及 Huggingface 和 langchain 库相当陌生,找不到任何东西可以给我提供关于这方面的线索。

我尝试使用管道仅设置我想要的任务“translation_de_to_en”,反之亦然,以及仅对默认管道和更详细的管道使用“翻译”。我还尝试将 kwargs 选项设置为 None 和 False 但没有成功

pipeline translation huggingface-transformers langchain large-language-model
1个回答
0
投票

您的代码没有任何错误。

错误的原因是,从

0.0.28
的版本
langchain-community
开始,只有任务

  • text2text-generation
  • text-generation
  • summarization

支持

HuggingFacePipeline

您的任务是

translation
,目前尚不支持。

至于为什么会出现错误,Langchain 将参数

return_full_text
(参见 this,第 264 行)传递给底层 HuggingFace 模型。但是,
MarianMTModel
(您正在使用的模型)不将此作为参数。

您最好直接使用基本 HuggingFace 模型。这是最简单的解决方案。

translation = _en_to_de_pipeline("Hello, how are you?")
print(translation)

输出

[{'translation_text': "Hallo, wie geht's?"}]

返回没有错误。

© www.soinside.com 2019 - 2024. All rights reserved.