LangChain升级至0.0.295+后Vertex AI模型抛出AttributeError求助
问题解决:LangChain升级后Vertex AI的AttributeError错误
错误根源
LangChain 0.0.295及以上版本调整了Vertex AI聊天模型(ChatVertexAI)的内部处理逻辑,同时若依赖的google-cloud-aiplatform库版本过低,会导致响应结构不匹配,触发'TextGenerationResponse' object has no attribute 'candidates'错误。
解决方案
方案1:升级依赖的Google云AI平台库
确保google-cloud-aiplatform版本与LangChain新版本兼容:
pip install --upgrade google-cloud-aiplatform
方案2:适配Chat模型的ConversationChain配置
ConversationChain默认prompt针对普通LLM模型,需改用ChatPromptTemplate适配ChatVertexAI:
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain.chains import ConversationChain def __initialize_basic_chain(self, llm, memory): # 构建适配Chat模型的prompt prompt = ChatPromptTemplate.from_messages([ ("system", "以下是人类与AI的友好对话。AI善于交流,会结合上下文给出具体细节;若不知道答案,会如实说明。"), MessagesPlaceholder(variable_name="history"), ("human", "{input}") ]) return ConversationChain(llm=llm, memory=memory, prompt=prompt)
方案3:改用普通LLM模型(若业务允许)
如果无需使用聊天模型chat-bison,可切换为文本模型text-bison并使用VertexAI类:
from langchain.llms import VertexAI case 'public_general_palm': llm = VertexAI(model_name="text-bison", temperature=0) chain = self.__initialize_basic_chain(llm, self.chain_memory) return chain(request.msg, return_only_outputs=True)
方案4:临时降级LangChain版本
若上述方案无法快速生效,可降级到之前稳定运行的版本:
pip install langchain==0.0.294
内容的提问来源于stack exchange,提问作者Desh20
相关产品推荐
相关产品推荐

