跳转到内容

Python SDK 概述

ai-lib-python(v1.1.0)是 AI-Protocol 的 Python 运行时。与 Rust 的 workspace crate 不同,Python 以单一包发布,并清晰划分执行 / 策略模块

层级模块职责
执行层(E)clientprotocolpipelinetransporttypesstructured、可选能力模块清单加载、算子 pipeline、httpx transport
策略层(P)resiliencecacheroutingpluginsguardrailsbatchtelemetrytokens重试、限流、路由 — 在客户端旁按需启用

聊天场景下,AiClient 不会调用 ProviderDriver。流程为:

  1. 加载提供商清单(ProtocolLoader
  2. 根据清单算子构建 Pipeline
  3. 通过 HttpTransport(httpx)发送 HTTP
  4. 产出统一的 StreamingEvent(Pydantic 模型)

提供商特有逻辑仍然存在(SSE 解码器、可选 driver、独立服务客户端),但默认集成路径是清单 + pipeline。

始终从 ai_lib_python 导出:

  • AiClientAiClientBuilderChatResponseCallStats
  • MessageMessageRoleMessageContentContentBlockStreamingEventToolCallToolDefinition
  • AiLibErrorProtocolErrorTransportError
  • 能力探测:HAS_VISIONHAS_AUDIOHAS_TELEMETRYHAS_TOKENIZERHAS_WATCHDOGHAS_KEYRINGrequire_extra

需要时显式导入子包resiliencestructuredembeddingsmcpcomputer_usedrivers 等。

  • 清单加载: V1 + V2 路径(dist/v2/providers/*.jsonv2/providers/*.yaml、…)
  • 标准错误码: 13 个码(E1001–E9999)
  • 结构化输出: structured 模块
  • 文本工具 / TTC: ai_lib_python.types.text_tool 下的类型
  • 能力注册表: registry.CapabilityRegistry,含 pip-extra 检测
领域包内提供不包含
MCPMcpToolBridge 格式转换接入 AiClient 的 MCP 服务端传输
Computer UseComputerActionSafetyPolicy 校验截图 / 输入执行环境
Embeddings / STT / TTS / Rerank独立 HTTP 客户端各模态的完整 pipeline 算子
热更新Builder 标志 + 内存缓存自动文件监听(需 watchdog;未端到端接线)
ProviderDriver公开 drivers 模块默认 AiClient 聊天路径
  • AiClient — 异步入口(await AiClient.create("provider/model")
  • ChatRequestBuilder.messages().system().user().stream().execute().execute_with_stats()
  • AiClientBuilder.production_ready().hot_reload()、韧性相关旋钮
  • ProtocolLoaderProtocolManifest、校验器
  • V2 类型位于 protocol.v2

Decoder → Selector → Accumulator → FanOut → EventMapper(由清单配置)。

HttpTransport、凭证解析(keyring<PROVIDER>_API_KEY)。

按需启用的模块 — 使用 AiClientBuilder.production_ready() 或显式接线 ResilientConfigAiClient.create() 不会自动开启。

Extra启用内容
vision / audio多模态辅助(HAS_VISIONHAS_AUDIO
embeddingsEmbeddingClient
stt / tts / reranking独立服务客户端
batch批处理收集器 / 执行器
telemetryOpenTelemetry 集成
tokenizer基于 tiktoken 的计数
full全部可选能力 + watchdogkeyring
Terminal window
pip install ai-lib-python[full]