Skip to main content

zhuye (Python SDK)

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

安装

pip install zhuye

需要本机有 zhuye 二进制在 PATH(或通过 ZHUYE_BIN 指向)。从 GitHub Releases 下载,或从本仓库 make build 产出 target/release/zhuye。

用法

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 二进制路径(默认搜索 PATH)。
ZHUYE_MODEL 模型 spec(mock/mock-1 用于离线 SDK 集成测试)。
ZHUYE_REMOTE_TOOL_TIMEOUT_SECS 远程工具等待客户端回执的上限,默认 120s。

测试

pip install -e .[dev]
pytest -ra

mock serve 进程跑在 Node 上(Node ≥ 18),不依赖真 zhuye 二进制 —— 测试完全离线,也不需要网络/API key。

端到端测试(对真 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.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 zhuye 0.1.1
File Size Uploaded Provenance Actions
zhuye-0.1.1.tar.gz 17.3 kB With provenance Details for zhuye-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for zhuye 0.1.1
File Interpreter ABI Platform Actions
zhuye-0.1.1-py3-none-any.whl Python 3 none any Details for zhuye-0.1.1-py3-none-any.whl

Total release size: 31.4 kB

Release files / zhuye-0.1.1.tar.gz

Download URL zhuye-0.1.1.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
cc9cf7cbc767f57248b1e2408c5e5b7fd0cf74bffbb200a9d55da67bc8fdc3e3
BLAKE2b-256 checksum
How to use checksums
25c47a8a27f872659b04669b8ffffd768e81855d9d3dd85b385745679dd5bd6a
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 28, 2026.

Transparency log

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

Download URL zhuye-0.1.1-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
82d5665dcf4e5ccf890c67802d9ba9c94f88fec7f2ecb7fe505039fc8ce0a13f
BLAKE2b-256 checksum
How to use checksums
24cf5e6fe38d365305d86ab97e1dc745e86cf06ff500c5cb045e2ee39d8a0cb0
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 28, 2026.

Transparency log

Security for zhuye 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.

Full provenance

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

Select from 2 files

Attestation for zhuye-0.1.1.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 28, 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