以编程方式使用文档

LangGraph 允许运行时配置动态修改代理行为和权限。当使用 LangSmith 部署时,您可以在请求体中(config)或特定请求头中传递此配置。这使得能够根据用户身份或其他请求进行调整。

为了保护隐私,请通过 http.configurable_headers 部分中的配置来控制哪些头被传递到运行时配置 langgraph.json file.

以下是自定义包含和排除头的方法:

{
  "http": {
    "configurable_headers": {
      "includes": ["x-user-id", "x-organization-id", "my-prefix-*"],
      "excludes": ["authorization", "x-api-key"]
    }
  }
}

这些 includesexcludes 列表接受精确的头部名称或使用 * 来匹配任意数量的字符。为了您的安全,不支持其他正则表达式模式。

在图内使用

您可以使用任意节点的 config 参数来访问图中包含的头部。

def my_node(state, config):
  organization_id = config["configurable"].get("x-organization-id")
  ...

或通过上下文获取(在工具和其他嵌套函数中很有用)。

from langgraph.config import get_config

def search_everything(query: str):
  organization_id = get_config()["configurable"].get("x-organization-id")
  ...

您甚至可以动态编译图。

# my_graph.py.


@contextlib.asynccontextmanager
async def generate_agent(config):
  organization_id = config["configurable"].get("x-organization-id")
  if organization_id == "org1":
    graph = ...
    yield graph
  else:
    graph = ...
    yield graph
{
  "graphs": {"agent": "my_grph.py:generate_agent"}
}

退出可配置头部

如果您想退出可配置头部,只需在 s list:

{
  "http": {
    "configurable_headers": {
      "excludes": ["*"]
    }
  }
}

中设置通配符模式即可排除所有头部,不将其添加到运行的配置中。

请注意,排除优先于包含。