RunnablePassthrough: Passing data through | 🦜️🔗 Langchain
1、RunnablePassthrough可以在不改变或添加额外键的情况下传递输入。通常和RunnableParallel结合使用去分配数值给到字典的新键
两种方式调用RunnablePassthrough
(1)自身调用
(2)通过函数分配额外的参数给到
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
runnable = RunnableParallel(
passed=RunnablePassthrough(),#第一种用法自身调用传递
extra=RunnablePassthrough.assign(mult=lambda x: x["num"] * 3),#第二种用法在extra字典添加一个mult新键并赋值
modified=lambda x: x["num"] + 1,#passed传递生成新变量
)
print(runnable.invoke({"num": 1}))
2、RunnablePassthrough 和RunnableMap结合使用
输入模板需要“context” 和“question”两个键值对,而用户只输入“question”,“context” 是已知的信息且需要和“question”结合判断作回答,通过RunnablePassthrough 来完成
template = """XXXX:
{context}
Question: {question}
"""
retrieval_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| model
| StrOutputParser()
)
retrieval_chain.invoke("where did harrison work?")