1

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

3 Answers3

1

Could you try below steps.

Create a virtual environment using the command

python -m venv my_venv_name

Activate the virtual environment by executing source

 my_venv_name/bin/activate

PIP install libraries

pip install langchain==0.0.208 
pip install """Other required libraries like OpenAI etc..

You should now successfully able to import

 from langchain.chains import ConversationBufferMemory
ZKS
  • 817
  • 3
  • 16
  • 31
  • Thanks for you reply; however it would seem the problem persists. Even after creating the virtual environment, activating it, then reinstalling all required libraries, langchain.chains continues to be uncooperative. If it helps, I am running python version 3.8.9 and I am using a mac. On the note of installing a different version of langchain, `pip install langchain==0.0.208 ` , still had the same negative results even in the venv. Perhaps there's something I am not seeing here, any other ideas are appreciated. – Tristan Tucker Aug 10 '23 at 23:46
1

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.

Marcin
  • 479
  • 6
  • 11
  • Thanks for the response. If you would, please tell me what version of langchain shows up when you run "pip show langchain" For me, even after upgrading it, it says "Version: 0.0.27" instead of "0.0.266", I guess it rounded up or something. I also realized since posting this question that the langchain I installed using pip is vastly different from the langchain I downloaded straight from github. Most notably the "chains" folder is missing a lot more in the pip version, which seems to be the source of my problem. When I try switching folders or running the Github version, errors continue. – Tristan Tucker Aug 17 '23 at 19:47
  • @TristanTucker the 'pip show langchain' shows version '0.0.266'. It is really weird that it shows '0.0.27' on your machine and I don't think there is something like 'roundig up' because those are natural numbers separated with dots. (so 0, 0, 266 and not 0.267 as float number). Try to install newer version of python (I have 3.11), as older ones may not support the later versions of langchain (and therefore you can't install it). – Marcin Aug 18 '23 at 12:11
  • Thanks for the help! After updating python to version 3.11.4 I was able to install langchain version 0.0.268 and found ConversationalRetrievalChain in langchain.chains.conversational_retrieval.base – Tristan Tucker Aug 19 '23 at 04:38
0

ConversationBufferMemory belongs to langchain.memory or specifically langchain.memory.buffer, if you do

from langchain.memory.buffer import ConversationBufferMemory
from langchain.memory import ConversationBufferMemory

Both should work. Check the API reference > https://api.python.langchain.com/en/latest/memory/langchain.memory.buffer.ConversationBufferMemory.html

By the way, langchain in my env

pip list | grep langchain
langchain                0.0.264
j3ffyang
  • 1,049
  • 12
  • 12