Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'GPT4All' object has no attribute 'model_type' (langchain 0.0.190) #5720

Closed
2 of 14 tasks
christoph-daehne opened this issue Jun 5, 2023 · 7 comments · Fixed by #5743
Closed
2 of 14 tasks

Comments

@christoph-daehne
Copy link

christoph-daehne commented Jun 5, 2023

System Info

Hi, this is related to #5651 but (on my machine ;) ) the issue is still there.

Versions

Who can help?

@pakcheera @bwv988 First of all: thanks for the report and the fix :). Did this issues disappear on you machines?

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

Error message

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /Users/christoph/src/sandstorm-labs/development-tools/ai-support-chat/gpt4all/chat.py:30 in      │
│ <module>                                                                                         │
│                                                                                                  │
│   27 │   model_name="all-mpnet-base-v2")                                                         │
│   28                                                                                             │
│   29 # see https://gpt4all.io/models/ggml-mpt-7b-instruct.bin                                    │
│ ❱ 30 llm = GPT4All(                                                                              │
│   31 │   model="./ggml-mpt-7b-instruct.bin",                                                     │
│   32 │   #backend='gptj',                                                                        │
│   33 │   top_p=0.5,                                                                              │
│                                                                                                  │
│ in pydantic.main.BaseModel.__init__:339                                                          │
│                                                                                                  │
│ in pydantic.main.validate_model:1102                                                             │
│                                                                                                  │
│ /Users/christoph/src/sandstorm-labs/development-tools/ai-support-chat/gpt4all/venv/lib/python3.1 │
│ 1/site-packages/langchain/llms/gpt4all.py:156 in validate_environment                            │
│                                                                                                  │
│   153 │   │   if values["n_threads"] is not None:                                                │
│   154 │   │   │   # set n_threads                                                                │
│   155 │   │   │   values["client"].model.set_thread_count(values["n_threads"])                   │
│ ❱ 156 │   │   values["backend"] = values["client"].model_type                                    │
│   157 │   │                                                                                      │
│   158 │   │   return values                                                                      │
│   159                                                                                            │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯

As you can see in gpt4all.py:156 contains the changed from the fix of #5651

Code

from langchain.llms import GPT4All

# see https://gpt4all.io/models/ggml-mpt-7b-instruct.bin
llm = GPT4All(
    model="./ggml-mpt-7b-instruct.bin",
    #backend='gptj',
    top_p=0.5,
    top_k=0,
    temp=0.1,
    repeat_penalty=0.8,
    n_threads=12,
    n_batch=16,
    n_ctx=2048)

FYI I am following this example in a blog post.

Expected behavior

I expect an instance of GPT4All instead of a stacktrace.

@bwv988
Copy link
Contributor

bwv988 commented Jun 5, 2023

Hey @christoph-daehne, I just downloaded the model and ran your code, but can't reproduce your error. For me the model loads just fine in langchain-0.0.190:

!pip install -U langchain
Requirement already satisfied: langchain in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (0.0.189)
Collecting langchain
  Downloading langchain-0.0.190-py3-none-any.whl (983 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 983.2/983.2 kB 128.8 kB/s eta 0:00:00m eta 0:00:01[36m0:00:01
Requirement already satisfied: PyYAML>=5.4.1 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from langchain) (6.0)
Requirement already satisfied: SQLAlchemy<3,>=1.4 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from langchain) (2.0.15)
Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from langchain) (3.8.4)
Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from langchain) (0.5.7)
Requirement already satisfied: numexpr<3.0.0,>=2.8.4 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from langchain) (2.8.4)
Requirement already satisfied: numpy<2,>=1 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from langchain) (1.24.3)
Requirement already satisfied: openapi-schema-pydantic<2.0,>=1.2 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from langchain) (1.2.4)
Requirement already satisfied: pydantic<2,>=1 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from langchain) (1.10.8)
Requirement already satisfied: requests<3,>=2 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from langchain) (2.29.0)
Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from langchain) (8.2.2)
Requirement already satisfied: attrs>=17.3.0 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (22.1.0)
Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (2.0.4)
Requirement already satisfied: multidict<7.0,>=4.5 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)
Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (4.0.2)
Requirement already satisfied: yarl<2.0,>=1.0 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.2)
Requirement already satisfied: frozenlist>=1.1.1 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.3)
Requirement already satisfied: aiosignal>=1.1.2 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)
Requirement already satisfied: marshmallow<4.0.0,>=3.3.0 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (3.19.0)
Requirement already satisfied: marshmallow-enum<2.0.0,>=1.5.1 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (1.5.1)
Requirement already satisfied: typing-inspect>=0.4.0 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (0.9.0)
Requirement already satisfied: typing-extensions>=4.2.0 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from pydantic<2,>=1->langchain) (4.5.0)
Requirement already satisfied: idna<4,>=2.5 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from requests<3,>=2->langchain) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from requests<3,>=2->langchain) (1.26.15)
Requirement already satisfied: certifi>=2017.4.17 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from requests<3,>=2->langchain) (2023.5.7)
Requirement already satisfied: greenlet!=0.4.17 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from SQLAlchemy<3,>=1.4->langchain) (2.0.2)
Requirement already satisfied: packaging>=17.0 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from marshmallow<4.0.0,>=3.3.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (23.0)
Requirement already satisfied: mypy-extensions>=0.3.0 in /home/sral/opt/miniconda3/envs/gpt4all/lib/python3.11/site-packages (from typing-inspect>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (1.0.0)
Installing collected packages: langchain
  Attempting uninstall: langchain
    Found existing installation: langchain 0.0.189
    Uninstalling langchain-0.0.189:
      Successfully uninstalled langchain-0.0.189
Successfully installed langchain-0.0.190
from langchain.llms import GPT4All

# see https://gpt4all.io/models/ggml-mpt-7b-instruct.bin
llm = GPT4All(
    model="/home/sral/models/gpt4all/ggml-mpt-7b-instruct.bin",
    #backend='gptj',
    top_p=0.5,
    top_k=0,
    temp=0.1,
    repeat_penalty=0.8,
    n_threads=12,
    n_batch=16,
    n_ctx=2048)
Found model file at  /home/sral/models/gpt4all/ggml-mpt-7b-instruct.bin
mpt_model_load: loading model from '/home/sral/models/gpt4all/ggml-mpt-7b-instruct.bin' - please wait ...
mpt_model_load: n_vocab        = 50432
mpt_model_load: n_ctx          = 2048
mpt_model_load: n_embd         = 4096
mpt_model_load: n_head         = 32
mpt_model_load: n_layer        = 32
mpt_model_load: alibi_bias_max = 8.000000
mpt_model_load: clip_qkv       = 0.000000
mpt_model_load: ftype          = 2
mpt_model_load: ggml ctx size = 5653.09 MB
mpt_model_load: kv self size  = 1024.00 MB
mpt_model_load: ........................ done
mpt_model_load: model size =  4629.02 MB / num tensors = 194

@45Matches
Copy link

I have the same issue as @christoph-daehne

% python privateGPT.py
Found model file.
gptj_model_load: loading model from 'models/ggml-gpt4all-j-v1.3-groovy.bin' - please wait ...
gptj_model_load: n_vocab = 50400
gptj_model_load: n_ctx   = 2048
gptj_model_load: n_embd  = 4096
gptj_model_load: n_head  = 16
gptj_model_load: n_layer = 28
gptj_model_load: n_rot   = 64
gptj_model_load: f16     = 2
gptj_model_load: ggml ctx size = 5401.45 MB
gptj_model_load: kv self size  =  896.00 MB
gptj_model_load: ................................... done
gptj_model_load: model size =  3609.38 MB / num tensors = 285
Traceback (most recent call last):
  File "/Users/user/.pyenv/versions/3.11.3/envs/vikings/privateGPT/privateGPT.py", line 76, in <module>
    main()
  File "/Users/user/.pyenv/versions/3.11.3/envs/vikings/privateGPT/privateGPT.py", line 36, in main
    llm = GPT4All(model=model_path, n_ctx=model_n_ctx, backend='gptj', callbacks=callbacks, verbose=False)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pydantic/main.py", line 339, in pydantic.main.BaseModel.__init__
  File "pydantic/main.py", line 1102, in pydantic.main.validate_model
  File "/Users/user/.pyenv/versions/vikings/lib/python3.11/site-packages/langchain/llms/gpt4all.py", line 156, in validate_environment
    values["backend"] = values["client"].model_type
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'GPT4All' object has no attribute 'model_type'

When I change directly model_type='gptj' in GPT4AllModel() init, the code works for me (i get the expected Enter a query: prompt, but not with the backend / client.model_type approach.

From Line 150 of gpt4all.py

        values["client"] = GPT4AllModel(
            model_name,
            model_path=model_path or None,
            model_type='gptj',
            allow_download=values["allow_download"],
        )
        # if values["n_threads"] is not None:
        #     # set n_threads
        #     values["client"].model.set_thread_count(values["n_threads"])
        # values["backend"] = values["client"].model_type
Screenshot 2023-06-05 at 11 04 34 pm

@bwv988
Copy link
Contributor

bwv988 commented Jun 5, 2023

@christoph-daehne @dotandpixel I think I figured it out.

After creating a new python environment from scratch and running the test code, I was able to reproduce the issue.

As it turns out, GPT4All's python bindings, which Langchain's GPT4All LLM code wraps, have changed in a subtle way, however the change is as of yet unreleased. I am on the latest version from https://github.com/nomic-ai/gpt4all which probably explains why I encountered the problem seen in #5651 to begin with.

However, since the new code in GPT4All is unreleased, my fix has created a scenario where Langchain's GPT4All wrapper has become incompatible with the currently released version of GPT4All.

I will submit another pull request to turn this into a backwards-compatible change. In the meantime, you should be able to get your code to run by upgrading to a newer version of GPT4All's Python bindings.

Further technical detail

The issue is with GPT4All and the semantics of model_type. In particular, the older version (0.2.3) doesn't have a model_type attribute, just a model_type argument for the constructor. Instead, the model type is accessible via model.model_type:

class GPT4All():                                                                                                                                                                                   
    """Python API for retrieving and interacting with GPT4All models.                                                                                                                              
                                                                                                                                                                                                   
    Attribuies:                                                                                                                                                                                    
        model: Pointer to underlying C model.                                                                                                                                                      
    """                                                                                                                                                                                            
                                                                                                                                                                                                   
    def __init__(self, model_name: str, model_path: str = None, model_type: str = None, allow_download=True):                                                                                      
        """                                                                                                                                                                                        
        Constructor                                                                                                                                                                                
                                                                                                                                                                                                   
        Args:                                                                                                                                                                                      
            model_name: Name of GPT4All or custom model. Including ".bin" file extension is optional but encouraged.                                                                               
            model_path: Path to directory containing model file or, if file does not exist, where to download model.                                                                               
                Default is None, in which case models will be stored in `~/.cache/gpt4all/`.                                                                                                       
            model_type: Model architecture to use - currently, options are 'llama', 'gptj', or 'mpt'. Only required if model                                                                       
                is custom. Note that these models still must be built from llama.cpp or GPTJ ggml architecture.                                                                                    
                Default is None.                                                                                                                                                                   
            allow_download: Allow API to download models from gpt4all.io. Default is True.                                                                                                         
        """                                                                                                                                                                                        
        self.model = None                                                                                                                                                                          
                                                                                                                                                                                                   
        # Model type provided for when model is custom                                                                                                                                             
        if model_type:                                                                                                                                                                             
            self.model = GPT4All.get_model_from_type(model_type)                                                                                                                                   
        # Else get model from gpt4all model filenames                                                                                                                                              
        else:                                                                                                                                                                                      
            self.model = GPT4All.get_model_from_name(model_name)

In contrast to this, the newer, unreleased version uses a model_type attribute:

class GPT4All():
    """Python API for retrieving and interacting with GPT4All models.
    
    Attribuies:
        model: Pointer to underlying C model.
    """

    def __init__(self, model_name: str, model_path: str = None, model_type: str = None, allow_download = True):
        """
        Constructor

        Args:
            model_name: Name of GPT4All or custom model. Including ".bin" file extension is optional but encouraged.
            model_path: Path to directory containing model file or, if file does not exist, where to download model.
                Default is None, in which case models will be stored in `~/.cache/gpt4all/`.
            model_type: Model architecture. This argument currently does not have any functionality and is just used as
                descriptive identifier for user. Default is None.
            allow_download: Allow API to download models from gpt4all.io. Default is True. 
        """
        self.model_type = model_type
        self.model = pyllmodel.LLModel()
        # Retrieve model and download if allowed
        model_dest = self.retrieve_model(model_name, model_path=model_path, allow_download=allow_download)
        self.model.load_model(model_dest)

@christoph-daehne
Copy link
Author

@bwv988 Awesome :) What a nasty-to-find little detail. Thank you very much. I will try it out

@GeminiAgaloos
Copy link

i was able to make it work with:
langchain==0.0.190
gpt4all==0.3.0

hwchase17 pushed a commit that referenced this issue Jun 5, 2023
Fixes #5720.

A more in-depth discussion is in my comment here:
#5720 (comment)

In a nutshell, there has been a subtle change in the latest version of
GPT4Alls Python bindings. The change I submitted yesterday is compatible
with this version, however, this version is as of yet unreleased and
thus the code change breaks Langchain's wrapper under the currently
released version of GPT4All.

This pull request proposes a backwards-compatible solution.
@45Matches
Copy link

@bwv988 Thank you Ralph for the detailed and speedy response. I'll check it out.

@45Matches
Copy link

In gpt4all.py updated to the above, with the following result. New to this so unsure what that means.

Replaced

        self.model = None

        # Model type provided for when model is custom
        if model_type:
            self.model = GPT4All.get_model_from_type(model_type)
        # Else get model from gpt4all model filenames
        else:
            self.model = GPT4All.get_model_from_name(model_name)

for this

        self.model_type = model_type
        self.model = pyllmodel.LLModel()

with this result

% python privateGPT.py
Found model file.
zsh: segmentation fault  python privateGPT.py

Undertone0809 pushed a commit to Undertone0809/langchain that referenced this issue Jun 19, 2023
…. (langchain-ai#5743)

Fixes langchain-ai#5720.

A more in-depth discussion is in my comment here:
langchain-ai#5720 (comment)

In a nutshell, there has been a subtle change in the latest version of
GPT4Alls Python bindings. The change I submitted yesterday is compatible
with this version, however, this version is as of yet unreleased and
thus the code change breaks Langchain's wrapper under the currently
released version of GPT4All.

This pull request proposes a backwards-compatible solution.
This was referenced Jun 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants