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

本地GPTQ LLM与Langchain集成时出现Prompt参数类型错误

解决ValueError:prompt参数应为字符串,实际传入list类型的问题

错误信息

ValueError:参数prompt应为字符串类型,实际传入的是<class 'list'>。若要在多个prompt上运行LLM,请使用generate方法。

注意:使用ChatOpenAI()时无此错误,仅使用本地量化LLM(Llama-2-13B-chat-GPTQ)时触发。

相关代码

加载本地LLM的代码

def get_llm_model():
    tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=True)
    model = AutoGPTQForCausalLM.from_quantized(
        "TheBloke/Llama-2-13B-chat-GPTQ",
        use_safetensors=True,
        trust_remote_code=True,
        device="cuda:0",
        use_triton=False,
        quantize_config=None,
    )
    generation_config = GenerationConfig.from_pretrained(MODEL_ID)
    pipe = pipeline(
        "text-generation",
        model=model,
        tokenizer=tokenizer,
        max_length=2048,
        temperature=0,
        top_p=0.95,
        repetition_penalty=1.15,
        generation_config=generation_config,
    )
    local_llm = HuggingFacePipeline(pipeline=pipe)
    return local_llm

触发错误的确定问题主题代码

def determine_topic(question):
    model = get_llm_model()
    template = f"Determine the topic in the following question: \n{question}. "
    human_prompt = HumanMessagePromptTemplate.from_template(template)
    chat_prompt = ChatPromptTemplate.from_messages([human_prompt])
    result = model(chat_prompt.format_prompt(question=question).to_messages())
    return result.content

问题原因

ChatOpenAI支持直接传入Message对象列表作为输入,但HuggingFacePipeline包装的text-generation类型pipeline仅接受字符串类型的prompt。chat_prompt.format_prompt(...).to_messages()返回的是Message对象列表,不符合本地LLM的输入要求,因此触发类型错误。

解决方案

将ChatPromptTemplate生成的内容转换为字符串格式,或者直接构造符合模型要求的字符串prompt,以下是两种可行修改方式:

方式1:直接构造Llama-2聊天格式字符串

def determine_topic(question):
    model = get_llm_model()
    # 按照Llama-2官方聊天模板构造字符串prompt
    prompt = f"<s>[INST] Determine the topic in the following question: \n{question}. [/INST]"
    result = model(prompt)
    return result.content

方式2:从ChatPromptTemplate提取字符串

def determine_topic(question):
    model = get_llm_model()
    human_prompt = HumanMessagePromptTemplate.from_template("Determine the topic in the following question: \n{question}. ")
    chat_prompt = ChatPromptTemplate.from_messages([human_prompt])
    # 将prompt转换为字符串而非Message列表
    prompt_str = chat_prompt.format_prompt(question=question).to_string()
    result = model(prompt_str)
    return result.content

测试调用

question = "How do you know x about topic y"
print(determine_topic(question))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 07:35:39