如何将运行时 secret 传递给 runnables
需要
langchain-core >= 0.2.22我们可以在运行时使用RunnableConfig.具体来说,我们可以将带有前缀的 secret 传递给__configurable田。这将确保这些密钥不会在调用过程中被跟踪:
from langchain_core.runnables import RunnableConfig
from langchain_core.tools import tool
@tool
def foo(x: int, config: RunnableConfig) -> int:
"""Sum x and a secret int"""
return x + config["configurable"]["__top_secret_int"]
foo.invoke({"x": 5}, {"configurable": {"__top_secret_int": 2, "traced_key": "bar"}})
API 参考:RunnableConfig | 工具
7
查看此运行的 LangSmith 跟踪,我们可以看到记录了 “traced_key” (作为 Metadata 的一部分),而我们的密钥 int 则没有:https://smith.langchain.com/public/aa7e3289-49ca-422d-a408-f6b927210170/r