Skip to main content
Open In ColabOpen on GitHub

如何将运行时密钥传递给可运行对象

需要 langchain-core >= 0.2.22

我们可以在运行时通过 RunnableConfig 将密钥传递给我们的 可运行对象。具体来说,我们可以使用 __ 前缀将密钥传递到 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" 已被记录(作为元数据的一部分),而我们的密钥整数则未被记录:https://smith.langchain.com/public/aa7e3289-49ca-422d-a408-f6b927210170/r