pythonlangchain

Using create_retrieval_chain due to RetrievalQA deprecation


I'm currently working with langchain and I saw that RetrievalQA was deprecated and that create_retrieval_chain can be used instead.
I tried the example mentioned in the documentation :

from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI


retriever = ...  # Your retriever
llm = ChatOpenAI()

system_prompt = (
    "Use the given context to answer the question. "
    "If you don't know the answer, say you don't know. "
    "Use three sentence maximum and keep the answer concise. "
    "Context: {context}"
)
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system_prompt),
        ("human", "{input}"),
    ]
)
question_answer_chain = create_stuff_documents_chain(llm, prompt)
chain = create_retrieval_chain(retriever, question_answer_chain)

chain.invoke({"input": query})

Except the module langchain.chains isn't recognized when I try the import. I'm using the 1.0.3 version of langchain.


Solution

  • langchain.chains was moved to langchain_classic.chains

    from langchain_classic.chains import create_retrieval_chain
    

    More: langchain-classic in What's new in v1