run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
33.39k stars 4.67k forks source link

[Bug]:Graph_RAG_LlamaIndex_Workshop | ImportError: cannot import name 'BaseCache' from 'langchain.cache' #13734

Open data-scientist-shivam799 opened 1 month ago

data-scientist-shivam799 commented 1 month ago

Bug Description

I was going through Graph_RAG_LlamaIndex_Workshop by llama index on YouTube (link is here) and following along the notebook (notebook is here) provided in the description of the video.

While running the following code, I am getting ImportError: cannot import name 'BaseCache' from 'langchain.cache' (/usr/local/lib/python3.10/dist-packages/langchain/cache.py)

from llama_index import ( VectorStoreIndex, SimpleDirectoryReader, KnowledgeGraphIndex, ServiceContext, )

I think this is because of any internal library mismatch, does anyone else faced the same issue?

Version

0.8.9

Steps to Reproduce

Just run the colab notebook.

Relevant Logs/Tracbacks

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-c5a468c8e199> in <cell line: 14>()
     12 )
     13 
---> 14 from llama_index import (
     15     VectorStoreIndex,
     16     SimpleDirectoryReader,

4 frames
/usr/local/lib/python3.10/dist-packages/llama_index/__init__.py in <module>
     10 from typing import Optional
     11 
---> 12 from llama_index.data_structs.struct_type import IndexStructType
     13 
     14 # embeddings

/usr/local/lib/python3.10/dist-packages/llama_index/data_structs/__init__.py in <module>
      1 """Init file."""
      2 
----> 3 from llama_index.data_structs.data_structs import (
      4     IndexDict,
      5     IndexGraph,

/usr/local/lib/python3.10/dist-packages/llama_index/data_structs/data_structs.py in <module>
     12 from dataclasses_json import DataClassJsonMixin
     13 
---> 14 from llama_index.schema import BaseNode, TextNode
     15 from llama_index.data_structs.struct_type import IndexStructType
     16 

/usr/local/lib/python3.10/dist-packages/llama_index/schema.py in <module>
     11     from pydantic import BaseModel, Field, root_validator
     12 
---> 13 from llama_index.bridge.langchain import Document as LCDocument
     14 from llama_index.utils import SAMPLE_TEXT
     15 

/usr/local/lib/python3.10/dist-packages/llama_index/bridge/langchain.py in <module>
      8 from langchain.agents.agent_toolkits.base import BaseToolkit
      9 from langchain.base_language import BaseLanguageModel
---> 10 from langchain.cache import BaseCache, GPTCache
     11 
     12 # callback

ImportError: cannot import name 'BaseCache' from 'langchain.cache' (/usr/local/lib/python3.10/dist-packages/langchain/cache.py)

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
dosubot[bot] commented 1 month ago

Hi @data-scientist-shivam799! I'm here to help you with the issue you're facing. Let's get this sorted out together!

To resolve the ImportError: "cannot import name 'BaseCache' from 'langchain.cache'" encountered while running the provided Colab notebook for the Graph_RAG_LlamaIndex_Workshop, you should ensure that you are not trying to import BaseCache from langchain.cache as it is not listed in the provided module. Instead, you should check the imports and remove or correct the import statement related to BaseCache.

Based on the provided content, BaseCache is not part of the langchain module. Here is a corrected version of the import statements:

import langchain
from langchain.agents import AgentExecutor, AgentType, initialize_agent

# agents and tools
from langchain.agents.agent_toolkits.base import BaseToolkit
from langchain.base_language import BaseLanguageModel

# callback
from langchain.callbacks.base import BaseCallbackHandler, BaseCallbackManager
from langchain.chains.prompt_selector import ConditionalPromptSelector, is_chat_model
from langchain.chat_models.base import BaseChatModel
from langchain.docstore.document import Document
from langchain.memory import ChatMessageHistory, ConversationBufferMemory

# chat and memory
from langchain.memory.chat_memory import BaseChatMemory
from langchain.output_parsers import ResponseSchema

# prompts
from langchain.prompts import PromptTemplate
from langchain.prompts.chat import (
    AIMessagePromptTemplate,
    BaseMessagePromptTemplate,
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
    SystemMessagePromptTemplate,
)

# schema
from langchain.schema import (
    AIMessage,
    BaseMemory,
    BaseMessage,
    BaseOutputParser,
    ChatGeneration,
    ChatMessage,
    FunctionMessage,
    HumanMessage,
    LLMResult,
    SystemMessage,
)

# embeddings
from langchain.schema.embeddings import Embeddings
from langchain.schema.prompt_template import BasePromptTemplate

# input & output
from langchain.text_splitter import RecursiveCharacterTextSplitter, TextSplitter
from langchain.tools import BaseTool, StructuredTool, Tool
from langchain_community.chat_models import ChatAnyscale, ChatOpenAI
from langchain_community.embeddings import (
    HuggingFaceBgeEmbeddings,
    HuggingFaceEmbeddings,
)

# LLMs
from langchain_community.llms import AI21, BaseLLM, Cohere, FakeListLLM, OpenAI

Make sure to remove any import statement for BaseCache as it is not part of the langchain module according to the provided content [1].

#### Details **About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

logan-markewich commented 1 month ago

v0.8.9 is quite old 😅 you probably need to lower your langchain version quite a bit (v0.0.303 maybe?)

Future versions of llamaindex would have removed the dependency on langchain too

data-scientist-shivam799 commented 1 month ago

@logan-markewich I tried to downgrade langchain and also tried upgrading llama_index, but the issue continues.