跳到主要内容

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

需要 langchain-core >= 0.2.22

我们可以使用 RunnableConfig 在运行时将密钥传递给我们的 runnables。具体来说,我们可以将带有 __ 前缀的密钥传递给 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


此页内容是否对您有帮助?