Langchain 多链参数传递
时间: 2025-07-22 22:09:58 浏览: 2
在 LangChain 框架中,实现多个链(Chain)之间的参数传递是构建复杂应用的关键步骤。LangChain 提供了多种机制来支持链之间的数据流动和参数共享,以下是一些常见方法:
### 1. 使用 `SequentialChain` 进行顺序链式调用
`SequentialChain` 是 LangChain 提供的一个工具,用于将多个链按顺序连接,并在它们之间传递参数。每个链的输出可以作为下一个链的输入,或者通过指定输入输出变量进行参数映射。
```python
from langchain.chains import SequentialChain, LLMChain
from langchain.prompts import PromptTemplate
from langchain_community.llms import OpenAI
llm = OpenAI(model="text-davinci-003", temperature=0.7)
# 第一个链:生成一个故事的开头
template1 = PromptTemplate.from_template("写一个关于{topic}的故事开头。")
chain1 = LLMChain(llm=llm, prompt=template1, output_key="story_start")
# 第二个链:继续故事的发展
template2 = PromptTemplate.from_template("根据以下开头继续故事:{story_start}")
chain2 = LLMChain(llm=llm, prompt=template2, output_key="story_continuation")
# 将两个链组合成一个顺序链
sequential_chain = SequentialChain(
chains=[chain1, chain2],
input_variables=["topic"],
output_variables=["story_start", "story_continuation"],
verbose=True
)
# 执行顺序链
result = sequential_chain({"topic": "未来科技"})
print(result["story_continuation"])
```
### 2. 使用 `TransformChain` 进行中间数据转换
如果需要在链之间进行数据格式转换或预处理,可以使用 `TransformChain`。它允许定义一个函数来处理输入并生成输出,供后续链使用。
```python
from langchain.chains import TransformChain, LLMChain, SequentialChain
from langchain.prompts import PromptTemplate
def transform_func(inputs: dict) -> dict:
text = inputs["text"]
# 假设我们在这里进行一些文本处理
return {"processed_text": text.upper()}
transform_chain = TransformChain(
input_variables=["text"],
output_variables=["processed_text"],
transform=transform_func
)
# 创建一个使用处理后文本的 LLMChain
template = PromptTemplate.from_template("解释以下术语:{processed_text}")
llm_chain = LLMChain(llm=OpenAI(), prompt=template)
# 组合成顺序链
sequential_chain = SequentialChain(
chains=[transform_chain, llm_chain],
input_variables=["text"],
output_variables=["processed_text", "text"],
verbose=True
)
# 执行链
result = sequential_chain({"text": "量子计算"})
print(result["text"])
```
### 3. 使用 `Runnable` 接口和 `bind` 方法
LangChain 的 `Runnable` 接口支持链的组合和参数绑定,允许在运行时动态传递参数。
```python
from langchain.chains import RunnableSequence
from langchain.prompts import PromptTemplate
# 定义第一个链
prompt1 = PromptTemplate.from_template("生成一个关于{topic}的简介。")
chain1 = prompt1 | llm
# 定义第二个链
prompt2 = PromptTemplate.from_template("根据以下简介生成一个结论:{output_from_chain1}")
chain2 = prompt2 | llm
# 组合成 RunnableSequence
full_chain = RunnableSequence(chain1, chain2)
# 执行链
result = full_chain.invoke({"topic": "人工智能伦理"})
print(result)
```
### 4. 使用 `Memory` 组件实现上下文共享
如果多个链需要共享上下文或状态,可以使用 `Memory` 组件(如 `ConversationBufferMemory`)来存储和传递历史信息。
```python
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
# 第一个链将输出保存到 memory
chain1 = LLMChain(
llm=llm,
prompt=PromptTemplate.from_template("回答问题:{question1}"),
memory=memory,
output_key="answer1"
)
# 第二个链读取 memory 中的内容
chain2 = LLMChain(
llm=llm,
prompt=PromptTemplate.from_template("根据以下回答总结:{answer1}"),
memory=memory
)
# 执行链
result1 = chain1.invoke({"question1": "量子力学的基本原理是什么?"})
result2 = chain2.invoke({})
print(result2["text"])
```
###
阅读全文
相关推荐


















