Langchain-huggingface

Hello, the langchain x huggingface framework seems perfect for what my team is trying to accomplish.

I installed langchain-huggingface with pip3 in a venv and following this guide, Hugging Face x LangChain : A new partner package I created a module like this but with a llma3 model:

from langchain_huggingface import HuggingFacePipeline

llm = HuggingFacePipeline.from_model_id(
    model_id="microsoft/Phi-3-mini-4k-instruct",
    task="text-generation",
    pipeline_kwargs={
        "max_new_tokens": 100,
        "top_k": 50,
        "temperature": 0.1,
    },
)
llm.invoke("Hugging Face is")

I get the following error when I try to run the script:

ModuleNotFoundError: No module named ‘langchain_huggingface’

I must be missing something or doing something very basic wrong.

1 Like

Hey @marky2376,

I’m the author of the blog post, I’ll try to help you.

I just retested the install and the import and both worked.

“ModuleNotFoundError” are usually errors when you are not executing the code in the same environment as where you installed the library.

Can you see langchain_huggingface in your !pip3 list ?

A second option is that you had a conflict in the installation of the library that you did not see ( the requirements of the libs include the latest version of tokenizers which might enter in conflict with other libs )

I tried installing both inside and outside the venv, here is the list for both:

mneely@llmmaximus:/usr/projects/langface$ !pip3 list
pip3 install langchain-huggingface list
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: langchain-huggingface in /usr/local/lib/python3.10/dist-packages (0.0.3)
ERROR: Could not find a version that satisfies the requirement list (from versions: none)
ERROR: No matching distribution found for list

(.venv) mneely@llmmaximus:/usr/projects/langface$ !pip3 list
pip3 install langchain-huggingface list list list
Collecting langchain-huggingface
Using cached langchain_huggingface-0.0.3-py3-none-any.whl (17 kB)
ERROR: Could not find a version that satisfies the requirement list (from versions: none)
ERROR: No matching distribution found for list
(.venv) mneely@llmmaximus:/usr/projects/langface$

Alright, you have typos in your shared logs :

You have pip3 install langchain-huggingface list list list

Outside of the error from typo, I can see that:
outside the env, the latest version ( 0.0.3 ) is installed :wink:

And inside it’s being reinstalled from cache because you most likely uninstalled before :

So, can you now give me the output of pip list ?

Or alternatively do the following :

  1. enter python
  2. test the import
joffreythomas@MBP-de-joffrey  % python                        
Python 3.11.6 (main, Oct  2 2023, 13:45:54) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from langchain_huggingface import HuggingFacePipeline
>>> 

I’m just typing “!pip3 list” and that command is out putting “list” multiple times. Maybe because I have attempted ~3 installs in my troubleshooting? I have never used that list command before so not sure.

(.venv) mneely@llmmaximus:/usr/projects/langface$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

from langchain_huggingface import HuggingFacePipeline
Traceback (most recent call last):
File “”, line 1, in
ModuleNotFoundError: No module named ‘langchain_huggingface’

No, if I try outside the venv, I get what you have:

from langchain_huggingface import HuggingFacePipeline

So I actually tried running my script again outside the venv, which I thought I had before. It executes and finds the package. (I get another error).

What am I missing from being able to run this in a venv?

Pip list is a command that will list all your installed packages.

Alright, so outside the env it is installed.

Once in, you can :

  1. Unintall pip uninstall langchain-huggingface
  2. Re-install pip install langchain-huggingface
  3. verify installation
joffreythomas@MBP-de-joffrey test % pip show langchain-huggingface
Name: langchain-huggingface
Version: 0.0.3
Summary: An integration package connecting Hugging Face and LangChain
Home-page: https://github.com/langchain-ai/langchain
Author: 
Author-email: 
License: MIT
Location: /opt/homebrew/lib/python3.11/site-packages
Requires: huggingface-hub, langchain-core, sentence-transformers, tokenizers, transformers
Required-by: 

If you have a problem during the installation process, please copy the log.

1 Like

Was having the same issue and scratched my head for a while, solution was pretty simple. The solution is to install jupyter from within the activated venv. So,

  1. activate environment
    at any time) install all your required library like langchain_huggingface
  2. install jupyter notebook
  3. run jupyter notebook

Very nice. I ended up not using a venv at all for now. I have gotten pretty far on my journey, and need to go back and reinstall all my packages within a venv. Getting my application to run as a service is near impossible without it being in venv, so that’s my next major step. Not using juputer, so I will have to figure it out as I go.