Search code examples
pythonstreamlitopenai-apilangchainlarge-language-model

Importing ConversationalRetrievalChain from langchain.chains isn't working


I am trying to follow various tutorials on langchain and streamlit and I have encountered many problems regarding the names of imports. My main problem is that I can't seem to import ConversationalRetrievalChain from langchain.chains. This isn't the first case of this strange issue, for example

from langchain.chains import ConversationBufferMemory

this line of code doesn't work, and returns the error: cannot import name 'ConversationBufferMemory' from 'langchain.chains' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/langchain/chains/init.py)

However, when I write the following code

from langchain.chains.conversation.memory import ConversationBufferMemory

It works fine. It would appear as if specifying the path to the packet I want to use in the import statement is imperative for it to work.

With this in mind I was wondering if anyone had any insight as to what path ConversationalRetrievalChain was in. I tried this, but langchain.chains.conversational_retrieval doesn't exist and many other websites like the [official langchain website] (https://python.langchain.com/docs/modules/memory/conversational_customization) have only lead me more astray.

Does anyone know where ConversationalRetrievalChain is located in Langchain version 0.0.27, or how I might go about finding it myself. Many thanks :)

What I have tried in my code:

  • from langchain.chains import ConversationalRetrievalChain
  • from langchain.chains.conversation import ConversationalRetrievalChain
  • from langchain.chains.conversation.memory import ConversationalRetrievalChain
  • langchain.chains.conversational_retrieval.base import ConversationalRetrievalChain

other things:

  • Installing an older version of langchain (keeps saying I need python >= 3.8.1 even though I have python 3.8.9)

Where I have gone to look:

  • /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/langchain/chains/
  • Langchain documentation

Solution

  • I had quite similar issue:

    ImportError: cannot import name 'ConversationalRetrievalChain' from 'langchain.chains'
    

    For me upgrading to the newest langchain package version helped:

    pip install langchain --upgrade
    

    Actual version is '0.0.266', so maybe install that instead of '0.0.208' which somebody pointed.

    Please also install the newest version of Python (I'm using v.3.11), as the older ones may not support langchain in newer versions.