Python SDK

将 Alephant 集成到您的 Python 应用程序中

以 Markdown 格式查看

官方 Alephant Python SDK 让 Python 应用程序能够访问 Alephant SaaS API。它同时支持同步和异步使用,并提供完整的类型提示。

安装

通过 pip 安装 SDK:

$pip install alephantai-saas-api

初始化

您可以根据应用程序架构初始化同步或异步客户端。

同步客户端

1import os
2from alephantai_saas import AlephantAi318537Api
3
4client = AlephantAi318537Api(
5 base_url=os.environ["ALEPHANT_SAAS_BASE_URL"],
6 api_key=f"Bearer {os.environ['ALEPHANT_API_TOKEN']}",
7)

异步客户端

适用于 FastAPI 或 Starlette 等现代异步框架:

1import os
2from alephantai_saas import AsyncAlephantAi318537Api
3
4async_client = AsyncAlephantAi318537Api(
5 base_url=os.environ["ALEPHANT_SAAS_BASE_URL"],
6 api_key=f"Bearer {os.environ['ALEPHANT_API_TOKEN']}",
7)

使用示例

冒烟测试

使用 health 端点验证 SDK 能否连接到已配置的 SaaS API 主机。

1response = client.system.health_check()
2print(response)

列出工作区

1def fetch_workspaces():
2 try:
3 response = client.workspaces.list_users_workspaces()
4 for workspace in response.data:
5 print(f"Workspace: {workspace.name} (ID: {workspace.id})")
6 except Exception as e:
7 print(f"Error fetching workspaces: {e}")

获取 Agent 分析数据

1async def get_agent_metrics(agent_id: str):
2 analytics = await async_client.analytics.get_agent_analytics(
3 agent_id=agent_id,
4 period="7d"
5 )
6 print(f"Total Cost: ${analytics.total_cost}")
7 print(f"Total Requests: {analytics.total_requests}")

基础 URL

SaaS SDK 需要显式指定 base_url。请根据您的环境使用开发、生产或私有化部署主机。

1client = AlephantAi318537Api(
2 base_url="https://alephant.your-company.com/api",
3 api_key="Bearer <token>",
4)