LangChain入门-Runnable 协议完全解析
标签: LangChain, Runnable, LCEL, 协议设计, Python, 异步编程
深入讲解 LangChain 核心 Runnable 协议,包括 LCEL 管道操作符、并行执行、路由分支、流式输出和批量处理
Runnable 协议
用途
LangChain 的核心抽象协议,为所有组件提供统一的执行接口。这是 LangChain 表达式语言(LCEL)的基础。
位置
libs/core/langchain_core/runnables/
知识图谱上下文
#mermaid-svg-fJtfRFO2F8rLBN8W{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-fJtfRFO2F8rLBN8W .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-fJtfRFO2F8rLBN8W .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-fJtfRFO2F8rLBN8W .error-icon{fill:#552222;}#mermaid-svg-fJtfRFO2F8rLBN8W .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-fJtfRFO2F8rLBN8W .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-fJtfRFO2F8rLBN8W .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-fJtfRFO2F8rLBN8W .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-fJtfRFO2F8rLBN8W .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-fJtfRFO2F8rLBN8W .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-fJtfRFO2F8rLBN8W .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-fJtfRFO2F8rLBN8W .marker{fill:#333333;stroke:#333333;}#mermaid-svg-fJtfRFO2F8rLBN8W .marker.cross{stroke:#333333;}#mermaid-svg-fJtfRFO2F8rLBN8W svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-fJtfRFO2F8rLBN8W p{margin:0;}#mermaid-svg-fJtfRFO2F8rLBN8W .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-fJtfRFO2F8rLBN8W .cluster-label text{fill:#333;}#mermaid-svg-fJtfRFO2F8rLBN8W .cluster-label span{color:#333;}#mermaid-svg-fJtfRFO2F8rLBN8W .cluster-label span p{background-color:transparent;}#mermaid-svg-fJtfRFO2F8rLBN8W .label text,#mermaid-svg-fJtfRFO2F8rLBN8W span{fill:#333;color:#333;}#mermaid-svg-fJtfRFO2F8rLBN8W .node rect,#mermaid-svg-fJtfRFO2F8rLBN8W .node circle,#mermaid-svg-fJtfRFO2F8rLBN8W .node ellipse,#mermaid-svg-fJtfRFO2F8rLBN8W .node polygon,#mermaid-svg-fJtfRFO2F8rLBN8W .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-fJtfRFO2F8rLBN8W .rough-node .label text,#mermaid-svg-fJtfRFO2F8rLBN8W .node .label text,#mermaid-svg-fJtfRFO2F8rLBN8W .image-shape .label,#mermaid-svg-fJtfRFO2F8rLBN8W .icon-shape .label{text-anchor:middle;}#mermaid-svg-fJtfRFO2F8rLBN8W .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-fJtfRFO2F8rLBN8W .rough-node .label,#mermaid-svg-fJtfRFO2F8rLBN8W .node .label,#mermaid-svg-fJtfRFO2F8rLBN8W .image-shape .label,#mermaid-svg-fJtfRFO2F8rLBN8W .icon-shape .label{text-align:center;}#mermaid-svg-fJtfRFO2F8rLBN8W .node.clickable{cursor:pointer;}#mermaid-svg-fJtfRFO2F8rLBN8W .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-fJtfRFO2F8rLBN8W .arrowheadPath{fill:#333333;}#mermaid-svg-fJtfRFO2F8rLBN8W .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-fJtfRFO2F8rLBN8W .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-fJtfRFO2F8rLBN8W .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-fJtfRFO2F8rLBN8W .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-fJtfRFO2F8rLBN8W .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-fJtfRFO2F8rLBN8W .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-fJtfRFO2F8rLBN8W .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-fJtfRFO2F8rLBN8W .cluster text{fill:#333;}#mermaid-svg-fJtfRFO2F8rLBN8W .cluster span{color:#333;}#mermaid-svg-fJtfRFO2F8rLBN8W div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-fJtfRFO2F8rLBN8W .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-fJtfRFO2F8rLBN8W rect.text{fill:none;stroke-width:0;}#mermaid-svg-fJtfRFO2F8rLBN8W .icon-shape,#mermaid-svg-fJtfRFO2F8rLBN8W .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-fJtfRFO2F8rLBN8W .icon-shape p,#mermaid-svg-fJtfRFO2F8rLBN8W .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-fJtfRFO2F8rLBN8W .icon-shape rect,#mermaid-svg-fJtfRFO2F8rLBN8W .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-fJtfRFO2F8rLBN8W .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-fJtfRFO2F8rLBN8W .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-fJtfRFO2F8rLBN8W :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
组合
使用
Runnable
语言模型
工具
检索器
链
嵌入模型
Agent
核心类/模块
Runnable 类
- Runnable: 基础抽象类,定义核心接口
- RunnableLambda: 将函数转换为 Runnable
- RunnableParallel: 并行执行多个 Runnable
- RunnableSequence: 顺序执行(LCEL 的 | 操作符)
- RunnablePassthrough: 传递输入不变
- RunnableConfig: 运行时配置
核心接口
| invoke() | 同步执行 | input, config | output |
| ainvoke() | 异步执行 | input, config | output |
| batch() | 批量执行 | inputs, config | List[output] |
| abatch() | 异步批量 | inputs, config | List[output] |
| stream() | 流式输出 | input, config | Iterator |
| astream() | 异步流式 | input, config | AsyncIterator |
| stream_events() | 事件流 | input, config | Iterator |
依赖关系
内部依赖
- langchain_core.messages: 消息类型
- langchain_core.callbacks: 回调系统
- langchain_core.output_parsers: 输出解析
外部依赖
- typing: 类型提示
- asyncio: 异步支持
- operator: 管道操作符
数据流
#mermaid-svg-ykuuAjjVC7JxPPug{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ykuuAjjVC7JxPPug .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ykuuAjjVC7JxPPug .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ykuuAjjVC7JxPPug .error-icon{fill:#552222;}#mermaid-svg-ykuuAjjVC7JxPPug .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ykuuAjjVC7JxPPug .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ykuuAjjVC7JxPPug .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ykuuAjjVC7JxPPug .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ykuuAjjVC7JxPPug .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ykuuAjjVC7JxPPug .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ykuuAjjVC7JxPPug .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ykuuAjjVC7JxPPug .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ykuuAjjVC7JxPPug .marker.cross{stroke:#333333;}#mermaid-svg-ykuuAjjVC7JxPPug svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ykuuAjjVC7JxPPug p{margin:0;}#mermaid-svg-ykuuAjjVC7JxPPug .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-ykuuAjjVC7JxPPug .cluster-label text{fill:#333;}#mermaid-svg-ykuuAjjVC7JxPPug .cluster-label span{color:#333;}#mermaid-svg-ykuuAjjVC7JxPPug .cluster-label span p{background-color:transparent;}#mermaid-svg-ykuuAjjVC7JxPPug .label text,#mermaid-svg-ykuuAjjVC7JxPPug span{fill:#333;color:#333;}#mermaid-svg-ykuuAjjVC7JxPPug .node rect,#mermaid-svg-ykuuAjjVC7JxPPug .node circle,#mermaid-svg-ykuuAjjVC7JxPPug .node ellipse,#mermaid-svg-ykuuAjjVC7JxPPug .node polygon,#mermaid-svg-ykuuAjjVC7JxPPug .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ykuuAjjVC7JxPPug .rough-node .label text,#mermaid-svg-ykuuAjjVC7JxPPug .node .label text,#mermaid-svg-ykuuAjjVC7JxPPug .image-shape .label,#mermaid-svg-ykuuAjjVC7JxPPug .icon-shape .label{text-anchor:middle;}#mermaid-svg-ykuuAjjVC7JxPPug .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ykuuAjjVC7JxPPug .rough-node .label,#mermaid-svg-ykuuAjjVC7JxPPug .node .label,#mermaid-svg-ykuuAjjVC7JxPPug .image-shape .label,#mermaid-svg-ykuuAjjVC7JxPPug .icon-shape .label{text-align:center;}#mermaid-svg-ykuuAjjVC7JxPPug .node.clickable{cursor:pointer;}#mermaid-svg-ykuuAjjVC7JxPPug .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ykuuAjjVC7JxPPug .arrowheadPath{fill:#333333;}#mermaid-svg-ykuuAjjVC7JxPPug .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ykuuAjjVC7JxPPug .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ykuuAjjVC7JxPPug .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ykuuAjjVC7JxPPug .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ykuuAjjVC7JxPPug .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ykuuAjjVC7JxPPug .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ykuuAjjVC7JxPPug .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ykuuAjjVC7JxPPug .cluster text{fill:#333;}#mermaid-svg-ykuuAjjVC7JxPPug .cluster span{color:#333;}#mermaid-svg-ykuuAjjVC7JxPPug div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ykuuAjjVC7JxPPug .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ykuuAjjVC7JxPPug rect.text{fill:none;stroke-width:0;}#mermaid-svg-ykuuAjjVC7JxPPug .icon-shape,#mermaid-svg-ykuuAjjVC7JxPPug .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ykuuAjjVC7JxPPug .icon-shape p,#mermaid-svg-ykuuAjjVC7JxPPug .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ykuuAjjVC7JxPPug .icon-shape rect,#mermaid-svg-ykuuAjjVC7JxPPug .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ykuuAjjVC7JxPPug .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ykuuAjjVC7JxPPug .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ykuuAjjVC7JxPPug :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
invoke
处理
return
stream
yield
yield
输入
Runnable
转换
输出
输入
Runnable
数据块
数据块
LCEL 管道操作符
使用 | 组合组件
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
# 管道组合
chain = (
ChatPromptTemplate.from_template("Tell me a joke about {topic}")
| ChatOpenAI(model="gpt-4")
| StrOutputParser()
)
实际执行流程
#mermaid-svg-vS4KewaPYC2VGqpP{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-vS4KewaPYC2VGqpP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-vS4KewaPYC2VGqpP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-vS4KewaPYC2VGqpP .error-icon{fill:#552222;}#mermaid-svg-vS4KewaPYC2VGqpP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vS4KewaPYC2VGqpP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-vS4KewaPYC2VGqpP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vS4KewaPYC2VGqpP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vS4KewaPYC2VGqpP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-vS4KewaPYC2VGqpP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vS4KewaPYC2VGqpP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vS4KewaPYC2VGqpP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vS4KewaPYC2VGqpP .marker.cross{stroke:#333333;}#mermaid-svg-vS4KewaPYC2VGqpP svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vS4KewaPYC2VGqpP p{margin:0;}#mermaid-svg-vS4KewaPYC2VGqpP .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-vS4KewaPYC2VGqpP .cluster-label text{fill:#333;}#mermaid-svg-vS4KewaPYC2VGqpP .cluster-label span{color:#333;}#mermaid-svg-vS4KewaPYC2VGqpP .cluster-label span p{background-color:transparent;}#mermaid-svg-vS4KewaPYC2VGqpP .label text,#mermaid-svg-vS4KewaPYC2VGqpP span{fill:#333;color:#333;}#mermaid-svg-vS4KewaPYC2VGqpP .node rect,#mermaid-svg-vS4KewaPYC2VGqpP .node circle,#mermaid-svg-vS4KewaPYC2VGqpP .node ellipse,#mermaid-svg-vS4KewaPYC2VGqpP .node polygon,#mermaid-svg-vS4KewaPYC2VGqpP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-vS4KewaPYC2VGqpP .rough-node .label text,#mermaid-svg-vS4KewaPYC2VGqpP .node .label text,#mermaid-svg-vS4KewaPYC2VGqpP .image-shape .label,#mermaid-svg-vS4KewaPYC2VGqpP .icon-shape .label{text-anchor:middle;}#mermaid-svg-vS4KewaPYC2VGqpP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-vS4KewaPYC2VGqpP .rough-node .label,#mermaid-svg-vS4KewaPYC2VGqpP .node .label,#mermaid-svg-vS4KewaPYC2VGqpP .image-shape .label,#mermaid-svg-vS4KewaPYC2VGqpP .icon-shape .label{text-align:center;}#mermaid-svg-vS4KewaPYC2VGqpP .node.clickable{cursor:pointer;}#mermaid-svg-vS4KewaPYC2VGqpP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-vS4KewaPYC2VGqpP .arrowheadPath{fill:#333333;}#mermaid-svg-vS4KewaPYC2VGqpP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-vS4KewaPYC2VGqpP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-vS4KewaPYC2VGqpP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vS4KewaPYC2VGqpP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-vS4KewaPYC2VGqpP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vS4KewaPYC2VGqpP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-vS4KewaPYC2VGqpP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-vS4KewaPYC2VGqpP .cluster text{fill:#333;}#mermaid-svg-vS4KewaPYC2VGqpP .cluster span{color:#333;}#mermaid-svg-vS4KewaPYC2VGqpP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-vS4KewaPYC2VGqpP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-vS4KewaPYC2VGqpP rect.text{fill:none;stroke-width:0;}#mermaid-svg-vS4KewaPYC2VGqpP .icon-shape,#mermaid-svg-vS4KewaPYC2VGqpP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vS4KewaPYC2VGqpP .icon-shape p,#mermaid-svg-vS4KewaPYC2VGqpP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-vS4KewaPYC2VGqpP .icon-shape rect,#mermaid-svg-vS4KewaPYC2VGqpP .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vS4KewaPYC2VGqpP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-vS4KewaPYC2VGqpP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-vS4KewaPYC2VGqpP :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
格式化
生成消息
提取文本
提示模板
LLM
解析器
最终输出
并行执行
RunnableParallel
from langchain_core.runnables import RunnableParallel
# 并行执行多个链
chain = RunnableParallel(
joke=chain1,
poem=chain2,
fact=chain3
)
并行流程
#mermaid-svg-OLrycbryzRJDwp0V{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-OLrycbryzRJDwp0V .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-OLrycbryzRJDwp0V .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-OLrycbryzRJDwp0V .error-icon{fill:#552222;}#mermaid-svg-OLrycbryzRJDwp0V .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-OLrycbryzRJDwp0V .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-OLrycbryzRJDwp0V .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-OLrycbryzRJDwp0V .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-OLrycbryzRJDwp0V .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-OLrycbryzRJDwp0V .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-OLrycbryzRJDwp0V .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-OLrycbryzRJDwp0V .marker{fill:#333333;stroke:#333333;}#mermaid-svg-OLrycbryzRJDwp0V .marker.cross{stroke:#333333;}#mermaid-svg-OLrycbryzRJDwp0V svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-OLrycbryzRJDwp0V p{margin:0;}#mermaid-svg-OLrycbryzRJDwp0V .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-OLrycbryzRJDwp0V .cluster-label text{fill:#333;}#mermaid-svg-OLrycbryzRJDwp0V .cluster-label span{color:#333;}#mermaid-svg-OLrycbryzRJDwp0V .cluster-label span p{background-color:transparent;}#mermaid-svg-OLrycbryzRJDwp0V .label text,#mermaid-svg-OLrycbryzRJDwp0V span{fill:#333;color:#333;}#mermaid-svg-OLrycbryzRJDwp0V .node rect,#mermaid-svg-OLrycbryzRJDwp0V .node circle,#mermaid-svg-OLrycbryzRJDwp0V .node ellipse,#mermaid-svg-OLrycbryzRJDwp0V .node polygon,#mermaid-svg-OLrycbryzRJDwp0V .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-OLrycbryzRJDwp0V .rough-node .label text,#mermaid-svg-OLrycbryzRJDwp0V .node .label text,#mermaid-svg-OLrycbryzRJDwp0V .image-shape .label,#mermaid-svg-OLrycbryzRJDwp0V .icon-shape .label{text-anchor:middle;}#mermaid-svg-OLrycbryzRJDwp0V .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-OLrycbryzRJDwp0V .rough-node .label,#mermaid-svg-OLrycbryzRJDwp0V .node .label,#mermaid-svg-OLrycbryzRJDwp0V .image-shape .label,#mermaid-svg-OLrycbryzRJDwp0V .icon-shape .label{text-align:center;}#mermaid-svg-OLrycbryzRJDwp0V .node.clickable{cursor:pointer;}#mermaid-svg-OLrycbryzRJDwp0V .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-OLrycbryzRJDwp0V .arrowheadPath{fill:#333333;}#mermaid-svg-OLrycbryzRJDwp0V .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-OLrycbryzRJDwp0V .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-OLrycbryzRJDwp0V .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-OLrycbryzRJDwp0V .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-OLrycbryzRJDwp0V .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-OLrycbryzRJDwp0V .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-OLrycbryzRJDwp0V .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-OLrycbryzRJDwp0V .cluster text{fill:#333;}#mermaid-svg-OLrycbryzRJDwp0V .cluster span{color:#333;}#mermaid-svg-OLrycbryzRJDwp0V div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-OLrycbryzRJDwp0V .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-OLrycbryzRJDwp0V rect.text{fill:none;stroke-width:0;}#mermaid-svg-OLrycbryzRJDwp0V .icon-shape,#mermaid-svg-OLrycbryzRJDwp0V .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-OLrycbryzRJDwp0V .icon-shape p,#mermaid-svg-OLrycbryzRJDwp0V .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-OLrycbryzRJDwp0V .icon-shape rect,#mermaid-svg-OLrycbryzRJDwp0V .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-OLrycbryzRJDwp0V .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-OLrycbryzRJDwp0V .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-OLrycbryzRJDwp0V :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
分支1
分支2
分支3
结果
结果
结果
输入
RunnableParallel
链1
链2
链3
合并结果
最终输出
路由和条件分支
RunnableBranch
from langchain_core.runnables import RunnableBranch
# 条件路由
branch = RunnableBranch(
(lambda x: x["language"] == "spanish", spanish_chain),
(lambda x: x["language"] == "english", english_chain),
default_chain
)
路由流程
#mermaid-svg-51Y4NGDWkPpT3zoc{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-51Y4NGDWkPpT3zoc .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-51Y4NGDWkPpT3zoc .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-51Y4NGDWkPpT3zoc .error-icon{fill:#552222;}#mermaid-svg-51Y4NGDWkPpT3zoc .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-51Y4NGDWkPpT3zoc .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-51Y4NGDWkPpT3zoc .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-51Y4NGDWkPpT3zoc .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-51Y4NGDWkPpT3zoc .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-51Y4NGDWkPpT3zoc .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-51Y4NGDWkPpT3zoc .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-51Y4NGDWkPpT3zoc .marker{fill:#333333;stroke:#333333;}#mermaid-svg-51Y4NGDWkPpT3zoc .marker.cross{stroke:#333333;}#mermaid-svg-51Y4NGDWkPpT3zoc svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-51Y4NGDWkPpT3zoc p{margin:0;}#mermaid-svg-51Y4NGDWkPpT3zoc .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-51Y4NGDWkPpT3zoc .cluster-label text{fill:#333;}#mermaid-svg-51Y4NGDWkPpT3zoc .cluster-label span{color:#333;}#mermaid-svg-51Y4NGDWkPpT3zoc .cluster-label span p{background-color:transparent;}#mermaid-svg-51Y4NGDWkPpT3zoc .label text,#mermaid-svg-51Y4NGDWkPpT3zoc span{fill:#333;color:#333;}#mermaid-svg-51Y4NGDWkPpT3zoc .node rect,#mermaid-svg-51Y4NGDWkPpT3zoc .node circle,#mermaid-svg-51Y4NGDWkPpT3zoc .node ellipse,#mermaid-svg-51Y4NGDWkPpT3zoc .node polygon,#mermaid-svg-51Y4NGDWkPpT3zoc .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-51Y4NGDWkPpT3zoc .rough-node .label text,#mermaid-svg-51Y4NGDWkPpT3zoc .node .label text,#mermaid-svg-51Y4NGDWkPpT3zoc .image-shape .label,#mermaid-svg-51Y4NGDWkPpT3zoc .icon-shape .label{text-anchor:middle;}#mermaid-svg-51Y4NGDWkPpT3zoc .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-51Y4NGDWkPpT3zoc .rough-node .label,#mermaid-svg-51Y4NGDWkPpT3zoc .node .label,#mermaid-svg-51Y4NGDWkPpT3zoc .image-shape .label,#mermaid-svg-51Y4NGDWkPpT3zoc .icon-shape .label{text-align:center;}#mermaid-svg-51Y4NGDWkPpT3zoc .node.clickable{cursor:pointer;}#mermaid-svg-51Y4NGDWkPpT3zoc .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-51Y4NGDWkPpT3zoc .arrowheadPath{fill:#333333;}#mermaid-svg-51Y4NGDWkPpT3zoc .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-51Y4NGDWkPpT3zoc .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-51Y4NGDWkPpT3zoc .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-51Y4NGDWkPpT3zoc .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-51Y4NGDWkPpT3zoc .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-51Y4NGDWkPpT3zoc .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-51Y4NGDWkPpT3zoc .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-51Y4NGDWkPpT3zoc .cluster text{fill:#333;}#mermaid-svg-51Y4NGDWkPpT3zoc .cluster span{color:#333;}#mermaid-svg-51Y4NGDWkPpT3zoc div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-51Y4NGDWkPpT3zoc .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-51Y4NGDWkPpT3zoc rect.text{fill:none;stroke-width:0;}#mermaid-svg-51Y4NGDWkPpT3zoc .icon-shape,#mermaid-svg-51Y4NGDWkPpT3zoc .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-51Y4NGDWkPpT3zoc .icon-shape p,#mermaid-svg-51Y4NGDWkPpT3zoc .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-51Y4NGDWkPpT3zoc .icon-shape rect,#mermaid-svg-51Y4NGDWkPpT3zoc .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-51Y4NGDWkPpT3zoc .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-51Y4NGDWkPpT3zoc .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-51Y4NGDWkPpT3zoc :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
条件1
条件2
默认
输入
条件判断
链1
链2
默认链
输出
流式输出
stream() 方法
# 流式文本生成
for chunk in chain.stream({"topic": "cats"}):
print(chunk, end="", flush=True)
astream() 异步流
async for chunk in chain.astream({"topic": "cats"}):
print(chunk, end="", flush=True)
批量处理
batch() 方法
# 批量执行
results = chain.batch([
{"topic": "cats"},
{"topic": "dogs"},
{"topic": "birds"}
])
并行批量
# 使用 max_concurrency 控制并发
results = chain.batch(inputs, config={"max_concurrency": 5})
配置和元数据
RunnableConfig
from langchain_core.runnables import RunnableConfig
config = RunnableConfig(
tags=["my-tag"],
metadata={"user_id": "123"},
callbacks=[my_handler],
run_name="my-run"
)
配置传递
#mermaid-svg-UjitDuYnKhS9qEmo{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-UjitDuYnKhS9qEmo .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-UjitDuYnKhS9qEmo .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-UjitDuYnKhS9qEmo .error-icon{fill:#552222;}#mermaid-svg-UjitDuYnKhS9qEmo .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-UjitDuYnKhS9qEmo .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-UjitDuYnKhS9qEmo .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-UjitDuYnKhS9qEmo .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-UjitDuYnKhS9qEmo .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-UjitDuYnKhS9qEmo .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-UjitDuYnKhS9qEmo .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-UjitDuYnKhS9qEmo .marker{fill:#333333;stroke:#333333;}#mermaid-svg-UjitDuYnKhS9qEmo .marker.cross{stroke:#333333;}#mermaid-svg-UjitDuYnKhS9qEmo svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-UjitDuYnKhS9qEmo p{margin:0;}#mermaid-svg-UjitDuYnKhS9qEmo .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-UjitDuYnKhS9qEmo .cluster-label text{fill:#333;}#mermaid-svg-UjitDuYnKhS9qEmo .cluster-label span{color:#333;}#mermaid-svg-UjitDuYnKhS9qEmo .cluster-label span p{background-color:transparent;}#mermaid-svg-UjitDuYnKhS9qEmo .label text,#mermaid-svg-UjitDuYnKhS9qEmo span{fill:#333;color:#333;}#mermaid-svg-UjitDuYnKhS9qEmo .node rect,#mermaid-svg-UjitDuYnKhS9qEmo .node circle,#mermaid-svg-UjitDuYnKhS9qEmo .node ellipse,#mermaid-svg-UjitDuYnKhS9qEmo .node polygon,#mermaid-svg-UjitDuYnKhS9qEmo .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-UjitDuYnKhS9qEmo .rough-node .label text,#mermaid-svg-UjitDuYnKhS9qEmo .node .label text,#mermaid-svg-UjitDuYnKhS9qEmo .image-shape .label,#mermaid-svg-UjitDuYnKhS9qEmo .icon-shape .label{text-anchor:middle;}#mermaid-svg-UjitDuYnKhS9qEmo .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-UjitDuYnKhS9qEmo .rough-node .label,#mermaid-svg-UjitDuYnKhS9qEmo .node .label,#mermaid-svg-UjitDuYnKhS9qEmo .image-shape .label,#mermaid-svg-UjitDuYnKhS9qEmo .icon-shape .label{text-align:center;}#mermaid-svg-UjitDuYnKhS9qEmo .node.clickable{cursor:pointer;}#mermaid-svg-UjitDuYnKhS9qEmo .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-UjitDuYnKhS9qEmo .arrowheadPath{fill:#333333;}#mermaid-svg-UjitDuYnKhS9qEmo .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-UjitDuYnKhS9qEmo .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-UjitDuYnKhS9qEmo .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-UjitDuYnKhS9qEmo .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-UjitDuYnKhS9qEmo .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-UjitDuYnKhS9qEmo .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-UjitDuYnKhS9qEmo .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-UjitDuYnKhS9qEmo .cluster text{fill:#333;}#mermaid-svg-UjitDuYnKhS9qEmo .cluster span{color:#333;}#mermaid-svg-UjitDuYnKhS9qEmo div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-UjitDuYnKhS9qEmo .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-UjitDuYnKhS9qEmo rect.text{fill:none;stroke-width:0;}#mermaid-svg-UjitDuYnKhS9qEmo .icon-shape,#mermaid-svg-UjitDuYnKhS9qEmo .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-UjitDuYnKhS9qEmo .icon-shape p,#mermaid-svg-UjitDuYnKhS9qEmo .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-UjitDuYnKhS9qEmo .icon-shape rect,#mermaid-svg-UjitDuYnKhS9qEmo .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-UjitDuYnKhS9qEmo .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-UjitDuYnKhS9qEmo .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-UjitDuYnKhS9qEmo :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
传递
传递
传递
使用
使用
使用
RunnableConfig
invoke/ainvoke
batch/abatch
stream/astream
标签
元数据
回调
自定义 Runnable
使用 RunnableLambda
from langchain_core.runnables import RunnableLambda
# 将函数转为 Runnable
def custom_function(input: dict) –> str:
return f"Processed: {input['text']}"
custom_runnable = RunnableLambda(custom_function)
继承 Runnable 类
from langchain_core.runnables import Runnable
class CustomRunnable(Runnable):
def invoke(self, input, config=None):
# 自定义处理逻辑
return self._process(input)
重要设计模式
1. 装饰器模式
# 使用 bind() 绑定参数
llm = ChatOpenAI(model="gpt-4")
llm_with_stop = llm.bind(stop=["\\n"])
2. 组合模式
#mermaid-svg-75c60uOZjjXfd7cb{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-75c60uOZjjXfd7cb .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-75c60uOZjjXfd7cb .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-75c60uOZjjXfd7cb .error-icon{fill:#552222;}#mermaid-svg-75c60uOZjjXfd7cb .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-75c60uOZjjXfd7cb .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-75c60uOZjjXfd7cb .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-75c60uOZjjXfd7cb .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-75c60uOZjjXfd7cb .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-75c60uOZjjXfd7cb .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-75c60uOZjjXfd7cb .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-75c60uOZjjXfd7cb .marker{fill:#333333;stroke:#333333;}#mermaid-svg-75c60uOZjjXfd7cb .marker.cross{stroke:#333333;}#mermaid-svg-75c60uOZjjXfd7cb svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-75c60uOZjjXfd7cb p{margin:0;}#mermaid-svg-75c60uOZjjXfd7cb .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-75c60uOZjjXfd7cb .cluster-label text{fill:#333;}#mermaid-svg-75c60uOZjjXfd7cb .cluster-label span{color:#333;}#mermaid-svg-75c60uOZjjXfd7cb .cluster-label span p{background-color:transparent;}#mermaid-svg-75c60uOZjjXfd7cb .label text,#mermaid-svg-75c60uOZjjXfd7cb span{fill:#333;color:#333;}#mermaid-svg-75c60uOZjjXfd7cb .node rect,#mermaid-svg-75c60uOZjjXfd7cb .node circle,#mermaid-svg-75c60uOZjjXfd7cb .node ellipse,#mermaid-svg-75c60uOZjjXfd7cb .node polygon,#mermaid-svg-75c60uOZjjXfd7cb .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-75c60uOZjjXfd7cb .rough-node .label text,#mermaid-svg-75c60uOZjjXfd7cb .node .label text,#mermaid-svg-75c60uOZjjXfd7cb .image-shape .label,#mermaid-svg-75c60uOZjjXfd7cb .icon-shape .label{text-anchor:middle;}#mermaid-svg-75c60uOZjjXfd7cb .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-75c60uOZjjXfd7cb .rough-node .label,#mermaid-svg-75c60uOZjjXfd7cb .node .label,#mermaid-svg-75c60uOZjjXfd7cb .image-shape .label,#mermaid-svg-75c60uOZjjXfd7cb .icon-shape .label{text-align:center;}#mermaid-svg-75c60uOZjjXfd7cb .node.clickable{cursor:pointer;}#mermaid-svg-75c60uOZjjXfd7cb .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-75c60uOZjjXfd7cb .arrowheadPath{fill:#333333;}#mermaid-svg-75c60uOZjjXfd7cb .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-75c60uOZjjXfd7cb .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-75c60uOZjjXfd7cb .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-75c60uOZjjXfd7cb .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-75c60uOZjjXfd7cb .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-75c60uOZjjXfd7cb .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-75c60uOZjjXfd7cb .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-75c60uOZjjXfd7cb .cluster text{fill:#333;}#mermaid-svg-75c60uOZjjXfd7cb .cluster span{color:#333;}#mermaid-svg-75c60uOZjjXfd7cb div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-75c60uOZjjXfd7cb .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-75c60uOZjjXfd7cb rect.text{fill:none;stroke-width:0;}#mermaid-svg-75c60uOZjjXfd7cb .icon-shape,#mermaid-svg-75c60uOZjjXfd7cb .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-75c60uOZjjXfd7cb .icon-shape p,#mermaid-svg-75c60uOZjjXfd7cb .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-75c60uOZjjXfd7cb .icon-shape rect,#mermaid-svg-75c60uOZjjXfd7cb .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-75c60uOZjjXfd7cb .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-75c60uOZjjXfd7cb .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-75c60uOZjjXfd7cb :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
组合
组合
形成
Runnable A
Runnable B
Runnable C
复杂链
3. 适配器模式
# 将函数适配为 Runnable
@runnable
def my_function(x: str) –> str:
return x.upper()
# 现在可以使用管道
chain = my_function | another_runnable
错误处理
重试机制
from langchain_core.runnables import RunnableLambda
from tenacity import retry
@retry(stop=stop_after_attempt(3))
def flaky_function(x):
# 可能失败的函数
return process(x)
runnable_with_retry = RunnableLambda(flaky_function)
回退机制
from langchain_core.runnables import RunnableWithFallbacks
# 主流程失败时使用备选
chain_with_fallback = primary_chain.with_fallbacks([fallback_chain])
性能优化
1. 流式处理
- 大数据集处理
- 实时响应
- 减少内存使用
2. 批量处理
- 并行执行多个请求
- 减少 API 调用开销
- 提高吞吐量
3. 异步执行
- 非阻塞 I/O
- 提高并发能力
- 更好的资源利用


