llama-cpp-python icon indicating copy to clipboard operation
llama-cpp-python copied to clipboard

import llama_cpp ERROR

Open 823863429 opened this issue 2 years ago • 4 comments

After successfully installing llama cpp Python using Python 3.10

Execute import llama_ cpp

Tips: Traceback (most recent call last): File "/root/anaconda3/lib/python3.10/site-packages/langchain/llms/llamacpp.py", line 106, in validate_environment from llama_cpp import Llama ImportError: cannot import name 'Llama' from partially initialized module 'llama_cpp' (most likely due to a circular import) (/mnt/www2/llama/llama_cpp.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/mnt/www2/llama/llama_cpp.py", line 14, in llm = LlamaCpp(model_path="/mnt/www2/llama/ggml-model-q4_0.bin") File "pydantic/main.py", line 339, in pydantic.main.BaseModel.init File "pydantic/main.py", line 1102, in pydantic.main.validate_model File "/root/anaconda3/lib/python3.10/site-packages/langchain/llms/llamacpp.py", line 122, in validate_environment raise ModuleNotFoundError( ModuleNotFoundError: Could not import llama-cpp-python library. Please install the llama-cpp-python library to use this embedding model: pip install llama-cpp-python

823863429 avatar Apr 18 '23 14:04 823863429

Same here when trying to test caching.

gjmulder avatar Apr 18 '23 16:04 gjmulder

@823863429 @gjmulder trying to repro, are you both using this from langchain?

abetlen avatar Apr 18 '23 18:04 abetlen

@abetlen sorry - above is a different error to what I'm getting, as reported in #90

gjmulder avatar Apr 18 '23 21:04 gjmulder

@823863429 @gjmulder尝试重现,你们都在使用 langchain 的这个吗?

Yes,try to use llama-cpp-python with langchain like: from langchain.llms import LlamaCpp from langchain import PromptTemplate, LLMChain template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"]) llm = LlamaCpp(model_path="./ggml-model-q4_0.bin") llm_chain = LLMChain(prompt=prompt, llm=llm) question = "xxxxxx" llm_chain.run(question)

823863429 avatar Apr 19 '23 02:04 823863429

Are you still having the same issue with langchain?

gjmulder avatar May 12 '23 17:05 gjmulder

I'm getting this error while trying to run the code detailed here: https://python.langchain.com/en/latest/modules/models/llms/integrations/llamacpp.html

I've downloaded the model ggml-model-q4_0.bin from here https://huggingface.co/Pi3141/alpaca-native-7B-ggml/tree/397e872bf4c83f4c642317a5bf65ce84a105786e

Here is the full output I'm seeing:

Traceback (most recent call last):
  File "C:\Projects\LangChainPythonTest\env\lib\site-packages\langchain\llms\llamacpp.py", line 122, in validate_environment
    from llama_cpp import Llama
ImportError: cannot import name 'Llama' from partially initialized module 'llama_cpp' (most likely due to a circular import) (c:\Projects\LangChainPythonTest\david\llama_cpp.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Projects\LangChainPythonTest\env\lib\site-packages\langchain\llms\llamacpp.py", line 122, in validate_environment
    from llama_cpp import Llama
  File "c:\Projects\LangChainPythonTest\david\llama_cpp.py", line 20, in <module>
    llm = LlamaCpp(
  File "pydantic\main.py", line 339, in pydantic.main.BaseModel.__init__
  File "pydantic\main.py", line 1102, in pydantic.main.validate_model
  File "C:\Projects\LangChainPythonTest\env\lib\site-packages\langchain\llms\llamacpp.py", line 141, in validate_environment
    raise ModuleNotFoundError(
ModuleNotFoundError: Could not import llama-cpp-python library. Please install the llama-cpp-python library to use this embedding model: pip install llama-cpp-python

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Projects\LangChainPythonTest\david\llama_cpp.py", line 20, in <module>
    llm = LlamaCpp(
  File "pydantic\main.py", line 339, in pydantic.main.BaseModel.__init__
  File "pydantic\main.py", line 1102, in pydantic.main.validate_model
  File "C:\Projects\LangChainPythonTest\env\lib\site-packages\langchain\llms\llamacpp.py", line 141, in validate_environment
    raise ModuleNotFoundError(
ModuleNotFoundError: Could not import llama-cpp-python library. Please install the llama-cpp-python library to use this embedding model: pip install llama-cpp-python

dconlisk avatar May 15 '23 13:05 dconlisk

@dconlisk are you sure you pip installed llama-cpp-python in your environment?

abetlen avatar May 15 '23 15:05 abetlen

Yes @abetlen, here's the output when I run that in my virtual environment:

image

dconlisk avatar May 15 '23 18:05 dconlisk

Today, I am facing the same issue as the one reported by @dconlisk.

oltipreka avatar May 18 '23 15:05 oltipreka

I kind of solved it, though I don't think is the perfect solution. However, I just did the following:

the sys.path.insert(0, <envs\your-environment-name\lib\site-packages>).

Apparently, I don't know for which reason, while working in a virtual env, python was not searching for the llama-cpp-python in site-packages of that virtual env.

oltipreka avatar May 19 '23 11:05 oltipreka

Can we close this bug as it looks like a Python path issue?

gjmulder avatar May 22 '23 11:05 gjmulder

To anyone having this problem, make sure the folder or file name you're importing llama_cpp to is not named llama_cpp or llama_cpp.py.

leoinnocenzi avatar Jun 08 '23 15:06 leoinnocenzi

if you are using mac, you probably have brew installed version of python. try use

brew uninstall --ignore-dependencies [email protected]  

WenchenLi avatar Aug 18 '23 18:08 WenchenLi

I am using windows with python locally installed, I also tried with docker containers. The only thing that worked for me was to upgrade the llama-cpp-python version from 1.77 to 1.78.

amartinez-forrester avatar Sep 11 '23 13:09 amartinez-forrester

try change your python file name llama_cpp.py

adeelhasan19 avatar Sep 25 '23 05:09 adeelhasan19

`ImportError Traceback (most recent call last) 4 frames /usr/local/lib/python3.10/dist-packages/langchain_community/llms/llamacpp.py in validate_environment(cls, values) 143 from llama_cpp import Llama, LlamaGrammar 144 except ImportError: --> 145 raise ImportError( 146 "Could not import llama-cpp-python library. " 147 "Please install the llama-cpp-python library to "

ImportError: Could not import llama-cpp-python library. Please install the llama-cpp-python library to use this embedding model: pip install llama-cpp-python`

I am facing this issue.. Can someone help me .. with colab

sunaarun avatar Jan 13 '24 23:01 sunaarun

Use this to download depemdent files. Works for me after doing this

git clone --recursive -j8 https://github.com/abetlen/llama-cpp-python.git

https://python.langchain.com/docs/integrations/llms/llamacpp

saby2184 avatar Mar 08 '24 20:03 saby2184

To anyone having this problem, make sure the folder or file name you're importing llama_cpp to is not named llama_cpp or llama_cpp.py.

this indeed did the trick. Why is that though? By naming the script llama_cpp_xxxxx.py are we shadowing the LlamaCpp module somehow?

Gr3at avatar Jun 14 '24 13:06 Gr3at