目录文档-技术白皮书44-EFT.WP.Data.ModelCards v1.0

第16章 实现绑定与验证 API


I. 章节目的与范围

:定义接口原型、请求/响应信封、错误码、鉴权与幂等、速率限制与版本协商;覆盖模型卡校验、计量校核、引用锚点检查、冻结切分/泄漏审计、评测与显著性、校准与覆盖、鲁棒/公平条目、发布与撤回;与数据契约、计量口径、引用锚点与导出清单对齐。验证 API规范性实现绑定提供

II. 服务面(规范性)

services:

modelcards.v1:

# 元数据与结构校验

- POST /api/v1/validate_model_card

- POST /api/v1/lint_model_card

- POST /api/v1/check_units

- POST /api/v1/verify_references

- POST /api/v1/split_freeze_check

- POST /api/v1/leakage_audit

# 评测/校准/稳健/公平

- POST /api/v1/eval_run

- POST /api/v1/eval_significance

- POST /api/v1/calibration_eval

- POST /api/v1/robustness_eval

- POST /api/v1/fairness_eval

# 模型侧绑定(可选)

- POST /api/v1/score

- POST /api/v1/calibrate

- POST /api/v1/explain

# 工件与发布

- POST /api/v1/hash_artifact

- POST /api/v1/sign_artifact

- POST /api/v1/publish_release

- POST /api/v1/revoke_release


III. 通用请求/响应与鉴权

request_envelope:

headers:

Authorization: "Bearer <oidc-token> | HMAC <key>:<sig>"

x-eift-idempotency: "<uuid>" # 幂等键(≥24h 有效)

content-type: "application/json"

body:

card: { ... } # model_card 对象(YAML/JSON)

artifacts?: [{path, bytes_b64?, sha256?}]

options?: {dry_run?: true, strict?: true}

response_envelope:

status: "ok" | "warn" | "error"

errors: [{code, message, path?, see?}]

warnings:[{code, message, path?, see?}]

metrics: { ... } # 校验/评测统计

version: "modelcards.v1"

security:

auth: "OIDC bearer | HMAC"

tls: "TLS1.2+"

scope: ["validate","evaluate","publish","admin"]

rate_limits:

per_key_per_minute: 120

burst: 60


IV. 规范性 OpenAPI 摘录

openapi: 3.0.3

info: {title: "EFT ModelCards API", version: "v1"}

paths:

/api/v1/validate_model_card:

post:

summary: Validate Model Card against schema & cross-volume constraints

requestBody:

required: true

content: {"application/json": {schema: {$ref: "#/components/schemas/CardEnvelope"}}}

responses:

"200": {description: "Result", content: {"application/json": {schema: {$ref: "#/components/schemas/Result"}}}}

/api/v1/check_units:

post:

summary: Dimensional/unit consistency check (Metrology v1.0)

responses: {"200": {content: {"application/json": {schema: {$ref: "#/components/schemas/Result"}}}}}

/api/v1/score:

post:

summary: Bind model scoring I/O with io_schema (Chapter 6)

responses: {"200": {content: {"application/json": {schema: {$ref: "#/components/schemas/ScoreResult"}}}}}

components:

schemas:

CardEnvelope: {type: object, required: [card], properties: {card: {}}}

Result:

type: object

properties:

status: {type: string, enum: [ok, warn, error]}

errors: {type: array, items: {$ref: "#/components/schemas/Issue"}}

warnings:{type: array,items: {$ref: "#/components/schemas/Issue"}}

metrics: {type: object}

ScoreResult:

type: object

properties: {outputs: {type: object}, latency_ms: {type: number}}

Issue:

type: object

properties:

code: {type: string}

message: {type: string}

path: {type: string}

see: {type: array, items: {type: string}}


V. 端点语义(要点)


VI. 错误码(规范性)

errors:

- {code:"ESCHEMA001", message:"missing required field", path:"$.title"}

- {code:"EREF001", message:"invalid reference format", path:"$.export_manifest.references[2]"}

- {code:"EMETR001", message:"units must be SI and check_dim=true", path:"$.metrology"}

- {code:"ESPLIT001", message:"ratios must sum to 1±1e-6", path:"$.evaluation.protocol"}

- {code:"ELEAK000", message:"cross-split leakage detected", path:"$.training_data.leakage_guards"}

- {code:"EIO001", message:"io_schema mismatch with deployment I/O", path:"$.io_schema"}

- {code:"EFAIR010", message:"fairness gap exceeds threshold", path:"$.fairness"}

- {code:"EROB010", message:"robustness drop exceeds threshold", path:"$.robustness"}

- {code:"ESIGN001", message:"artifact signature mismatch", path:"$.export_manifest.artifacts[0]"}


VII. 幂等性、版本协商与兼容性

idempotency:

header: "x-eift-idempotency"

window_hours: 24

versioning:

api: "modelcards.v1" # 破坏性变更 → 提升 MAJOR

minor: "向后兼容新增"

compatibility:

request_backward: "minor+patch"

response_fields: "新增仅追加,不移除"


VIII. 安全、审计与合规


IX. 机器可读实现片段(参考 Ixx-?)

def validate_model_card(card: dict) -> dict: ...

def lint_model_card(card: dict, rules: dict) -> dict: ...

def check_units(card: dict) -> dict: ...

def verify_references(card: dict) -> dict: ...

def split_freeze_check(card: dict) -> dict: ...

def leakage_audit(card: dict) -> dict: ...

def eval_run(card: dict, seeds: list[int], repeats: int) -> dict: ...

def eval_significance(baseline: dict, contender: dict) -> dict: ...

def calibration_eval(card: dict) -> dict: ...

def robustness_eval(card: dict) -> dict: ...

def fairness_eval(card: dict) -> dict: ...

def score(inputs: dict) -> dict: ...

def calibrate(probs: list[float], method: str="temperature") -> dict: ...

def explain(inputs: dict, method: str="grad") -> dict: ...

def hash_artifact(path: str|bytes) -> dict: ...

def sign_artifact(path: str|bytes, key_id: str) -> dict: ...

def publish_release(card: dict, channel="stable") -> dict: ...

def revoke_release(tag: str, reason: str) -> dict: ...

返回统一结构:{"ok": bool, "errors":[...], "warnings":[...], "metrics":{...}}。

X. 示例调用(可直接使用)

# 结构与跨卷校验

curl -s -X POST https://api.eift.org/api/v1/validate_model_card \

-H "Authorization: Bearer <token>" \

-H "x-eift-idempotency: 7b7a0b1e-0a21-4f3f-9d0b-3b1e9b1f3c22" \

-H "Content-Type: application/json" \

-d '{"card": { "model_id":"eift.vision.cls.resnet50", "version":"v1.0",

"metrology":{"units":"SI","check_dim":true},

"evaluation":{"protocol":{"splits":"frozen"},"metrics":{}},

"export_manifest":{"version":"v1.0","artifacts":[],

"references":["EFT.WP.Core.DataSpec v1.0:EXPORT",

"EFT.WP.Core.Metrology v1.0:check_dim"]}}}'


XI. 与导出清单的耦合(规范性)

export_manifest:

artifacts:

- {path:"api/openapi.yaml", sha256:"..."}

- {path:"api/clients/python.tar.gz", sha256:"..."}

references:

- "EFT.WP.Core.DataSpec v1.0:EXPORT"

- "EFT.WP.Core.Metrology v1.0:check_dim"

可校验并携带引用锚点。必须API 说明与客户端 SDK

XII. 本章合规自检


版权与许可(CC BY 4.0)

版权声明:除另有说明外,《能量丝理论》(含文本、图表、插图、符号与公式)的著作权由作者(“屠广林”先生)享有。
许可方式:本作品采用 Creative Commons 署名 4.0 国际许可协议(CC BY 4.0)进行许可;在注明作者与来源的前提下,允许为商业或非商业目的进行复制、转载、节选、改编与再分发。
署名格式(建议):作者:“屠广林”;作品:《能量丝理论》;来源:energyfilament.org;许可证:CC BY 4.0。

首次发布: 2025-11-11|当前版本:v5.1
协议链接:https://creativecommons.org/licenses/by/4.0/