news 2026/5/26 2:18:23

LangChain 1.3 完全教程:从入门到精通 - Part 5: LCEL(LangChain Expression Language)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
LangChain 1.3 完全教程:从入门到精通 - Part 5: LCEL(LangChain Expression Language)

Part 5: LCEL(LangChain Expression Language)

文章目录

    • Part 5: LCEL(LangChain Expression Language)
      • 5.1 LCEL 概述
      • 5.2 Runnable 接口详解
        • 所有方法表格
        • 完整 Demo
        • with_retry 和 with_fallbacks
      • 5.3 管道操作符 |
        • 数据流动图
      • 5.4 RunnablePassthrough
      • 5.5 RunnableParallel
      • 5.6 RunnableLambda
      • 5.7 RunnableBranch
      • 5.8 配置与运行时修改
      • 5.9 错误处理
      • 5.10 LCEL 最佳实践

5.1 LCEL 概述

LCEL 是 LangChain 的核心创新,用声明式语法组合各种组件。

类比:LCEL 就像 Linux 管道cat file | grep "error" | sort,在 LCEL 中:prompt | model | parser

Runnable
(核心接口)

统一方法签名

可组合组件

invoke / batch / stream

ainvoke / abatch / astream

RunnablePassthrough

RunnableLambda

RunnableParallel

RunnableBranch


5.2 Runnable 接口详解

所有方法表格
方法说明同步/异步
invoke(input)单次调用同步
batch(inputs)批量调用同步
stream(input)流式调用同步
ainvoke(input)单次调用异步
abatch(inputs)批量调用异步
astream(input)流式调用异步
pick(keys)选择特定字段-
assign(mapping)添加字段-
map()对列表元素逐一应用-
with_fallbacks(fallbacks)设置备用方案-
with_retry()设置重试策略-
with_config(config)设置运行时配置-
完整 Demo
importasynciofromdotenvimportload_dotenv load_dotenv()fromlangchain_openaiimportChatOpenAIfromlangchain_core.promptsimportChatPromptTemplatefromlangchain_core.output_parsersimportStrOutputParserfromlangchain_core.runnablesimportRunnablePassthrough,RunnableParallel prompt=ChatPromptTemplate.from_template("用一句话解释{concept}")model=ChatOpenAI(model="gpt-4o-mini",temperature=0.7)chain=prompt|model|StrOutputParser()# 1. invokeresult=chain.invoke({"concept":"量子计算"})print(result)# 2. batchresults=chain.batch([{"concept":"机器学习"},{"concept":"深度学习"}])forrinresults:print(r)# 3. streamforchunkinchain.stream({"concept":"AI"}):print(chunk,end="",flush=True)print()# 4-6. 异步方法asyncdefasync_demo():result=awaitchain.ainvoke({"concept":"区块链"})print(result)results=awaitchain.abatch([{"concept":"云计算"}])asyncforchunkinchain.astream({"concept":"物联网"}):print(chunk,end="",flush=True)print()asyncio.run(async_demo())# 7. pickparallel=RunnableParallel(concept=RunnablePassthrough(),explanation=chain,)picked=parallel.pick("explanation")print(picked.invoke("Python"))# 8. assignassigned=parallel.assign(length=lambdax:len(x["explanation"]))result=assigned.invoke("Python")print(f"长度:{result['length']}")# 9. mapsimple=ChatPromptTemplate.from_template("用2个字概括{c}")|model|StrOutputParser()mapped=simple.map()results=mapped.invoke(["Python","Java","Go"])
with_retry 和 with_fallbacks
fromdotenvimportload_dotenv load_dotenv()fromlangchain_openaiimportChatOpenAIfromlangchain_core.promptsimportChatPromptTemplatefromlangchain_core.output_parsersimportStrOutputParser chain=ChatPromptTemplate.from_template("解释{concept}")|ChatOpenAI(model="gpt-4o-mini")|StrOutputParser()# with_retry:自动重试reliable=chain.with_retry(stop_after_attempt=3,wait_exponential_multiplier=1,wait_exponential_max=10,)# with_fallbacks:备用方案backup=ChatPromptTemplate.from_template("简单解释{concept}")|ChatOpenAI(model="gpt-4o-mini",temperature=0.0)|StrOutputParser()fault_tolerant=chain.with_fallbacks([backup])result=fault_tolerant.invoke({"concept":"Python"})# with_config:运行时配置configured=chain.with_config(run_name="my_chain",tags=["tutorial"],metadata={"version":"1.0"},)

5.3 管道操作符 |

数据流动图
最终结果OutputParserChatModelPromptTemplate用户输入最终结果OutputParserChatModelPromptTemplate用户输入填充模板变量调用 LLM提取文本{"topic": "AI"}"解释什么是AI"AIMessage"AI是..."
fromdotenvimportload_dotenv load_dotenv()fromlangchain_openaiimportChatOpenAIfromlangchain_core.promptsimportChatPromptTemplatefromlangchain_core.output_parsersimportStrOutputParser prompt=ChatPromptTemplate.from_template("用一句话解释{concept}")model=ChatOpenAI(model="gpt-4o-mini",temperature=0.7)parser=StrOutputParser()# 管道组合chain=prompt|model|parser result=chain.invoke({"concept":"量子纠缠"})print(result)# 验证数据流step1=prompt.invoke({"concept":"量子纠缠"})# ChatPromptValuestep2=model.invoke(step1)# AIMessagestep3=parser.invoke(step2)# str

5.4 RunnablePassthrough

fromlangchain_core.runnablesimportRunnablePassthrough,RunnableParallelfromlangchain_openaiimportChatOpenAIfromlangchain_core.promptsimportChatPromptTemplatefromlangchain_core.output_parsersimportStrOutputParser model=ChatOpenAI(model="gpt-4o-mini")# 基本用法:透传输入pt=RunnablePassthrough()print(pt.invoke("你好"))# "你好"# assign:添加字段enriched=RunnablePassthrough.assign(greeting=lambdax:f"你好,{x['name']}!",is_adult=lambdax:x["age"]>=18,)result=enriched.invoke({"name":"张三","age":25})print(result)# {'name': '张三', 'age': 25, 'greeting': '你好,张三!', 'is_adult': True}# 在 RunnableParallel 中保留原始输入analysis=ChatPromptTemplate.from_template("分析情感:{text}")|model|StrOutputParser()parallel=RunnableParallel(original=RunnablePassthrough(),sentiment=analysis,)result=parallel.invoke("今天天气真好!")print(f"原文:{result['original']}")print(f"情感:{result['sentiment']}")

5.5 RunnableParallel

fromlangchain_core.runnablesimportRunnableParallelfromlangchain_openaiimportChatOpenAIfromlangchain_core.promptsimportChatPromptTemplatefromlangchain_core.output_parsersimportStrOutputParser model=ChatOpenAI(model="gpt-4o-mini")analysis=RunnableParallel(summary=ChatPromptTemplate.from_template("总结:{text}")|model|StrOutputParser(),sentiment=ChatPromptTemplate.from_template("情感分析:{text}")|model|StrOutputParser(),keywords=ChatPromptTemplate.from_template("提取3个关键词:{text}")|model|StrOutputParser(),)result=analysis.invoke({"text":"新版本性能提升50%,但安装有些复杂。"})print(f"摘要:{result['summary']}")print(f"情感:{result['sentiment']}")print(f"关键词:{result['keywords']}")# 多语言翻译parallel=RunnableParallel(chinese=ChatPromptTemplate.from_template("翻译成中文:{text}")|model|StrOutputParser(),japanese=ChatPromptTemplate.from_template("翻译成日文:{text}")|model|StrOutputParser(),)result=parallel.invoke({"text":"Hello, World!"})

5.6 RunnableLambda

fromlangchain_core.runnablesimportRunnableLambdafromlangchain_openaiimportChatOpenAIfromlangchain_core.promptsimportChatPromptTemplatefromlangchain_core.output_parsersimportStrOutputParser# 包装自定义函数defword_count(text:str)->int:returnlen(text)defadd_prefix(text:str)->str:returnf"[AI助手]{text}"count_runnable=RunnableLambda(word_count)print(count_runnable.invoke("Hello World"))# 11# 在链中使用model=ChatOpenAI(model="gpt-4o-mini")chain=(ChatPromptTemplate.from_template("解释{concept}")|model|StrOutputParser()|RunnableLambda(add_prefix)# 给输出添加前缀)result=chain.invoke({"concept":"Python"})print(result)# [AI助手] Python 是一种...

5.7 RunnableBranch

fromlangchain_core.runnablesimportRunnableBranchfromlangchain_openaiimportChatOpenAIfromlangchain_core.promptsimportChatPromptTemplatefromlangchain_core.output_parsersimportStrOutputParser model=ChatOpenAI(model="gpt-4o-mini")short_chain=ChatPromptTemplate.from_template("简短回答:{text}")|model|StrOutputParser()long_chain=ChatPromptTemplate.from_template("详细回答:{text}")|model|StrOutputParser()branch=RunnableBranch((lambdax:len(x)<20,short_chain),# 短文本long_chain,# 默认(长文本))print(branch.invoke("什么是AI?"))# 简短回答print(branch.invoke("请详细解释人工智能的发展历史、技术分支和应用场景")[:100])# 详细回答

5.8 配置与运行时修改

fromlangchain_core.runnablesimportConfigurableFieldfromlangchain_openaiimportChatOpenAI# ConfigurableField:运行时动态修改参数model=ChatOpenAI(model="gpt-4o-mini",temperature=ConfigurableField(id="temperature",name="Temperature",description="控制随机性",),)result=model.invoke("你好")# 使用默认 temperatureresult=model.invoke("你好",config={"configurable":{"temperature":0.0}})# 运行时修改

5.9 错误处理

fromdotenvimportload_dotenv load_dotenv()fromlangchain_openaiimportChatOpenAIfromlangchain_core.promptsimportChatPromptTemplatefromlangchain_core.output_parsersimportStrOutputParser chain=ChatPromptTemplate.from_template("解释{concept}")|ChatOpenAI(model="gpt-4o-mini")|StrOutputParser()# with_retry 参数reliable=chain.with_retry(stop_after_attempt=3,# 最大尝试次数retry_if_exception_type=(Exception,),# 重试的异常类型wait_exponential_multiplier=1,# 初始等待 1 秒wait_exponential_max=10,# 最大等待 10 秒)# with_fallbacks 参数backup=ChatPromptTemplate.from_template("简单解释{concept}")|ChatOpenAI(model="gpt-4o-mini",temperature=0.0)|StrOutputParser()fault_tolerant=chain.with_fallbacks([backup],# 备用链列表exceptions_to_handle=(Exception,),# 触发 fallback 的异常)

5.10 LCEL 最佳实践

  1. 优先使用 LCEL:所有新代码都应该使用 LCEL 的管道语法
  2. 保持链简洁:每个链只做一件事,复杂逻辑通过组合实现
  3. 善用 RunnableParallel:独立的操作应该并行执行
  4. 添加错误处理:生产环境必须配置 retry 和 fallback
  5. 使用 with_config:添加 run_name 和 tags 便于调试和监控
  6. 类型标注:使用with_types()标注输入输出类型

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/26 2:17:32

避坑指南:Sentaurus与SILVACO TCAD仿真NPN三极管,结果为啥差了几十uA?

Sentaurus与SILVACO TCAD仿真差异深度解析&#xff1a;从物理模型到网格优化的全链路排查当我们在Sentaurus和SILVACO这两个主流TCAD工具中对同一个NPN三极管进行仿真时&#xff0c;经常会发现输出特性曲线存在微妙的差异——比如在相同基极电流(Ib5μA)条件下&#xff0c;集电…

作者头像 李华
网站建设 2026/5/26 2:17:30

从零打造复古辉光管腕表:高压驱动、低功耗与微型化设计实战

1. 项目概述&#xff1a;从零打造一枚复古辉光管腕表几年前&#xff0c;我在一个老旧的无线电设备里第一次见到辉光管&#xff08;Nixie Tube&#xff09;&#xff0c;那种橘红色的数字在黑暗中幽幽亮起的样子&#xff0c;瞬间就把我拉回到了上世纪六七十年代的科幻电影里。从那…

作者头像 李华
网站建设 2026/5/26 2:16:13

提示词压缩技术:降本增效的黑科技

提示词压缩技术核心原理深度解析 一、先搞懂:为什么提示词压缩是"刚需中的刚需"? 你可能会问:“现在模型都有1M上下文了,为什么还要压缩?” 这篇文章里的"三笔账"其实是每个大模型工程师每天都在面对的现实: 1. 钱包之痛:真实的成本计算 具体例…

作者头像 李华
网站建设 2026/5/26 2:13:50

RAG 实战指南:深入浅出向量数据库 Milvus

在大模型时代&#xff0c;RAG&#xff08;Retrieval-Augmented Generation&#xff0c;检索增强生成&#xff09;已经成为知识问答、智能客服、企业知识库等场景的标配方案。而 RAG 的核心&#xff0c;正是向量数据库。本文将带你系统了解开源分布式向量数据库 Milvus&#xff…

作者头像 李华
网站建设 2026/5/26 2:12:48

Amphenol ICC MSPEC6P2BB010高速线束深度解析

在高速服务器、AI计算平台以及企业级存储设备快速发展的今天&#xff0c;高速线束组件已经成为设备内部互连的重要组成部分。尤其是在PCIe、SAS、NVMe以及高速背板连接场景中&#xff0c;线束的性能不仅影响数据传输效率&#xff0c;还会直接影响系统稳定性与设备寿命。 本文结…

作者头像 李华