LangChain LCEL链构建报错:不支持dict与NoneType的|运算
问题分析与解决
错误的核心原因是build_collection_vector_find_prompt_v2()函数没有返回有效的PromptTemplate实例:
- 函数内部仅拼接了prompt字符串,但未将其包装为
PromptTemplate对象并返回,导致该函数实际返回None - LCEL链式调用中,
dict | None的操作不被支持,因此触发TypeError
修复方法
修改build_collection_vector_find_prompt_v2()函数,在末尾创建并返回PromptTemplate实例:
def build_collection_vector_find_prompt_v2() -> PromptTemplate: prompt = ( get_helpful_assistant_prefix() + """ Based on the provided summary Generate a JSON object containing one or more of the following keys: seg_1, seg_2, seg_3, seg_4, seg_5, . . . Return ONLY this list of JSON objects.""" + """ PATH SEGMENT VALUES: {PathSegmentValues} USER INFORMATION SUMMARY: {UserInformationSummary} """ ) # 将拼接好的字符串转为PromptTemplate实例并返回 return PromptTemplate.from_template(prompt)
这样函数会返回正确的PromptTemplate对象,LCEL的链式调用dict | PromptTemplate | ...就能正常执行。
内容的提问来源于stack exchange,提问作者devinbost
相关产品推荐
相关产品推荐

