LLM申请针对RAG方法抛出错误

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

我正在按照此处的说明进行示例 - https://www.linkedin.com/pulse/get-insight-from-your-business-data-build-llm-application-jain/

我已经按照示例尝试了 RetrievalQA Chain。唯一的变化是我有 CSV 文件而不是 PDF 加载器,并且我使用了 CSV 加载器;在致电连锁店询问问题时,我收到错误。请告诉我如何解决这个问题。

question = "Is probability a class topic?"
result = qa_chain({"query": question}) ==> This line throws the below error
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"])
python large-language-model
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.