ikc-ai-gateway-sdk
SDK 0.5.1 提供通用 ModelRecord 管理客户端 CRUD 与草稿/已存配置测试;类型来自 shared 0.37.0。管理客户端区分幂等 reqId 与链路 traceId,不提供 Snapshot apply 或运行时授权 API。gRPC extra 要求 grpcio>=1.84.0、protobuf>=7.35.1,<8,与固定生成器版本一致。
AI Gateway 的传输客户端 SDK:经 HTTP/SSE 或 gRPC 调用网关的六类能力(文本对话 / vLLM / 向量化 / 重排 / 语音识别 / 多模态),自动解包统一响应壳 Envelope、回显 traceId/reqId,非成功码抛异常。
SDK 只负责传输,不定义业务 schema:请求/响应可直接传 camelCase dict,也可使用可选 models extra 提供的类型化模型。
安装
pip install ikc-ai-gateway-sdk # HTTP/SSE + CLI(最小依赖 httpx / pydantic)
pip install "ikc-ai-gateway-sdk[grpc]" # 追加 gRPC 通道(grpcio / protobuf)
[models]/[all]额外引入类型化业务模型(可选的私有依赖)。
快速开始(HTTP / SSE)
from ikc_ai_gateway_sdk import AIGatewayClient, AIGatewayError
with AIGatewayClient("http://127.0.0.1:18100", "<api-key>") as gw:
# 同步对话 —— 返回解包后的 Envelope dict
r = gw.chat({"model": "<model>",
"messages": [{"role": "user", "content": "你好"}]})
print(r["data"]["text"], r["traceId"])
# 流式对话(SSE)—— 逐块 yield data: 文本,末尾 [DONE]
for chunk in gw.chat({"model": "<model>",
"messages": [{"role": "user", "content": "讲个笑话"}]},
stream=True):
if chunk.strip() == "[DONE]":
break
print(chunk, end="", flush=True)
# 向量化 / 重排
gw.embed({"model": "<embed-model>", "input": ["你好", "世界"]})
gw.rerank({"model": "<rerank-model>", "query": "…",
"passages": ["…", "…"], "topK": 2})
异步长任务(语音识别 / 多模态:受理 + 轮询)
acc = gw.transcribe({"source": {"inputUrl": "https://example.com/a.wav", "format": "wav"}},
async_mode=True) # 202 受理,顶层带 taskId
view = gw.get_task(acc["taskId"], domain="asr")["data"] # 自动跟随嵌套 taskId 至终态
动态参数(头通道)
headers= 透传自定义头(X-AI-Provider / X-AI-Model / X-AI-Hyper-Params / X-AI-Model-Info):
gw.chat({"messages": [{"role": "user", "content": "hi"}]},
headers={"X-AI-Provider": "openai", "X-AI-Model": "<model>",
"X-AI-Hyper-Params": '{"temperature":0.2}'})
gRPC 通道
client.grpc 懒连接(需 [grpc] extra;gRPC 桩已内置于 wheel):
resp = gw.grpc.chat({"model": "<model>",
"messages": [{"role": "user", "content": "hi"}]}, capability="llm")
print(resp["data"]["text"], resp["traceId"])
for chunk in gw.grpc.chat_stream({"model": "<model>",
"messages": [{"role": "user", "content": "讲个笑话"}]}):
if chunk["done"]:
break
print(chunk["delta"], end="", flush=True)
CLI(ikc-aigw)
地址/凭证经参数或环境变量 AIGW_BASE_URL(默认 http://localhost:18100)/ AIGW_API_KEY;请求体经 --body(JSON 串)、--file(JSON 文件)或 stdin 管道。
export AIGW_BASE_URL=http://127.0.0.1:18100 AIGW_API_KEY=<key>
ikc-aigw chat --body '{"model":"<model>","messages":[{"role":"user","content":"hi"}]}'
ikc-aigw chat --stream --body '{"model":"<model>","messages":[{"role":"user","content":"讲个笑话"}]}'
ikc-aigw embed --file req.json
cat req.json | ikc-aigw rerank
ikc-aigw transcribe --async --body '{"source":{"inputUrl":"https://example.com/a.wav","format":"wav"}}'
ikc-aigw task <taskId> --domain asr
ikc-aigw analyze --body '{"source":{"kind":"image","inputUrl":"https://example.com/x.jpg","prompt":"描述这张图"}}'
异常
| 异常 | 触发 |
|---|---|
AIGatewayError |
Envelope errCode 非成功码(携带 err_code / err_msg / trace_id / req_id / data) |
TransportError |
网络不可达 / 超时 / 非法 JSON 响应 |
StreamError |
SSE 流式传输中断 |
方法一览
| 方法 | HTTP | 说明 |
|---|---|---|
chat(request, stream=False, headers=None) |
POST /api/v1/chat/completions |
stream=True 返回 SSE 迭代器 |
vllm_chat(request, stream=False, headers=None) |
POST /api/v1/vllm/chat/completions |
同上 |
embed(request, headers=None) |
POST /api/v1/embeddings |
返回解包后的 Envelope dict |
rerank(request, headers=None) |
POST /api/v1/rerank |
|
transcribe(request, async_mode=False, headers=None) |
POST /api/v1/asr/transcribe |
async_mode=True → ?async=true 受理 |
analyze(request, async_mode=False, headers=None) |
POST /api/v1/mm/analyze |
同上 |
get_task(task_id, domain="asr") |
GET /api/v1/{domain}/result/{task_id} |
轮询异步任务(domain ∈ asr / mm) |
2026-10-19 本地构建
0.2.0 新增独立 AIGatewayManagementClient,管理凭证与推理凭证分离;补齐独立 gRPC 桩。 本次构建 wheel/sdist,未上传 PyPI。
Metadata
Release files for ikc-ai-gateway-sdk 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ikc_ai_gateway_sdk-0.5.1.tar.gz | 15.5 kB | Without provenance | Details for ikc_ai_gateway_sdk-0.5.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ikc_ai_gateway_sdk-0.5.1-py3-none-any.whl | Python 3 | none | any | Details for ikc_ai_gateway_sdk-0.5.1-py3-none-any.whl |
Total release size: 34.6 kB
Release files / ikc_ai_gateway_sdk-0.5.1.tar.gz
| Download URL | ikc_ai_gateway_sdk-0.5.1.tar.gz |
|---|---|
| Size | 15.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8e85e37241e3f6cad238eb8b33334da9d78f04140241d0b22b298cfcce62c667
|
|
BLAKE2b-256 checksum How to use checksums |
262c1395391ad9b43e80c9c36f3834f038dadd66d5e2584c95850ed0b1fc0d1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / ikc_ai_gateway_sdk-0.5.1-py3-none-any.whl
| Download URL | ikc_ai_gateway_sdk-0.5.1-py3-none-any.whl |
|---|---|
| Size | 19.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e0342261f2017b9a8c2b52ab6236df92b14fc6a899865764c0ffc6054fec850d
|
|
BLAKE2b-256 checksum How to use checksums |
561cfbeb468057782e52ac75d41a87ea34df6d8ff41d25d3b98d0f298a36a978
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for ikc-ai-gateway-sdk 0.5.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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.