· gpt4all langchain til generative-ai

LangChain: 1 validation error for LLMChain - value is not a valid dict (type=type_error.dict)

I surely can’t be the first to make the mistake that I’m about to describe and I expect I won’t be the last! I’m still swimming in the LLM waters and I was trying to get GPT4All to play nicely with LangChain.

I wrote the following code to create an LLM chain in LangChain so that every question would use the same prompt template:

from langchain import PromptTemplate, LLMChain
from gpt4all import GPT4All

llm = GPT4All(
    model_name="ggml-gpt4all-j-v1.3-groovy",
    model_path="/Users/markhneedham/Library/Application Support/nomic.ai/GPT4All/"
)

template = """
You are a friendly chatbot assistant that responds in a conversational
manner to users questions. Keep the answers short, unless specifically
asked by the user to elaborate on something.

Question: {question}

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

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

When I ran this code, I got the following error:

Output
Traceback (most recent call last):
  File "/Users/markhneedham/projects/docs-bot/gpt4all_chain.py", line 26, in <module>
    llm_chain = LLMChain(prompt=prompt, llm=llm)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/markhneedham/projects/docs-bot/env/lib/python3.11/site-packages/langchain/load/serializable.py", line 65, in __init__
    super().__init__(**kwargs)
  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for LLMChain
llm
  value is not a valid dict (type=type_error.dict)

It took me a long time to work out that the issue is that the value we’ve passed in as llm is invalid as it doesn’t extend LangChain’s BaseLanguageModel class. And the reason for that is that I accidentally passed in the GPT4All class, rather than the one with the same name from the LangChain library. Let’s fix that:

from langchain.llms import GPT4All

llm = GPT4All(
    model="/Users/markhneedham/Library/Application Support/nomic.ai/GPT4All/ggml-gpt4all-j-v1.3-groovy",
)

And now we can run that code without any errors!

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket