langchain: Unable to use gpt4all model

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)

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 7
  • Comments: 27 (6 by maintainers)

Commits related to this issue

Most upvoted comments

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

Same for me, I am having this error as well

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

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.

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

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 ?

thank you !!

As above, I downgraded pygpt4all and it now works:

pip install ‘pygpt4all==v1.0.1’ --force-reinstall

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) `