SoulPolicy Python SDK — LLM behavior trust primitives (drift / guardrail / snapshot / style)
Project description
soulpolicy — Python SDK
LLM 行为可信度 primitive。一行集成,按调用计费。
安装
pip install soulpolicy
30 秒上手
from soulpolicy import Client
sp = Client(api_key="sk_live_xxx") # 或设 SOULPOLICY_API_KEY env
# 1) 用 5+ 条「健康输出」建一个 baseline
bl = sp.baselines.create(samples=[
"Hello, thanks for contacting us. How can I help today?",
"Hi there, I am happy to assist. Could you share more details?",
"Apologies for the trouble. Let me look into that for you.",
"Sure, I can check the status. One moment please.",
"Thank you for your patience. I will follow up by email.",
])
print(bl.id, bl.fingerprint)
# 2) 对每次 LLM 输出做漂移判定
r = sp.drift_checks.check(text="Sure, let me help.", baseline=bl.id)
if r.severity == "critical":
handle_drift(r)
print(r.score, r.severity, r.evidence["nearest_baseline_samples"])
# 3) 任何时刻可复现:用 reproducibility 包重算
replay = sp.drift_checks.replay(
text="Sure, let me help.",
baseline=bl.id,
reproducibility=r.reproducibility,
)
assert replay.matched
# 4) 客户回标 — 帮助我们改进 precision/recall
sp.drift_checks.feedback(r.id, label="true_positive")
错误处理
from soulpolicy import Client, AuthenticationError, IdempotencyError
try:
sp.baselines.create(samples=["x"]) # 少于 3 条
except InvalidRequestError as e:
print(e.code, e.message, e.param)
except AuthenticationError:
print("api key invalid")
except IdempotencyError:
print("idempotency key reused with different payload")
Idempotency
POST 请求默认自动带 Idempotency-Key(sdk-auto-<random>)。手动指定:
sp.drift_checks.create(text=..., baseline=..., idempotency_key="my-key-1")
自托管
sp = Client(api_key="sk_local_xxx", base_url="https://soulpolicy.internal.example.com")
资源对照
| Method | Endpoint |
|---|---|
sp.baselines.create(...) |
POST /api/v2/baselines |
sp.baselines.retrieve(id) |
GET /api/v2/baselines/{id} |
sp.baselines.report(id) |
GET /api/v2/baselines/{id}/report |
sp.drift_checks.check(...) |
POST /api/v2/drift_checks |
sp.drift_checks.replay(...) |
POST /api/v2/drift_checks/replay |
sp.drift_checks.feedback(id, ...) |
POST /api/v2/drift_checks/{id}/feedback |
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
soulpolicy-0.2.0.tar.gz
(19.0 kB
view details)
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-0.2.0.tar.gz.
File metadata
- Download URL: soulpolicy-0.2.0.tar.gz
- Upload date:
- Size: 19.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d363f8cc80bf66a9af43ed32decd0216ae7374b068c43e7ab59027b0dd0a63a
|
|
| MD5 |
09e5de080872b1896150ceb39a2d2453
|
|
| BLAKE2b-256 |
1b2d51efa84f56124f7aa5cd9dbf86b57fc8cfb0456705ce2a1ddd61757f040b
|
File details
Details for the file soulpolicy-0.2.0-py3-none-any.whl.
File metadata
- Download URL: soulpolicy-0.2.0-py3-none-any.whl
- Upload date:
- Size: 17.6 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 |
905a57fd95fa1377a3da78f3ff0cd722c0bfc1d4b8f1ee0367b868b4efa728b3
|
|
| MD5 |
6b18aa441c31658f21ca4e34d21e4514
|
|
| BLAKE2b-256 |
560165918386771d707dcf090344858c079320fb388140eac6109b761a51b146
|