pythonlangchainsentence-transformers

ImportError: Dependencies for InstructorEmbedding not found, while it is installed


I already installed InstructorEmbedding, but it keeps giving me the error, in jupyter notebook environment using Python 3.12 (I also tried in 3.11). Kernel restarting didn't help.

import torch
from langchain.embeddings import HuggingFaceInstructEmbeddings

DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu"


embedding = HuggingFaceInstructEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": DEVICE})

error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File /opt/conda/lib/python3.11/site-packages/langchain_community/embeddings/huggingface.py:151, in HuggingFaceInstructEmbeddings.__init__(self, **kwargs)
    150 try:
--> 151     from InstructorEmbedding import INSTRUCTOR
    153     self.client = INSTRUCTOR(
    154         self.model_name, cache_folder=self.cache_folder, **self.model_kwargs
    155     )

File /opt/conda/lib/python3.11/site-packages/InstructorEmbedding/__init__.py:1
----> 1 from .instructor import *

File /opt/conda/lib/python3.11/site-packages/InstructorEmbedding/instructor.py:9
      8 from torch import Tensor, device
----> 9 from sentence_transformers import SentenceTransformer
     10 from sentence_transformers.models import Transformer

ModuleNotFoundError: No module named 'sentence_transformers'

The above exception was the direct cause of the following exception:

ImportError                               Traceback (most recent call last)
Cell In[2], line 10
      4 DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu"
      6 #loader = PyPDFDirectoryLoader("aircraft_pdfs")
      7 #docs = loader.load()
      8 #print(len(docs))  # length of all pages together
---> 10 embedding = HuggingFaceInstructEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": DEVICE})

File /opt/conda/lib/python3.11/site-packages/langchain_community/embeddings/huggingface.py:157, in HuggingFaceInstructEmbeddings.__init__(self, **kwargs)
    153     self.client = INSTRUCTOR(
    154         self.model_name, cache_folder=self.cache_folder, **self.model_kwargs
    155     )
    156 except ImportError as e:
--> 157     raise ImportError("Dependencies for InstructorEmbedding not found.") from e

ImportError: Dependencies for InstructorEmbedding not found.

here is the output of pip freeze

transformers==4.37.2
torch==2.2.0
langchain==0.1.6
InstructorEmbedding==1.0.1
...

Solution

  • I think you would also need to install . Try installing it via:

    pip install -U sentence-transformers==2.2.2
    

    and then run your code. Please make sure you install the version 2.2.2 otherwise you'll end up with this error:

    TypeError: INSTRUCTOR._load_sbert_model() got an unexpected keyword argument 'token'
    

    It seems the latest version of sentence-transformers has some compatibility issues.