可配置头¶
LangGraph 允许在运行时进行配置,以动态修改代理的行为和权限。当使用 LangGraph 平台 时,可以通过请求正文(config
)或特定的请求头传递此配置。这使得可以根据用户身份或其他请求数据进行调整(有关如何在图中访问这些配置的详细信息,请参阅配置指南)。
为了保护隐私,您可以在 langgraph.json
文件中的 http.configurable_headers
部分控制哪些头被传递到运行时配置。
以下是自定义包含和排除头的方法:
{
"http": {
"configurable_headers": {
"include": ["x-user-id", "x-organization-id", "my-prefix-*"],
"exclude": ["authorization", "x-api-key"]
}
}
}
include
和 exclude
列表接受精确的头名称或使用 *
匹配任意数量字符的模式。出于安全考虑,不支持其他正则表达式模式。
在图中使用配置¶
你可以通过任何节点的config
参数来访问包含的头信息。
或者从上下文中获取(在工具或其他嵌套函数中有用)。
from langgraph.config import get_config
def search_everything(query: str):
organization_id = get_config()["configurable"].get("x-organization-id")
...
甚至可以利用它动态编译图。
# my_graph.py.
import contextlib
@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
有关如何使用运行时配置的更多示例,请参阅配置指南。
忽略可配置头¶
如果你希望忽略可配置头,可以在exclude
列表中设置通配符模式:
这将排除所有头信息,使其不被添加到你的运行配置中。
需要注意的是,排除优先于包含。