LangGraph node bindings for SoulPolicy drift / guardrail primitives
Project description
soulpolicy-langgraph
5 行给你的 LangGraph agent 加漂移监测。
安装
pip install soulpolicy soulpolicy-langgraph
用法
from langgraph.graph import StateGraph, END
from typing import TypedDict
from soulpolicy import Client
from soulpolicy_langgraph import drift_check_node, should_block_on_drift
class State(TypedDict, total=False):
messages: list
drift: dict | None
sp = Client(api_key="sk_live_xxx")
graph = StateGraph(State)
graph.add_node("agent", my_agent_step)
graph.add_node("drift", drift_check_node(sp, baseline="bl_xxx"))
graph.set_entry_point("agent")
graph.add_edge("agent", "drift")
graph.add_conditional_edges(
"drift",
should_block_on_drift("warn"), # 路由谓词
{"continue": "agent", "block": END},
)
5 行:1 个 client,1 个 node,1 个谓词,2 条边。其余照常写。
文本提取
默认从 state["messages"] 取最后一条 AI/assistant 消息。也支持 state["output"] / state["last_message"] 字符串。要自定义:
graph.add_node("drift", drift_check_node(
sp,
baseline="bl_xxx",
state_text=lambda s: s["agent_reply"],
))
失败容忍
drift_check_node(sp, baseline="bl_xxx", on_error="skip")
# 调用失败时写入 state["drift"]=None + state["drift_error"]=str(e)
# 默认 "raise" 让 LangGraph retry/handle
路由
should_block_on_drift("warn") —— severity ≥ warn 路由到 "block"。可改:
should_block_on_drift("critical", continue_target="next", block_target="rollback")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file soulpolicy_langgraph-0.2.0.tar.gz.
File metadata
- Download URL: soulpolicy_langgraph-0.2.0.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2cababda32bda0dc42aed82e42cfa86b3d9a0a016f4202eb57e0f66161d12369
|
|
| MD5 |
81061a19a8c19e0ee943fd761b4b6efa
|
|
| BLAKE2b-256 |
fa0630f25011aa97732fb1b6c1c2ce9a866606535bea8eed7df6ea95c7858a58
|
File details
Details for the file soulpolicy_langgraph-0.2.0-py3-none-any.whl.
File metadata
- Download URL: soulpolicy_langgraph-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69d2a84e45971fae4e1cb40c544bdc461d1cbc37a04b6cb0eaccd9e1aa90b52b
|
|
| MD5 |
2e555adefcaa405e9d4dd5d435b6f293
|
|
| BLAKE2b-256 |
30883783350630935a5b240975819bc9ec59bd8f2fb9136d3a758e127a391d89
|