Skip to content

SOVRClient (Python)

SOVRClient 是 Python SDK 的主客户端类,提供所有 API 操作的入口。

初始化

python
from sovr_sdk import SOVRClient

client = SOVRClient(
    base_url: str,
    api_key: str = None,
    timeout: int = 30
)

参数

参数类型必填默认值说明
base_urlstr-API 基础 URL
api_keystrNoneAPI Key,不传则从环境变量读取
timeoutint30请求超时时间(秒)

示例

python
# 基础初始化
client = SOVRClient(
    base_url="https://api.sovr.ai",
    api_key="sk_live_xxxxxxxxxxxx"
)

# 使用环境变量
import os
os.environ["SOVR_API_KEY"] = "sk_live_xxxxxxxxxxxx"
client = SOVRClient(base_url="https://api.sovr.ai")

# 自定义超时
client = SOVRClient(
    base_url="https://api.sovr.ai",
    api_key="sk_live_xxxxxxxxxxxx",
    timeout=60
)

Decision 方法

create_decision

创建新的决策记录。

python
def create_decision(
    action: str,
    context: dict,
    risk_level: str = "low",
    requires_approval: bool = False,
    bundle_id: str = None
) -> Decision

参数:

参数类型必填说明
actionstr执行的动作类型
contextdict执行上下文
risk_levelstr风险等级:low/medium/high/critical
requires_approvalbool是否需要人工审批
bundle_idstr关联的信任包 ID

返回: Decision 对象

示例:

python
decision = client.create_decision(
    action="send_email",
    context={
        "to": "user@example.com",
        "subject": "Order Confirmation",
        "body": "Your order has been confirmed."
    },
    risk_level="low"
)
print(f"Decision ID: {decision.id}")

get_decision

获取决策详情。

python
def get_decision(decision_id: str) -> Decision

示例:

python
decision = client.get_decision("dec_xxxxxxxxxxxx")
print(f"Status: {decision.status}")

list_decisions

列出决策记录。

python
def list_decisions(
    status: str = None,
    risk_level: str = None,
    limit: int = 20,
    offset: int = 0
) -> List[Decision]

示例:

python
decisions = client.list_decisions(
    status="pending",
    limit=10
)
for d in decisions:
    print(f"{d.id}: {d.action}")

Trust Bundle 方法

create_trust_bundle

创建新的信任包。

python
def create_trust_bundle(
    name: str,
    bundle_type: str,
    description: str = None
) -> TrustBundle

参数:

参数类型必填说明
namestr信任包名称
bundle_typestr类型:decision/transaction/policy
descriptionstr描述

示例:

python
bundle = client.create_trust_bundle(
    name="Payment Processing",
    bundle_type="transaction",
    description="支付处理流程的信任包"
)

add_evidence

向信任包添加证据。

python
def add_evidence(
    bundle_id: str,
    evidence_type: str,
    content: dict,
    source: str = None
) -> Evidence

示例:

python
evidence = client.add_evidence(
    bundle_id="tb_xxxxxxxxxxxx",
    evidence_type="audit_trail",
    content={"action": "payment_initiated", "amount": 100},
    source="payment_service"
)

evaluate_sufficiency

评估信任包的充分性。

python
def evaluate_sufficiency(bundle_id: str) -> SufficiencyEvaluation

示例:

python
evaluation = client.evaluate_sufficiency("tb_xxxxxxxxxxxx")
print(f"Score: {evaluation.score}")
print(f"Status: {evaluation.status}")

Audit 方法

query_audit_logs

查询审计日志。

python
def query_audit_logs(
    entity_type: str = None,
    entity_id: str = None,
    event_type: str = None,
    start_time: datetime = None,
    end_time: datetime = None,
    limit: int = 20,
    offset: int = 0
) -> List[AuditLog]

示例:

python
from datetime import datetime, timedelta

logs = client.query_audit_logs(
    entity_type="decision",
    start_time=datetime.now() - timedelta(days=7),
    limit=50
)

属性

is_connected

检查客户端是否已连接。

python
@property
def is_connected(self) -> bool

api_version

获取 API 版本。

python
@property
def api_version(self) -> str

Released under the MIT License.