使用LangChain调用GPT4All时出现AttributeError及ValidationError求助
LangChain + GPT4All 运行报错解决方法
问题场景
本地测试LangChain结合GPT4All时,运行以下代码:
from langchain import PromptTemplate, LLMChain from langchain.llms import GPT4All from langchain.callbacks.base import CallbackManager from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler local_path = './models/gpt4all-converted.bin' callback_manager = CallbackManager([StreamingStdOutCallbackHandler()]) template = """Question: {question} Answer: Let's think step by step. """ prompt = PromptTemplate(template=template, input_variables=["question"]) 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?" # question = input("Enter your question: ") llm_chain.run(question)
运行后出现报错:
Exception ignored in: <function Model.__del__ at 0x7f45417143a0> Traceback (most recent call last): File "/home/chirag/.local/lib/python3.8/site-packages/pyllamacpp/model.py", line 336, in __del__ if self._ctx: AttributeError: 'Model' object has no attribute '_ctx' Traceback (most recent call last): File "basic_langchain_setup.py", line 16, in <module> llm = GPT4All(model=local_path, File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__ pydantic.error_wrappers.ValidationError: 1 validation error for GPT4All __root__ __init__() got an unexpected keyword argument 'ggml_model' (type=type_error)
解决方法
1. 版本兼容调整
报错核心是LangChain与GPT4All/pyllamacpp版本不匹配,LangChain的GPT4All封装类调用了底层库未定义的ggml_model参数。执行以下命令安装兼容版本:
pip uninstall langchain gpt4all pyllamacpp -y pip install langchain==0.0.196 gpt4all==0.2.3
2. 参数与路径验证
- 确认代码中
local_path指向的模型文件路径正确,文件完整无损坏; - 若后续升级版本,注意LangChain的GPT4All类参数变化,新版本可能需改用
model_path替代model参数。
内容的提问来源于stack exchange,提问作者Chirag Jain
相关产品推荐
相关产品推荐

