在 LangChain 中链接 Runnable 时出现类型错误:需要 Runnable、callable 或 dict

Shr*_*tan 5 python nlp artificial-intelligence langchain

我正在与 LangChain 合作创建一个基于检索的 QA 系统。但是,当我尝试链接 Runnables 时,遇到无法解决的 TypeError。当我尝试使用 | 时发生错误 (管道)运算符将 RunnablePassthrough 与自定义提示和 ChatOpenAI 实例链接起来。

这是我收到的错误消息:

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

我已经查明了这部分代码的错误:

rag_chain = ( {"context": context, "question": RunnablePassthrough()} | rag_custom_prompt | llm )

我希望 RunnablePassthrough() 将上下文和问题传递到链中的下一步,但在强制到 Runnable 期间似乎失败了。

以下几乎是我的全部代码:

## Convert the pdf into txt
def pdf_to_txt(inst_manuals):

    txt = ""
    for manual in inst_manuals:
        reader = PdfReader(inst_manuals)
        for page in reader.pages:
            txt += page.extract_text()

    return txt

## Convert txt into chunks 
def chunkify_txt(txt):

    txt_splitter = CharacterTextSplitter(
        separator= "\n",
        chunk_size= 1000,
        chunk_overlap= 200,
        length_function= len
    )

    chunks = txt_splitter.split_text(txt)

    return chunks

## Obtain the vector store
def get_vector(chunks):
    embeddings = OpenAIEmbeddings()

    vectorstore = FAISS.from_texts(texts= chunks, embedding = embeddings)

    return vectorstore

## Retrieve useful info similar to user query
def retrieve(vectorstore, question):
    logging.basicConfig()
    logging.getLogger("langchain.retrievers.multi_query").setLevel(logging.INFO)

    retriever_from_llm = MultiQueryRetriever.from_llm(
        retriever=vectorstore.as_retriever(), llm=ChatOpenAI(temperature=0)
    )
    unique_docs = retriever_from_llm.get_relevant_documents(query=question)
    
    print(f"Number of unique documents retrieved: {len(unique_docs)}")
    
    return unique_docs
    

## Generate response for user query

def gen_resp(retriever, question):
    llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
    template = """... [custom prompt template] ..."""
    rag_custom_prompt = PromptTemplate.from_template(template)

    context = "\n".join(doc.page_content for doc in retriever)

    rag_chain = (
        {"context": context, "question": RunnablePassthrough()} | rag_custom_prompt | llm
    )

    answer = rag_chain.invoke(question)

    return answer

Run Code Online (Sandbox Code Playgroud)

以前有人遇到过这种情况吗?关于如何正确链接这些 Runnables 或者我可能做错了什么有什么建议吗?

我已尝试以下操作:

使用不同的检索器(可根据要求提供详细信息)。我已经检查了 LangChain 文档以了解 Runnables 和链接操作的正确使用。我尝试互换 rag_chain 字典中的上下文和问题键,以查看顺序是否是问题所在。

Shr*_*tan -1

如果您正在使用 langchain 并尝试实现 RAG(检索增强生成),以下是我如何解决在 get_vector 函数中创建检索器的问题。

\n

关键是初始化一个检索器,该检索器使用所提供文档中的 FAISS 矢量存储。这里\xe2\x80\x99是正确设置检索器的函数:

\n
from langchain.embeddings import OpenAIEmbeddings\nfrom langchain.vectorstores import FAISS\nfrom langchain.llms import ChatOpenAI\nfrom langchain.prompts import PromptTemplate, \nRunnablePassthrough\n\ndef get_vector(chunks):\n  embeddings = OpenAIEmbeddings()\n  vectorstore = \n  FAISS.from_documents(documents=chunks, embedding=embeddings)\n  retriever = vectorstore.as_retriever()\nreturn retriever\n
Run Code Online (Sandbox Code Playgroud)\n

接下来,生成响应时,您不需要显式检索文档。相反,将检索器传递给 gen_resp 函数。此函数中的 RAG 逻辑将处理针对您的文档的相似性搜索:

\n
def gen_resp(retriever, question):\nllm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)\ntemplate = """\nUse the following pieces of context to answer the question at the end.\nIf you don't know the answer, just say that you do not have the relevant information needed to provide a verified answer, don't try to make up an answer.\nWhen providing an answer, aim for clarity and precision. Position yourself as a knowledgeable authority on the topic, but also be mindful to explain the information in a manner that is accessible and comprehensible to those without a technical background.\nAlways say "Do you have any more questions pertaining to this instrument?" at the end of the answer.\n{context}\nQuestion: {question}\nHelpful Answer:"""\n\nrag_custom_prompt = PromptTemplate.from_template(template)\n\n# Set up the RAG chain\nrag_chain = (\n    {"context": retriever, "question": RunnablePassthrough()} | \n    rag_custom_prompt | \n    llm\n)\n\n# Invoke the RAG chain with the question\nanswer = rag_chain.invoke(question)\n\nreturn answer\n
Run Code Online (Sandbox Code Playgroud)\n

此方法利用 langchain RAG 的强大功能来查找最相关的上下文并根据该上下文生成响应。我希望这可以帮助其他正在学习和尝试 langchain RAG 功能的人。

\n