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

基于RAG方案的LLM应用调用RetrievalQA链触发TypeError错误求助

问题:RetrievalQA调用时触发TypeError: unhashable type: 'list'

问题背景

按照教程实现LLM应用,仅将示例中的PDF Loader替换为CSV Loader,调用RetrievalQA链时触发如下TypeError错误。

报错代码及信息

触发错误的代码行:

question = "Is probability a class topic?"
result = qa_chain({"query": question})  # 该行触发错误
result["result"]

报错栈信息:

TypeError Traceback (most recent call last)
Cell In[18], line 2
1 question = "Is probability a class topic?"
----> 2 result = qa_chain({"query": question })
3 print(result["result"])
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\langchain\chains\base.py:312, in Chain.__call__(self, inputs, return_only_outputs, callbacks, tags, metadata, run_name, include_run_info)
310 except BaseException as e:
.......
........
--> 808 if task in custom_tasks:
809 normalized_task = task
810 targeted_task, task_options = clean_custom_task(custom_tasks[task])
TypeError: unhashable type: 'list'

完整代码

from langchain.document_loaders import CSVLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM,pipeline
from langchain import HuggingFacePipeline
from langchain.prompts import PromptTemplate
from langchain.chains import RetrievalQA

file = '<<path to CSV File>>'
loader = CSVLoader(file_path=file, encoding='utf8')
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
docs = text_splitter.split_documents(documents)
modelPath = "<hugging face local folder>\\all-MiniLM-L6-v2"
model_kwargs = {'device':'cpu'}
encode_kwargs = {'normalize_embeddings':False}
embeddings = HuggingFaceEmbeddings(
  model_name = modelPath,
  model_kwargs = model_kwargs,
  encode_kwargs=encode_kwargs
)
db = FAISS.from_documents(docs, embeddings)
tokenizer = AutoTokenizer.from_pretrained("<<huggingface local folder>>\\flan-t5-large")
model = AutoModelForSeq2SeqLM.from_pretrained("<<huggingface local folder>>\\flan-t5-large")
pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
llm = HuggingFacePipeline(
    pipeline = pipeline,
    model_kwargs={"temperature": 0, "max_length": 512},
)
template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Keep the answer as concise as possible. 
{context}
Question: {question}
Helpful Answer:"""
QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
qa_chain = RetrievalQA.from_chain_type(   
  llm=llm,
    verbose=True,
  chain_type="stuff",   
  retriever=db.as_retriever(),   
  chain_type_kwargs={"prompt": QA_CHAIN_PROMPT} 
) 
question = "Is probability a class topic?"
result = qa_chain({"query": question }) 
print(result["result"])

解决方案

错误根源是创建HuggingFacePipeline实例时,错误地将transformers.pipeline类传入pipeline参数,而不是之前创建好的pipe实例。

修改llm的定义部分,将pipeline = pipeline改为pipeline = pipe:

llm = HuggingFacePipeline(
    pipeline = pipe,  # 替换为已创建的pipe实例
    model_kwargs={"temperature": 0, "max_length": 512},
)

修改后重新运行代码即可解决该TypeError问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 23:47:13