langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Unable to use gpt4all model

Open msinha251 opened this issue 2 years ago • 24 comments

Hi Team,

I am getting below error while trying to use the gpt4all model, Can someone please advice ?

Error:

  File "/home/ubuntu/.local/share/virtualenvs/local-conversational-ai-chatbot-using-gpt4-6TvxabtR/lib/python3.10/site-packages/langchain/llms/gpt4all.py", line 181, in _call
    text = self.client.generate(
TypeError: Model.generate() got an unexpected keyword argument 'new_text_callback'

Code:

from langchain import PromptTemplate, LLMChain
from langchain.llms import GPT4All
from langchain.callbacks.base import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

template = """Question: {question}

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

prompt = PromptTemplate(template=template, input_variables=["question"])

local_path = './models/ggjt-model.bin'

# Callbacks support token-wise streaming
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
# Verbose is required to pass to the callback manager
llm = GPT4All(model=local_path, callback_manager=callback_manager, verbose=True)

llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "What NFL team won the Super Bowl in the year Justin Bieber was born?"

llm_chain.run(question)

msinha251 avatar Apr 30 '23 17:04 msinha251

This is fixed in https://github.com/hwchase17/langchain/pull/3837

npow avatar Apr 30 '23 20:04 npow

@npow I am still getting the error. When is the new release be issued? Also, can you add support for the Gpt4all-J model ?

serop-ba avatar May 03 '23 06:05 serop-ba

Same for me, I am having this error as well

tdnghia98 avatar May 03 '23 12:05 tdnghia98

@npow After updating the code and model, stuck with below error:

Error: Model.__init__() missing 1 required positional argument: 'ggml_model' (type=type_error)

Code:

from langchain import PromptTemplate, LLMChain
from langchain.llms import GPT4All
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

template = """Question: {question}

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

prompt = PromptTemplate(template=template, input_variables=["question"])

local_path = "./models/ggml-gpt4all-l13b-snoozy.bin"

# Callbacks support token-wise streaming
#callbacks = [StreamingStdOutCallbackHandler()]
# Verbose is required to pass to the callback manager
llm = GPT4All(model=local_path, verbose=True)

llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "What NFL team won the Super Bowl in the year Justin Bieber was born?"

llm_chain.run(question)

langchain.__version__
'0.0.157'

Any idea how to fix this ?

msinha251 avatar May 03 '23 12:05 msinha251

can someone please look into this ?

serop-ba avatar May 03 '23 14:05 serop-ba

Same here. It would also be great to add support for GPT4all-J models.

PawelFaron avatar May 03 '23 14:05 PawelFaron

Also getting this error, think a new update must have broke it.

JosephHampton avatar May 03 '23 14:05 JosephHampton

Also getting this error, think a new update must have broke it.

Yeah, it looks like pygpt4all has been updated to work in Interactive mode.

I downgraded pygpt4all and the error was resolved.

pygpt4all commit log

Code for GPT4ALL-J: `"""Wrapper for the GPT4All-J model.""" from functools import partial from typing import Any, Dict, List, Mapping, Optional, Set

from pydantic import Extra, Field, root_validator

from langchain.callbacks.manager import CallbackManagerForLLMRun from langchain.llms.base import LLM from langchain.llms.utils import enforce_stop_tokens

class GPT4All_J(LLM): r"""Wrapper around GPT4All-J language models.

To use, you should have the ``pygpt4all`` python package installed, the
pre-trained model file, and the model's config information.

Example:
    .. code-block:: python

        from langchain.llms import GPT4All_J
        model = GPT4All_J(model="./models/gpt4all-model.bin")

        # Simplest invocation
        response = model("Once upon a time, ")
"""

model: str
"""Path to the pre-trained GPT4All model file."""

n_threads: Optional[int] = Field(4, alias="n_threads")
"""Number of threads to use."""

n_predict: Optional[int] = 256
"""The maximum number of tokens to generate."""

temp: Optional[float] = 0.8
"""The temperature to use for sampling."""

top_p: Optional[float] = 0.95
"""The top-p value to use for sampling."""

top_k: Optional[int] = 40
"""The top-k value to use for sampling."""

echo: Optional[bool] = False
"""Whether to echo the prompt."""

stop: Optional[List[str]] = []
"""A list of strings to stop generation when encountered."""

repeat_last_n: Optional[int] = 64
"Last n tokens to penalize"

repeat_penalty: Optional[float] = 1.3
"""The penalty to apply to repeated tokens."""

n_batch: int = Field(1, alias="n_batch")
"""Batch size for prompt processing."""

streaming: bool = False
"""Whether to stream the results or not."""

client: Any = None  #: :meta private:

class Config:
    """Configuration for this pydantic object."""

    extra = Extra.forbid

@property
def _default_params(self) -> Dict[str, Any]:
    """Get the identifying parameters."""
    return {
        "seed": self.seed,
        "n_predict": self.n_predict,
        "n_threads": self.n_threads,
        "n_batch": self.n_batch,
        "repeat_last_n": self.repeat_last_n,
        "repeat_penalty": self.repeat_penalty,
        "top_k": self.top_k,
        "top_p": self.top_p,
        "temp": self.temp,
    }

@staticmethod
def _llama_param_names() -> Set[str]:
    """Get the identifying parameters."""
    return {}

@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
    """Validate that the python package exists in the environment."""
    try:
        from pygpt4all.models.gpt4all_j import GPT4All_J as GPT4AllModel
        
        llama_keys = cls._llama_param_names()
        model_kwargs = {k: v for k, v in values.items() if k in llama_keys}
        values["client"] = GPT4AllModel(
            model_path=values["model"],
            **model_kwargs,
        )

    except ImportError:
        raise ValueError(
            "Could not import pygpt4all python package. "
            "Please install it with `pip install pygpt4all`."
        )
    return values

@property
def _identifying_params(self) -> Mapping[str, Any]:
    """Get the identifying parameters."""
    return {
        "model": self.model,
        **self._default_params,
        **{
            k: v
            for k, v in self.__dict__.items()
            if k in GPT4All_J._llama_param_names()
        },
    }

@property
def _llm_type(self) -> str:
    """Return the type of llm."""
    return "gpt4all"

def _call(
    self,
    prompt: str,
    stop: Optional[List[str]] = None,
    run_manager: Optional[CallbackManagerForLLMRun] = None,
) -> str:
    r"""Call out to GPT4All's generate method.

    Args:
        prompt: The prompt to pass into the model.
        stop: A list of strings to stop generation when encountered.

    Returns:
        The string generated by the model.

    Example:
        .. code-block:: python

            prompt = "Once upon a time, "
            response = model(prompt, n_predict=55)
    """
    if run_manager:
        text_callback = partial(run_manager.on_llm_new_token, verbose=self.verbose)
        # modify self.client.generate self.client.cpp_generate
        text = self.client.cpp_generate(
            prompt,
            new_text_callback=text_callback
        )
    else:
        text = self.client.generate(prompt)
    if stop is not None:
        text = enforce_stop_tokens(text, stop)
    return text

Run LLMChain

from langchain import PromptTemplate, LLMChain from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

template = """Question: {question}

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

prompt = PromptTemplate(template=template, input_variables=["question"])

callbacks = [StreamingStdOutCallbackHandler()] llm = GPT4All_J(model='./ggml-gpt4all-j-v1.3-groovy.bin', callbacks=callbacks, verbose=True) llm_chain = LLMChain(prompt=prompt, llm=llm) question = "What NFL team won the Super Bowl in the year Justin Bieber was born?" llm_chain.run(question) `

isichan0501 avatar May 03 '23 23:05 isichan0501

As above, I downgraded pygpt4all and it now works:

pip install 'pygpt4all==v1.0.1' --force-reinstall

paulroberttaylor avatar May 04 '23 06:05 paulroberttaylor

@paulroberttaylor does it work with GPT4all-J? or is it not supported yet ?

serop-ba avatar May 04 '23 06:05 serop-ba

I haven't tried it, only ggml-gpt4all-l13b-snoozy.bin

paulroberttaylor avatar May 04 '23 06:05 paulroberttaylor

thank you !!

serop-ba avatar May 04 '23 06:05 serop-ba

@paulroberttaylor I did that and I no longer get errors but running the chain now takes ages, I have been waiting 15 minutes and nothing came out. Using Gpt4all directly from pygpt4all is much quicker so it is not hardware problem (I'm running it on google collab)

llm_chain = LLMChain(prompt=prompt, llm=llm) question = "What NFL team won the Super Bowl in the year Justin Bieber was born?" llm_chain.run(question)

Do you have any idea what the issue is now?

serop-ba avatar May 04 '23 07:05 serop-ba

I'm running locally, but the reply started instantly for me.

Maybe Collab kernel needs to be restarted if it isn't a new one?

paulroberttaylor avatar May 04 '23 07:05 paulroberttaylor

I did restart it but there seem to be a problem still

serop-ba avatar May 04 '23 07:05 serop-ba

Was out and about when I wrote my ealier post. This is the code I used:

`from langchain import PromptTemplate, LLMChain from langchain.llms import GPT4All from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler #from pygpt4all import GPT4All

template = """Question: {question}

Answer: """

prompt = PromptTemplate(template=template, input_variables=["question"])

local_path = './models/ggml-gpt4all-l13b-snoozy.bin' callbacks = [StreamingStdOutCallbackHandler()] llm = GPT4All(model=local_path, callbacks=callbacks, verbose=True) llm_chain = LLMChain(prompt=prompt, llm=llm)

print(llm_chain.run("What NFL team won the Super Bowl in the year Justin Bieber was born?"))`

And the answer it gave:

` Question: What NFL team won the Super Bowl in the year Justin Bieber was born?

Answer: The New York Giants (Superbowl XLVI)`

paulroberttaylor avatar May 04 '23 08:05 paulroberttaylor

@vowelparrot I created pull request to solve this issue and add support for GPT4All_J if you have a time to review it: https://github.com/hwchase17/langchain/pull/4131

PawelFaron avatar May 04 '23 21:05 PawelFaron

@PawelFaron Thank you so much for the pull request !!

serop-ba avatar May 05 '23 06:05 serop-ba

I'll review today. Thanks for your patience everyone - lots of changes in dependent libs!

vowelparrot avatar May 05 '23 15:05 vowelparrot

I still have this problem in my environment. Can someone please advise me on this?

Error:

1 validation error for GPT4All
__root__
  Model.__init__() missing 1 required positional argument: 'ggml_model' (type=type_error)
  File "/home/***/***/GPT4ALL/main_gpt4all_lang.py", line 15, in <module>
    llm = GPT4All(model=local_path, callbacks=callbacks, verbose=True)
pydantic.error_wrappers.ValidationError: 1 validation error for GPT4All
__root__
  Model.__init__() missing 1 required positional argument: 'ggml_model' (type=type_error)

Code:

from langchain import PromptTemplate, LLMChain
from langchain.llms import GPT4All
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

template = """Question: {question}

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

prompt = PromptTemplate(template=template, input_variables=["question"])

local_path = './GPT4ALL/models/ggml-model-q4_0.bin'

callbacks = [StreamingStdOutCallbackHandler()]

llm = GPT4All(model=local_path, callbacks=callbacks, verbose=True)

llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "What NFL team won the Super Bowl in the year Justin Bieber was born?"

llm_chain.run(question)

langchain.version '0.0.165' pygpt4all 1.1.0

@npow After updating the code and model, stuck with below error:

Error: Model.__init__() missing 1 required positional argument: 'ggml_model' (type=type_error)

Code:

from langchain import PromptTemplate, LLMChain
from langchain.llms import GPT4All
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

template = """Question: {question}

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

prompt = PromptTemplate(template=template, input_variables=["question"])

local_path = "./models/ggml-gpt4all-l13b-snoozy.bin"

# Callbacks support token-wise streaming
#callbacks = [StreamingStdOutCallbackHandler()]
# Verbose is required to pass to the callback manager
llm = GPT4All(model=local_path, verbose=True)

llm_chain = LLMChain(prompt=prompt, llm=llm)

question = "What NFL team won the Super Bowl in the year Justin Bieber was born?"

llm_chain.run(question)
langchain.__version__
'0.0.157'

Any idea how to fix this ?

ksw-1024 avatar May 11 '23 15:05 ksw-1024

gpt4all moved repo with python bindings in their main repo these imports should be changed to gpt4all (and mentioning in the comments as well) pygpt4all is still alive tho...

Chae4ek avatar May 12 '23 00:05 Chae4ek

@Chae4ek Thank you for your comment. I don't have a deep understanding of LLM and LangChain yet, but just rewriting "pygpt4all" as "gpt4all" won't work, right? (because it calls itself). What would be the best solution?

Sorry for the elementary question.

ksw-1024 avatar May 12 '23 00:05 ksw-1024

Thanks for raising this issue, everyone. The GPT4All "official" bindings have changed three times in as many weeks - I'd be happy to review a PR updating to the imports to whatever is being used now

vowelparrot avatar May 12 '23 07:05 vowelparrot

I tried it in my environment and it worked fine. Thanks for your quick response.

ksw-1024 avatar May 12 '23 12:05 ksw-1024

I got it to work by downgrading gpt4all to version 0.3.6 then the bindings match up. Otherwise, 1.0.1 for instance gpt4all does not work and throws the n_ctx error.

james-see avatar Jul 03 '23 03:07 james-see

pip install 'pygpt4all==v1.0.1' --force-reinstall

Hi there everyone. Sorry for the total noob question. When I run this, it uninstalls a huge pile of stuff and then halts some part through the installation and says it can't go further because it wants pandas version between 1 and 2.0, and I have 2.0.3. Now I've basically broken all the dependencies as a result. Does someone here have a "requirements.txt" file that I can reliably run and install all the dependencies in one batch?

Oh, and it also still complains about the LLM file saying its not valid ... I'm running langchain under privateGPT.py (also on github)

I checked the file (groovy.bin) and its md5 is correct at 81a09a0ddf89690372fc296ff7f625af

Any help gratefully received.

Ubuntu Server 22.04.2 LTS, python 3.10.

Screen Shot 2023-07-29 at 13 03 31

JohnOstrowick avatar Jul 29 '23 11:07 JohnOstrowick

pip install 'pygpt4all==v1.0.1' --force-reinstall

Thank you! Worked for me, too.

jaybo10011001 avatar Aug 24 '23 15:08 jaybo10011001