ApeMind Python SDK (apemind-sdk)
ApeMind 官方 Python SDK,基于仓库公开 OpenAPI 规范(docs/openapi/openapi.json)自动生成 Pydantic v2 数据模型与同步 / 异步 httpx 客户端,覆盖知识库、文档、检索与 Agent 对话核心业务链路。
- 在线文档:https://apemind.ai/docs/developers/python-sdk.html
- 完整 API 参考:https://apemind.ai/docs/api/index.html
- Python 版本要求:
>= 3.10 - 运行时依赖:仅
httpx与pydantic(v2)
安装
方式一:通过 PyPI 安装(推荐)
pip install apemind-sdk
方式二:安装 GitHub Release 构建包(支持内网/离线私有化环境)
每次发版 CI 会自动构建对应版本的 .whl、.tar.gz 与 apemind-sdk-SHA256SUMS.txt 并附在 GitHub Release 下:
# 下载对应版本的 wheel 后安装(可拷入离线环境执行)
pip install ./apemind_sdk-<version>-py3-none-any.whl
方式三:从源码目录安装
pip install ./sdk/python
快速开始
客户端默认读取环境变量 APEMIND_BASE_URL(默认 https://apemind.ai)、APEMIND_API_KEY 与可选的 APEMIND_WORKSPACE_ID,也支持在构造时显式传入:
from apemind import ApeMindClient, extract_turn_text
with ApeMindClient(
base_url="https://apemind.ai",
api_key="sk-...",
workspace_id="org_...",
) as client:
# 1. 创建知识库(声明式配置)
kb = client.knowledge_bases.create(title="产品制度库")
kb_id = kb.metadata.id
# 2. 两步上传文档并等待索引完成
uploaded = client.documents.upload(kb_id, "handbook.pdf", path="制度/handbook.pdf")
client.documents.confirm(kb_id, [uploaded.document_id])
doc = client.documents.wait_until_indexed(kb_id, uploaded.document_id)
# 3. 知识库混合检索(向量 + 全文 + 图谱)
search_res = client.search.collection(kb_id, "年假天数如何计算?", vector_topk=5, fulltext_topk=5)
for item in search_res.items or []:
print(item.rank, item.score, item.content[:80])
# 4. Agent 异步 Turn 对话与句级引用
chat = client.chats.create(agent_id="agent_...")
turn = client.chats.ask(chat.id, "请总结年假制度并给出依据条款。")
print(extract_turn_text(turn))
evidence = client.chats.get_turn_evidence(chat.id, turn.turn_id)
需要异步调用时使用 AsyncApeMindClient(接口与同步版一致):
from apemind import AsyncApeMindClient
async with AsyncApeMindClient(base_url="https://apemind.ai", api_key="sk-...") as client:
res = await client.search.collection("kb_...", "合同解除条件")
覆盖能力与客户端分层
- 高层资源接口(推荐日常使用):
client.knowledge_bases:声明式创建/查询/局部更新、列表、索引状态汇总、重建失败索引、删除client.documents:单步上传、两步暂存上传 + 确认、URL 抓取、列表/详情、内容读写、原文件下载、删除、索引轮询等待wait_until_indexed()client.search:单知识库多路混合检索collection()、跨库检索workspace()、检索历史、grep()/read()client.agents:声明式创建/列表/查询/局部更新/删除 Agentclient.chats:会话管理、异步 Turn 创建/轮询/等待/取消/句级证据、OpenAI 兼容同步chat_completions()与流式stream_chat_completions()client.me()/client.list_workspaces():当前认证身份与可用组织工作空间
- 底层自动生成绑定:通过
client.api直接访问所有 65 个从 OpenAPI 自动生成的强类型方法,通过apemind.models访问所有自动生成的 Pydantic v2 模型。
示例代码
examples/01_knowledge_base_and_documents.py:声明式建库、两步上传确认与索引等待examples/02_hybrid_search.py:知识库三路混合检索与工作区跨库检索examples/03_agent_chat_turn.py:原生异步 Turn 对话与句级引用证据提取examples/04_openai_compat_chat.py:OpenAI 兼容流式对话
从 OpenAPI 重新生成与测试
# 重新生成 src/apemind/_generated/{models.py,api.py,__init__.py}
python3 sdk/python/scripts/generate_sdk.py
# 校验无漂移(CI 门禁同款)
python3 sdk/python/scripts/generate_sdk.py --check
# 运行单元测试与代码检查
PYTHONPATH=sdk/python/src pytest sdk/python/tests -q
ruff check sdk/python/src sdk/python/scripts sdk/python/tests sdk/python/examples
发布机制
仓库每次发布 v* 正式 Release(或推送 sdk/v* Tag)时,.github/workflows/release-python-sdk.yml 会自动:
- 将 Tag 转换为 PEP 440 版本号(如
v2.12.33→2.12.33); - 校验 OpenAPI 零漂移并运行 SDK 单元测试;
- 构建
.whl、.tar.gz与apemind-sdk-SHA256SUMS.txt,并在干净虚拟环境中验证 wheel 安装与导入; - 上传构建产物到对应 GitHub Release;
- 当仓库变量
APEMIND_PYPI_PUBLISH=true(稳定版本号vX.Y.Z/sdk/vX.Y.Z)或手动触发workflow_dispatch勾选publish_pypi时,通过pypiGitHub Environment(支持 PyPI Trusted Publishing OIDC 或PYPI_API_TOKEN)自动发布到 PyPI。
Metadata
Release files for apemind-sdk 0.1.0
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 |
|---|---|---|---|---|
| apemind_sdk-0.1.0.tar.gz | 48.7 kB | With provenance | Details for apemind_sdk-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| apemind_sdk-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for apemind_sdk-0.1.0-py3-none-any.whl |
Total release size: 91.5 kB
Release files / apemind_sdk-0.1.0.tar.gz
| Download URL | apemind_sdk-0.1.0.tar.gz |
|---|---|
| Size | 48.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6131f736f4cd05e79d17f336e909eaf2320530b47cdad2a2f31c6e0c5061987d
|
|
BLAKE2b-256 checksum How to use checksums |
0f48aa36e01c2f3154fa8d8cba58fac29866c4ebb4ca467773d6ac58c80f6a00
|
| 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 Nov 26, 2026.
Transparency logRelease files / apemind_sdk-0.1.0-py3-none-any.whl
| Download URL | apemind_sdk-0.1.0-py3-none-any.whl |
|---|---|
| Size | 42.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d0ce47e4b3fca38aea66c181333da6ff8db204a4bdc2e70d8abb85329b64fd85
|
|
BLAKE2b-256 checksum How to use checksums |
e4bed97e92fb6c8b73b6a14864d50a996a43fb4691d09608318505bff896b10b
|
| 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 Nov 26, 2026.
Transparency logSecurity for apemind-sdk 0.1.0
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
Both files in this release have been uploaded with provenance metadata.
Attestation for apemind_sdk-0.1.0.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 Nov 26, 2026.
Transparency logA 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.