Skip to main content

seaart-sdk

SeaArt AI 平台 Python SDK,按 seaart_sdk_go 的公开接口翻译实现,当前提供三类能力:

  • client.modal / client.Modal:多模态任务接口
  • client.llm / client.LLM:LLM 透传接口
  • client.passthrough / client.Passthrough:厂商原始 API 透传接口

特点:

  • 纯标准库实现,无第三方运行时依赖
  • 保留原始请求透传能力
  • 支持 SSE 流式响应解析
  • 支持任务轮询和通用 task builder

安装

本地开发:

pip install -e .

要求:

  • Python 3.10+

初始化

import seaart_sdk as sa

client = sa.Client(
    sa.ClientConfig(
        api_key="sa-your-api-key",
    )
)

默认网关配置:

  • base_url: https://gateway.example.com
  • model_base_url: https://gateway.example.com/model
  • llm_base_url: https://gateway.example.com/llm
  • passthrough_base_url: https://gateway.example.com/model

也可以分别覆盖:

client = sa.Client(
    sa.ClientConfig(
        api_key="sa-your-api-key",
        base_url="https://gateway.example.com",
        model_base_url="https://mm-gateway.example.com",
        llm_base_url="https://llm-gateway.example.com",
        passthrough_base_url="https://mm-gateway.example.com",
        timeout=60.0,
        project="my-project",
    )
)

Modal API

原始透传请求:

task = client.modal.create(
    {
        "model": "vidu_q3_reference",
        "input": [
            {
                "type": "message",
                "role": "user",
                "content": [
                    {"type": "text", "text": "cinematic shot"},
                    {"type": "image_url", "url": "https://example.com/ref1.webp"},
                    {"type": "image_url", "url": "https://example.com/ref2.webp"},
                ],
            }
        ],
        "parameters": {"duration": 5},
    },
    sa.WithHeader("X-Trace-Id", "trace-123"),
)

print(task.id, task.status)

等待任务完成:

task = client.modal.wait(
    "task_abc123",
    sa.WithPollInterval(3.0),
    sa.WithPollTimeout(300.0),
)

print(task.status, task.progress, task.urls())

也可以在创建后继续等待:

task = client.modal.create({"model": "vidu_q3_reference"})
task = task.wait(sa.WithPollInterval(3.0))

Typed helper:

body = (
    sa.NewTask("vidu_q3_reference")
    .user(
        sa.Text("cinematic shot"),
        sa.ImageURL("https://example.com/ref1.webp"),
        sa.ImageURL("https://example.com/ref2.webp"),
    )
    .param("duration", 5)
    .metadata("trace_id", "trace-123")
    .build()
)

task = client.modal.create(body)

模型列表和参数详情:

models = client.modal.list_models(
    sa.ModelSearchParams(
        query="",
        limit=2,
    )
)
for hit in models.hits:
    print(hit["name"])

skill = client.modal.get_model_skill("alibaba_animate_anyone_detect")
print(skill)

list_models / search_models 支持的查询参数:

  • query -> q
  • input -> input
  • output -> output
  • type -> type
  • provider -> provider
  • limit -> limit

图片/视频鉴黄:

鉴黄接口复用 model_base_url,对应 POST /v1/image/scan。请求会通过 openresty 转发到 inference-gateway。

result = client.modal.scan_image(
    sa.ImageScanRequest(
        uri="https://example.com/image.jpg",
        risk_types=[
            sa.ImageScanRiskTypePolity,
            sa.ImageScanRiskTypeErotic,
            sa.ImageScanRiskTypeViolent,
            sa.ImageScanRiskTypeChild,
        ],
        detected_age=0,
        is_video=0,
    )
)

print(result.ok, result.nsfw_level, result.risk_types)

也可以直接传 dict。视频检测时设置 is_video=1,并可传 duration 用于计费:

result = client.modal.scan_image({
    "uri": "https://example.com/video.mp4",
    "risk_types": [sa.ImageScanRiskTypeErotic, sa.ImageScanRiskTypeViolent],
    "is_video": 1,
    "duration": 12.5,
})

常用响应字段包括 ok、nsfw_level、label_items、risk_types、frame_results 和 usage。

风险类型说明:

常量 接口值 说明
sa.ImageScanRiskTypePolity POLITY 政治敏感、公共安全等风险内容
sa.ImageScanRiskTypeErotic EROTIC 色情、裸露、性暗示等成人内容
sa.ImageScanRiskTypeViolent VIOLENT 暴力、血腥、武器、伤害等内容
sa.ImageScanRiskTypeChild CHILD 儿童安全风险,尤其是儿童相关不安全或性化内容

敏感词检测:

敏感词检测接口复用 model_base_url,对应 POST /v1/text/scan。

result = client.modal.scan_text(
    sa.TextScanRequest(
        text="prompt to check",
        scene=1,
        area_types=[sa.TextScanAreaTypeForeign],
        way=sa.TextScanWayDictionary,
    )
)

print(result.usage)
print(result.status.code, result.status.msg)
print(result.data.sensitive_words)

area_types 可选 TextScanAreaTypeAll、TextScanAreaTypeDomestic、TextScanAreaTypeForeign。way 可选 TextScanWayDictionary、TextScanWayModel、TextScanWayMixed、TextScanWayCharacter。敏感词索引 start_index / end_index 基于 rune 数组,网关注入的计费信息在 result.usage。

人脸检测:

人脸检测接口复用 model_base_url,对应 POST /v1/face/scan,由 openresty 转发到 inference-gateway,再转发到上游 /cloud/face/scan。

result = client.modal.scan_face(
    sa.FaceScanRequest(
        uri="https://example.com/image.jpg",
        is_video=0,
        scene="avatar",
    )
)

print(result.ok, result.usage)
print(result.extra.get("face_count"))

也可以传 img_base64。视频检测时设置 is_video=1,并可传 duration 用于计费。上游人脸检测返回结构会保留在 result.extra,网关注入的计费信息在 result.usage。

Passthrough API

Passthrough 层保留厂商原始 API 形态。路径需要带厂商前缀,例如 /kling/...、/vidu/...、/google/...。

resp = client.passthrough.post(
    "/kling/v1/videos/text2video",
    {
        "model_name": "kling-v1",
        "prompt": "cinematic shot",
    },
    sa.WithHeader("X-Trace-Id", "trace-123"),
)

print(resp.status_code, resp.body.decode("utf-8"))

如果要完全透传原始 JSON 字节,使用 request_raw:

resp = client.passthrough.request_raw(
    "POST",
    "/google/v1beta/models/gemini-2.5-flash-image:generateContent",
    b'{"contents":[{"parts":[{"text":"paint a cat"}]}]}',
)

当前提供:

  • request
  • request_raw
  • get
  • post
  • put
  • delete

LLM API

LLM 层保持“请求透传 + 原始响应返回”的形式:

raw = client.llm.chat_completions(
    {
        "model": "gpt-4o-mini",
        "messages": [{"role": "user", "content": "hello"}],
        "max_tokens": 64,
    },
    sa.WithHeader("X-Trace-Id", "trace-123"),
)

resp = sa.Decode(raw, sa.ChatCompletionResponse)
print(resp.choices[0].message.content)

当前支持:

  • chat_completions
  • chat_completions_stream
  • messages
  • messages_stream
  • responses
  • responses_stream
  • rerank
  • embeddings
  • list_models

流式响应示例:

stream = client.llm.chat_completions_stream(
    {
        "model": "gpt-4o-mini",
        "messages": [{"role": "user", "content": "hello"}],
    }
)

for event in stream:
    if event.err:
        raise event.err
    if event.done:
        break
    chunk = sa.Decode(event.data, sa.ChatCompletionResponse)
    print(chunk.choices[0].delta.content)

测试

python3 -m unittest discover -s tests -v

发布

本地打包与检查:

make check

发布到默认 twine 仓库配置:

make release

发布到 GitLab Package Registry:

make release-gitlab \
  TWINE_REPOSITORY_URL="https://<gitlab-host>/api/v4/projects/<project_id>/packages/pypi" \
  TWINE_USERNAME="__token__" \
  TWINE_PASSWORD="<token>"

GitLab CI 已配置为在打 tag 时自动构建并发布到当前项目的 Package Registry:

git tag v0.1.0
git push origin v0.1.0

Metadata

Release files for seaart-sdk 0.1.4

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

Source distribution (sdist)

Source distribution for seaart-sdk 0.1.4
File Size Uploaded Provenance
seaart_sdk-0.1.4.tar.gz 28.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for seaart-sdk 0.1.4
File Interpreter ABI Platform
seaart_sdk-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 52.4 kB

Release files / seaart_sdk-0.1.4.tar.gz

Download URL seaart_sdk-0.1.4.tar.gz
Size 28.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7891dc7917a698194cebde6f51c5a237fd7970ee42e28767d22aff3e639e420f
BLAKE2b-256 checksum
How to use checksums
5923678d07631d5d5c2e8b960b3c575d9cf03f8a79354e84f55018088cf173b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release files / seaart_sdk-0.1.4-py3-none-any.whl

Download URL seaart_sdk-0.1.4-py3-none-any.whl
Size 23.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
19c65dfbcb937df29eecb82866eef34382d95d2130c1f95c73bc519745f5ee60
BLAKE2b-256 checksum
How to use checksums
fbe38dd18f6121864a65e36945cc6bc5586184714ce43dbc5db23c3c980b61a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Security for seaart-sdk 0.1.4

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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