[Python] RunnableSequence instead of LLMChain throws an error (updating from depreacated...

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    When I have this code first

    from langchain_community.llms import HuggingFacePipeline
    from transformers import AutoTokenizer
    import transformers
    import torch

    model="meta-llama/Llama-2-7b-chat-hf"
    tokenizer=AutoTokenizer.from_pretrained(model)

    pipeline=transformers.pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    torch_dtype=torch.bfloat16,
    trust_remote_code=True,
    device_map="auto",
    max_length=1000,
    do_sample=True,
    top_k=10,
    num_return_sequences=1,
    eos_token_id=tokenizer.eos_token_id
    )

    llm=HuggingFacePipeline(pipeline=pipeline, model_kwargs={'temperature':0})

    from langchain.prompts import PromptTemplate
    prompt_template=PromptTemplate(input_variables=["book_name"],
    template="Provide me a concise summary of the book {book_name}")


    and then I complete it with

    from langchain.chains import LLMChain

    chain = LLMChain(llm=llm, prompt=prompt_template, verbose=True)
    response= chain.run("Alchemist")
    print(response)


    I get a response with the summary I wanted , but I get deprecation warnings. So following the warnings I try to replace the second part with

    chain = prompt | llm
    response = chain.invoke("The name of the rose")
    print(response)


    but I get the error

    TypeError: Expected a Runnable, callable or dict.Instead got an unsupported type: <class 'str'>


    What am I doing wrong?

    I have used something similar but with llm being a HuggingFaceEndPoint and in that case it worked, so I suspect that it has to do with llm being a HuggingFacePipeline but can someone tell me how to correct the code?

    Edit: I tried

    chain2 = prompt | llm | StrOutputParser()
    response2 = chain2.invoke({"bookname":"The name of the rose"})
    # response2 = chain2.invoke("The name of the rose")
    print(response2)


    but the error persists

    Continue reading...

Compartilhe esta Página