Skip to main content

zhuye (Python SDK)

让 Python 项目直接驱动 Zhuye Agent 的轻量 SDK。底层通过 spawn zhuye serve 子进程、走 JSONL stdio 协议,零运行时依赖(仅 stdlib)。

安装

pip install zhuye

安装即用:PyPI 提供平台 wheel(macOS arm64 / x86_64,Linux x86_64 / aarch64 glibc,Windows x86_64),内嵌对应平台的 zhuye 二进制,SDK 优先解析它 —— 无需自装二进制、无需配环境变量。

二进制解析优先级:显式 options.bin → ZHUYE_BIN → wheel 内嵌 二进制 → PATH 上的 zhuye。当前平台不在支持矩阵(如 musl/Alpine) 时回退 PATH:从 GitHub Releases 下载,或从本仓库 make build 产出 target/release/zhuye 后以 ZHUYE_BIN 指向。

用法

from zhuye import ZhuyeAgent, ToolOutput, ContentBlock

agent = ZhuyeAgent.spawn()

# 注册客户端自定义工具(LLM 调用时由本地函数返回结果)
def get_weather(args):
    return ToolOutput(
        content=[ContentBlock(type="text", text=f"{args['city']} 今天晴 26℃")],
        is_error=False,
        metadata={},
        elapsed_ms=0,
    )

agent.register_tool(
    name="get_weather",
    description="查询城市天气",
    parameters={
        "type": "object",
        "properties": {"city": {"type": "string"}},
        "required": ["city"],
    },
    handler=get_weather,
)

# 高层:聚合 delta,返回最终文本
print(agent.prompt("北京天气如何?"))

# 低层:遍历 turn 事件
for ev in agent.submit("第二轮"):
    msg = ev["msg"]
    if msg["type"] == "agent_message_delta":
        print(msg["delta"], end="", flush=True)
    if msg["type"] == "turn_complete":
        break

agent.close()

全局事件

next_global_event(timeout_secs=5) 读取 id="" 的会话事件,与 submit() 的 按 submission 路由的事件迭代器独立。超时抛 queue.Empty,进程 stdout 关闭后返回 None;未知变体原样返回。

handshake = agent.next_global_event(timeout_secs=5)

环境变量

变量 作用
ZHUYE_BIN zhuye 二进制路径(最高优先级覆盖;缺省用 wheel 内嵌二进制)。
ZHUYE_MODEL 模型 spec(mock/mock-1 用于离线 SDK 集成测试)。
ZHUYE_REMOTE_TOOL_TIMEOUT_SECS 远程工具等待客户端回执的上限,默认 120s。

测试

python3 -m pip install pytest
python3 -m pytest -ra

mock serve 进程跑在 Node 上(Node ≥ 18),不依赖真 zhuye 二进制 —— 测试完全离线,也不需要网络/API key。仓库内开发直接以源码树运行(python -m pytest 会把本目录加入 sys.path),无需构建安装;打包后端为 maturin(平台 wheel 由 CI 产出并内嵌二进制,见 docs/release.md),本地构建需 Rust 工具链。

端到端测试(对真 zhuye serve 子进程的集成)在仓库 CI 里跑;在仓库根配置好 ZHUYE_BIN 后执行 ./tests/run_all.sh sdk-python。

协议

SDK 与 zhuye serve 之间的 wire 协议见 ../PROTOCOL.md(同仓库 sdks/PROTOCOL.md)。Rust 核心新增协议变体时,SDK 自动降为 UnknownEventMsg,不会中断下游。

Metadata

Release files for zhuye 0.1.3

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

Source distribution (sdist)

Source distribution for zhuye 0.1.3
File Size Uploaded Provenance Actions
zhuye-0.1.3.tar.gz 13.5 kB With provenance Details for zhuye-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for zhuye 0.1.3
File Interpreter ABI Platform Actions
zhuye-0.1.3-py3-none-manylinux_2_34_x86_64.whl Python 3 none Linux glibc 2.34+ x86-64 Details for zhuye-0.1.3-py3-none-manylinux_2_34_x86_64.whl

Total release size: 14.3 MB

Release files / zhuye-0.1.3.tar.gz

Download URL zhuye-0.1.3.tar.gz
Size 13.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a60343036808f8a04646aa259bcd09fec679eff5b85d99757e86e1b7c7f90f0e
BLAKE2b-256 checksum
How to use checksums
8c860aa169d2c0817151035efc30a5e2031d8f09562a4e967b11c5f91e6e54b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Release files / zhuye-0.1.3-py3-none-manylinux_2_34_x86_64.whl

Download URL zhuye-0.1.3-py3-none-manylinux_2_34_x86_64.whl
Size 14.3 MB
Tags Linux glibc 2.34+ x86-64 Python 3
SHA-256 checksum
How to use checksums
abb25f3360fc1777de436ee793d4571bcda1970379c7c00a4966e50aa77a04b0
BLAKE2b-256 checksum
How to use checksums
35777f8d83f63de1bee8896e8df8e68ae6d5973f12d94a89a082cf270ae08d69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Security for zhuye 0.1.3

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for zhuye-0.1.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

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