Skip to main content

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

Metadata

Release files for soulpolicy 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for soulpolicy 0.2.0
File Size Uploaded
soulpolicy-0.2.0.tar.gz 19.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for soulpolicy 0.2.0
File Interpreter ABI Platform
soulpolicy-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.6 kB

Release files / soulpolicy-0.2.0.tar.gz

Download URL soulpolicy-0.2.0.tar.gz
Size 19.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8d363f8cc80bf66a9af43ed32decd0216ae7374b068c43e7ab59027b0dd0a63a
BLAKE2b-256 checksum
How to use checksums
1b2d51efa84f56124f7aa5cd9dbf86b57fc8cfb0456705ce2a1ddd61757f040b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release files / soulpolicy-0.2.0-py3-none-any.whl

Download URL soulpolicy-0.2.0-py3-none-any.whl
Size 17.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
905a57fd95fa1377a3da78f3ff0cd722c0bfc1d4b8f1ee0367b868b4efa728b3
BLAKE2b-256 checksum
How to use checksums
560165918386771d707dcf090344858c079320fb388140eac6109b761a51b146
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.14

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page