跳到主要内容
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 | tool
7

查看此运行的 LangSmith 追踪,我们可以看到“traced_key”被记录(作为元数据的一部分),而我们的秘密整数没有:https://smith.langchain.com/public/aa7e3289-49ca-422d-a408-f6b927210170/r


此页是否对您有帮助?