Skip to main content

Huace AIGC MaaS Python SDK

这是用于接入 new-api MaaS 托管 Key 能力的 Python 客户端。它负责申请或复用托管 Key、查询 Key 元信息和查询用量,不是 AIGC Auth SDK,也不代替模型协议客户端。

SDK 仅依赖 Python 标准库,支持 Python 3.10 及更高版本。

安装

pip install huace-aigc-maas-client

使用前准备

调用 SDK 前,请确认服务端已经完成以下配置:

  • base_url 是 MaaS/new-api 服务地址,并使用 HTTPS。
  • app_id 是已注册的正整数 AIGC Auth 应用 ID。
  • 应用已启用,并且恰好配置一个启用的 MaaS 路由。
  • 当前用户与服务端人员的映射已经确认,否则会返回 MAPPING_REVIEW_PENDING。
  • 申请托管 Key 时不需要传 channel_code 或来源应用 Secret。

快速开始

ensure_key 使用当前用户从 AIGC Auth 获取的用户 Token,申请或复用一个托管 Key:

from huace_aigc_maas import APIError, Client, EnsureKeyRequest, UsageQuery

client = Client("https://maas.example.com", "12")

try:
    ensured = client.ensure_key(
        EnsureKeyRequest(
            user_token="auth-user-token",
            idempotency_key="user-42-provision-v1",
        )
    )

    # 托管 Key 只在服务端使用,请勿写入日志或返回浏览器。
    api_key = ensured.credential.api_key
    base_url = ensured.credential.base_url
    models = ensured.credential.models

    key_info = client.get_key_info(api_key)
    usage = client.get_usage(api_key, UsageQuery())
except APIError as exc:
    print(exc.status_code, exc.code, exc.message)
    raise

ensure_key 返回 EnsureKeyResult,其中包括:

  • state:本次申请或复用的状态。
  • app:应用信息,包括 id 和 name。
  • user:用户映射信息,包括本地用户 ID 和 AIGC Auth 用户 ID。
  • credential:托管凭证,包括 api_key、base_url 和可用 models。

同一个应用和用户会复用同一个托管 Key。未传 idempotency_key 时,SDK 会自动生成 UUID, 并在重试请求中复用该值。

模型请求由调用方使用返回的 base_url 和 api_key 完成。例如,模型接口地址通常为 {base_url}/v1/chat/completions,请求头使用 Authorization: Bearer <api_key>。

查询 Key 和用量

查询 Key 信息

key_info = client.get_key_info(api_key)
print(key_info.status, key_info.models, key_info.remain_quota)

get_key_info 返回 Key 状态、所属应用和用户、可用模型、额度等信息,不会返回明文 Key。

查询用量

不传时间范围时,查询服务端默认统计范围:

usage = client.get_usage(api_key)
print(usage.summary.count, usage.summary.token_used)

也可以使用 Unix 时间戳限定查询范围:

usage = client.get_usage(
    api_key,
    UsageQuery(start_timestamp=1722470400, end_timestamp=1725148800),
)

for model in usage.models:
    print(model.model_name, model.count, model.token_used)

接口和鉴权

方法 路径 Authorization 使用的凭证 说明
POST /api/integrations/v1/keys/ensure AIGC Auth 用户 Token 申请或复用托管 Key
GET /api/integrations/v1/self/key MaaS API Key 查询 Key 信息
GET /api/integrations/v1/self/usage MaaS API Key 查询用量

申请接口还会发送以下请求头:

  • X-AIGC-Auth-App-Id: <app_id>
  • Idempotency-Key: <stable value>

SDK 会自动添加 Authorization: Bearer ...。如果传入的凭证已经带有 Bearer 前缀,SDK 会规范化后再发送。

超时、重试和错误处理

client = Client(
    "https://maas.example.com",
    "12",
    timeout=10.0,
    max_retries=2,
    retry_base_delay=0.1,
)
  • 默认请求超时为 10 秒。
  • 默认最多重试 2 次,采用指数退避。
  • 网络错误、HTTP 429 和 HTTP 5xx 会自动重试。
  • 其他 HTTP 4xx 错误不会自动重试。
  • 服务端错误会抛出 APIError,可读取 status_code、code 和 message。

参数格式错误(例如 base_url 缺少协议或 app_id 不是正整数)会直接抛出 ValueError。

安全边界

  • user_token 只传给 ensure_key,Client 不会保存它。
  • 托管 API Key 只能在受信任的服务端保存和使用,不要让浏览器直接持有。
  • 所有请求都应通过 HTTPS 传输。
  • 不要把用户 Token、托管 API Key 写入日志、URL、指标或错误上报。
  • 不要向申请接口传来源应用 Secret。

相关文档

本地测试

在 SDK 源码目录执行:

python -m unittest discover -s tests -v

Metadata

Release files for huace-aigc-maas-client 0.1.1

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

Source distribution (sdist)

Source distribution for huace-aigc-maas-client 0.1.1
File Size Uploaded Provenance
huace_aigc_maas_client-0.1.1.tar.gz 6.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for huace-aigc-maas-client 0.1.1
File Interpreter ABI Platform
huace_aigc_maas_client-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.4 kB

Release files / huace_aigc_maas_client-0.1.1.tar.gz

Download URL huace_aigc_maas_client-0.1.1.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
889ac412edf96c90700c706c89491ac44b740588214a6c0fae4118ae706be808
BLAKE2b-256 checksum
How to use checksums
e97339fde4bf8364aa7827638cb13948de16271cb5e13b5fdc3dfea8f344079f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.14

Release files / huace_aigc_maas_client-0.1.1-py3-none-any.whl

Download URL huace_aigc_maas_client-0.1.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b4ac5674d0d399d043c23c31eacb8e82ac5492fca523e56e420b3cab471ca5e
BLAKE2b-256 checksum
How to use checksums
f204c6f84fb7ee845eb45e5db968a968e279845ffb12e1e0326d8ba36bed50d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.14

Security for huace-aigc-maas-client 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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