python-sdk

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python SDK

Python SDK

Build AI applications with the inference.sh Python SDK.
Python SDK
使用inference.sh Python SDK构建AI应用。
Python SDK

Quick Start

快速开始

bash
pip install inferencesh
python
from inferencesh import inference

client = inference(api_key="inf_your_key")
bash
pip install inferencesh
python
from inferencesh import inference

client = inference(api_key="inf_your_key")

Run an AI app

运行AI应用

result = client.run({ "app": "infsh/flux-schnell", "input": {"prompt": "A sunset over mountains"} }) print(result["output"])
undefined
result = client.run({ "app": "infsh/flux-schnell", "input": {"prompt": "A sunset over mountains"} }) print(result["output"])
undefined

Installation

安装

bash
undefined
bash
undefined

Standard installation

标准安装

pip install inferencesh
pip install inferencesh

With async support

带异步支持的安装

pip install inferencesh[async]

**Requirements:** Python 3.8+
pip install inferencesh[async]

**要求:** Python 3.8+

Authentication

身份验证

python
import os
from inferencesh import inference
python
import os
from inferencesh import inference

Direct API key

直接传入API密钥

client = inference(api_key="inf_your_key")
client = inference(api_key="inf_your_key")

From environment variable (recommended)

从环境变量获取(推荐方式)

client = inference(api_key=os.environ["INFERENCE_API_KEY"])

Get your API key: Settings → API Keys → Create API Key
client = inference(api_key=os.environ["INFERENCE_API_KEY"])

获取API密钥:设置 → API密钥 → 创建API密钥

Running Apps

运行应用

Basic Execution

基础执行

python
result = client.run({
    "app": "infsh/flux-schnell",
    "input": {"prompt": "A cat astronaut"}
})

print(result["status"])  # "completed"
print(result["output"])  # Output data
python
result = client.run({
    "app": "infsh/flux-schnell",
    "input": {"prompt": "A cat astronaut"}
})

print(result["status"])  # "completed"
print(result["output"])  # 输出数据

Fire and Forget

无需等待执行

python
task = client.run({
    "app": "google/veo-3-1-fast",
    "input": {"prompt": "Drone flying over mountains"}
}, wait=False)

print(f"Task ID: {task['id']}")
python
task = client.run({
    "app": "google/veo-3-1-fast",
    "input": {"prompt": "Drone flying over mountains"}
}, wait=False)

print(f"任务ID: {task['id']}")

Check later with client.get_task(task['id'])

后续可通过 client.get_task(task['id']) 检查状态

undefined
undefined

Streaming Progress

流式进度更新

python
for update in client.run({
    "app": "google/veo-3-1-fast",
    "input": {"prompt": "Ocean waves at sunset"}
}, stream=True):
    print(f"Status: {update['status']}")
    if update.get("logs"):
        print(update["logs"][-1])
python
for update in client.run({
    "app": "google/veo-3-1-fast",
    "input": {"prompt": "Ocean waves at sunset"}
}, stream=True):
    print(f"状态: {update['status']}")
    if update.get("logs"):
        print(update["logs"][-1])

Run Parameters

运行参数

ParameterTypeDescription
app
stringApp ID (namespace/name@version)
input
dictInput matching app schema
setup
dictHidden setup configuration
infra
string'cloud' or 'private'
session
stringSession ID for stateful execution
session_timeout
intIdle timeout (1-3600 seconds)
参数类型描述
app
string应用ID(命名空间/名称@版本)
input
dict匹配应用架构的输入数据
setup
dict隐藏的配置设置
infra
string'cloud'(云端)或 'private'(私有部署)
session
string用于有状态执行的会话ID
session_timeout
int空闲超时时间(1-3600秒)

File Handling

文件处理

Automatic Upload

自动上传

python
result = client.run({
    "app": "image-processor",
    "input": {
        "image": "/path/to/image.png"  # Auto-uploaded
    }
})
python
result = client.run({
    "app": "image-processor",
    "input": {
        "image": "/path/to/image.png"  # 自动上传
    }
})

Manual Upload

手动上传

python
from inferencesh import UploadFileOptions
python
from inferencesh import UploadFileOptions

Basic upload

基础上传

file = client.upload_file("/path/to/image.png")
file = client.upload_file("/path/to/image.png")

With options

带选项的上传

file = client.upload_file( "/path/to/image.png", UploadFileOptions( filename="custom_name.png", content_type="image/png", public=True ) )
result = client.run({ "app": "image-processor", "input": {"image": file["uri"]} })
undefined
file = client.upload_file( "/path/to/image.png", UploadFileOptions( filename="custom_name.png", content_type="image/png", public=True ) )
result = client.run({ "app": "image-processor", "input": {"image": file["uri"]} })
undefined

Sessions (Stateful Execution)

会话(有状态执行)

Keep workers warm across multiple calls:
python
undefined
在多次调用中保持工作进程活跃:
python
undefined

Start new session

启动新会话

result = client.run({ "app": "my-app", "input": {"action": "init"}, "session": "new", "session_timeout": 300 # 5 minutes }) session_id = result["session_id"]
result = client.run({ "app": "my-app", "input": {"action": "init"}, "session": "new", "session_timeout": 300 # 5分钟 }) session_id = result["session_id"]

Continue in same session

在同一会话中继续执行

result = client.run({ "app": "my-app", "input": {"action": "process"}, "session": session_id })
undefined
result = client.run({ "app": "my-app", "input": {"action": "process"}, "session": session_id })
undefined

Agent SDK

Agent SDK

Template Agents

模板化Agent

Use pre-built agents from your workspace:
python
agent = client.agent("my-team/support-agent@latest")
使用工作区中的预构建Agent:
python
agent = client.agent("my-team/support-agent@latest")

Send message

发送消息

response = agent.send_message("Hello!") print(response.text)
response = agent.send_message("Hello!") print(response.text)

Multi-turn conversation

多轮对话

response = agent.send_message("Tell me more")
response = agent.send_message("Tell me more")

Reset conversation

重置对话

agent.reset()
agent.reset()

Get chat history

获取聊天历史

chat = agent.get_chat()
undefined
chat = agent.get_chat()
undefined

Ad-hoc Agents

自定义Agent

Create custom agents programmatically:
python
from inferencesh import tool, string, number, app_tool
通过编程方式创建自定义Agent:
python
from inferencesh import tool, string, number, app_tool

Define tools

定义工具

calculator = ( tool("calculate") .describe("Perform a calculation") .param("expression", string("Math expression")) .build() )
image_gen = ( app_tool("generate_image", "infsh/flux-schnell@latest") .describe("Generate an image") .param("prompt", string("Image description")) .build() )
calculator = ( tool("calculate") .describe("执行计算操作") .param("expression", string("数学表达式")) .build() )
image_gen = ( app_tool("generate_image", "infsh/flux-schnell@latest") .describe("生成图片") .param("prompt", string("图片描述")) .build() )

Create agent

创建Agent

agent = client.agent({ "core_app": {"ref": "infsh/claude-sonnet-4@latest"}, "system_prompt": "You are a helpful assistant.", "tools": [calculator, image_gen], "temperature": 0.7, "max_tokens": 4096 })
response = agent.send_message("What is 25 * 4?")
undefined
agent = client.agent({ "core_app": {"ref": "infsh/claude-sonnet-4@latest"}, "system_prompt": "You are a helpful assistant.", "tools": [calculator, image_gen], "temperature": 0.7, "max_tokens": 4096 })
response = agent.send_message("What is 25 * 4?")
undefined

Available Core Apps

可用核心应用

ModelApp Reference
Claude Sonnet 4
infsh/claude-sonnet-4@latest
Claude 3.5 Haiku
infsh/claude-haiku-35@latest
GPT-4o
infsh/gpt-4o@latest
GPT-4o Mini
infsh/gpt-4o-mini@latest
模型应用引用
Claude Sonnet 4
infsh/claude-sonnet-4@latest
Claude 3.5 Haiku
infsh/claude-haiku-35@latest
GPT-4o
infsh/gpt-4o@latest
GPT-4o Mini
infsh/gpt-4o-mini@latest

Tool Builder API

工具构建器API

Parameter Types

参数类型

python
from inferencesh import (
    string, number, integer, boolean,
    enum_of, array, obj, optional
)

name = string("User's name")
age = integer("Age in years")
score = number("Score 0-1")
active = boolean("Is active")
priority = enum_of(["low", "medium", "high"], "Priority")
tags = array(string("Tag"), "List of tags")
address = obj({
    "street": string("Street"),
    "city": string("City"),
    "zip": optional(string("ZIP"))
}, "Address")
python
from inferencesh import (
    string, number, integer, boolean,
    enum_of, array, obj, optional
)

name = string("用户姓名")
age = integer("年龄(岁)")
score = number("分数 0-1")
active = boolean("是否活跃")
priority = enum_of(["low", "medium", "high"], "优先级")
tags = array(string("标签"), "标签列表")
address = obj({
    "street": string("街道"),
    "city": string("城市"),
    "zip": optional(string("邮政编码"))
}, "地址")

Client Tools (Run in Your Code)

客户端工具(在你的代码中运行)

python
greet = (
    tool("greet")
    .display("Greet User")
    .describe("Greets a user by name")
    .param("name", string("Name to greet"))
    .require_approval()
    .build()
)
python
greet = (
    tool("greet")
    .display("问候用户")
    .describe("按姓名问候用户")
    .param("name", string("要问候的姓名"))
    .require_approval()
    .build()
)

App Tools (Call AI Apps)

应用工具(调用AI应用)

python
generate = (
    app_tool("generate_image", "infsh/flux-schnell@latest")
    .describe("Generate an image from text")
    .param("prompt", string("Image description"))
    .setup({"model": "schnell"})
    .input({"steps": 20})
    .require_approval()
    .build()
)
python
generate = (
    app_tool("generate_image", "infsh/flux-schnell@latest")
    .describe("根据文本生成图片")
    .param("prompt", string("图片描述"))
    .setup({"model": "schnell"})
    .input({"steps": 20})
    .require_approval()
    .build()
)

Agent Tools (Delegate to Sub-agents)

Agent工具(委托给子Agent)

python
from inferencesh import agent_tool

researcher = (
    agent_tool("research", "my-org/researcher@v1")
    .describe("Research a topic")
    .param("topic", string("Topic to research"))
    .build()
)
python
from inferencesh import agent_tool

researcher = (
    agent_tool("research", "my-org/researcher@v1")
    .describe("研究指定主题")
    .param("topic", string("研究主题"))
    .build()
)

Webhook Tools (Call External APIs)

Webhook工具(调用外部API)

python
from inferencesh import webhook_tool

notify = (
    webhook_tool("slack", "https://hooks.slack.com/...")
    .describe("Send Slack notification")
    .secret("SLACK_SECRET")
    .param("channel", string("Channel"))
    .param("message", string("Message"))
    .build()
)
python
from inferencesh import webhook_tool

notify = (
    webhook_tool("slack", "https://hooks.slack.com/...")
    .describe("发送Slack通知")
    .secret("SLACK_SECRET")
    .param("channel", string("频道"))
    .param("message", string("消息内容"))
    .build()
)

Internal Tools (Built-in Capabilities)

内置工具(原生功能)

python
from inferencesh import internal_tools

config = (
    internal_tools()
    .plan()
    .memory()
    .web_search(True)
    .code_execution(True)
    .image_generation({
        "enabled": True,
        "app_ref": "infsh/flux@latest"
    })
    .build()
)

agent = client.agent({
    "core_app": {"ref": "infsh/claude-sonnet-4@latest"},
    "internal_tools": config
})
python
from inferencesh import internal_tools

config = (
    internal_tools()
    .plan()
    .memory()
    .web_search(True)
    .code_execution(True)
    .image_generation({
        "enabled": True,
        "app_ref": "infsh/flux@latest"
    })
    .build()
)

agent = client.agent({
    "core_app": {"ref": "infsh/claude-sonnet-4@latest"},
    "internal_tools": config
})

Streaming Agent Responses

流式Agent响应

python
def handle_message(msg):
    if msg.get("content"):
        print(msg["content"], end="", flush=True)

def handle_tool(call):
    print(f"\n[Tool: {call.name}]")
    result = execute_tool(call.name, call.args)
    agent.submit_tool_result(call.id, result)

response = agent.send_message(
    "Explain quantum computing",
    on_message=handle_message,
    on_tool_call=handle_tool
)
python
def handle_message(msg):
    if msg.get("content"):
        print(msg["content"], end="", flush=True)

def handle_tool(call):
    print(f"\n[工具: {call.name}]")
    result = execute_tool(call.name, call.args)
    agent.submit_tool_result(call.id, result)

response = agent.send_message(
    "Explain quantum computing",
    on_message=handle_message,
    on_tool_call=handle_tool
)

File Attachments

文件附件

python
undefined
python
undefined

From file path

从文件路径读取

with open("image.png", "rb") as f: response = agent.send_message( "What's in this image?", files=[f.read()] )
with open("image.png", "rb") as f: response = agent.send_message( "What's in this image?", files=[f.read()] )

From base64

从base64格式读取

response = agent.send_message( "Analyze this", files=["data:image/png;base64,iVBORw0KGgo..."] )
undefined
response = agent.send_message( "Analyze this", files=["data:image/png;base64,iVBORw0KGgo..."] )
undefined

Skills (Reusable Context)

Skills(可复用上下文)

python
agent = client.agent({
    "core_app": {"ref": "infsh/claude-sonnet-4@latest"},
    "skills": [
        {
            "name": "code-review",
            "description": "Code review guidelines",
            "content": "# Code Review\n\n1. Check security\n2. Check performance..."
        },
        {
            "name": "api-docs",
            "description": "API documentation",
            "url": "https://example.com/skills/api-docs.md"
        }
    ]
})
python
agent = client.agent({
    "core_app": {"ref": "infsh/claude-sonnet-4@latest"},
    "skills": [
        {
            "name": "code-review",
            "description": "代码评审指南",
            "content": "# 代码评审\n\n1. 检查安全性\n2. 检查性能..."
        },
        {
            "name": "api-docs",
            "description": "API文档",
            "url": "https://example.com/skills/api-docs.md"
        }
    ]
})

Async Support

异步支持

python
from inferencesh import async_inference
import asyncio

async def main():
    client = async_inference(api_key="inf_...")

    # Async app execution
    result = await client.run({
        "app": "infsh/flux-schnell",
        "input": {"prompt": "A galaxy"}
    })

    # Async agent
    agent = client.agent("my-org/assistant@latest")
    response = await agent.send_message("Hello!")

    # Async streaming
    async for msg in agent.stream_messages():
        print(msg)

asyncio.run(main())
python
from inferencesh import async_inference
import asyncio

async def main():
    client = async_inference(api_key="inf_...")

    # 异步执行应用
    result = await client.run({
        "app": "infsh/flux-schnell",
        "input": {"prompt": "A galaxy"}
    })

    # 异步Agent
    agent = client.agent("my-org/assistant@latest")
    response = await agent.send_message("Hello!")

    # 异步流式传输
    async for msg in agent.stream_messages():
        print(msg)

asyncio.run(main())

Error Handling

错误处理

python
from inferencesh import RequirementsNotMetException

try:
    result = client.run({"app": "my-app", "input": {...}})
except RequirementsNotMetException as e:
    print(f"Missing requirements:")
    for err in e.errors:
        print(f"  - {err['type']}: {err['key']}")
except RuntimeError as e:
    print(f"Error: {e}")
python
from inferencesh import RequirementsNotMetException

try:
    result = client.run({"app": "my-app", "input": {...}})
except RequirementsNotMetException as e:
    print(f"缺少依赖项:")
    for err in e.errors:
        print(f"  - {err['type']}: {err['key']}")
except RuntimeError as e:
    print(f"错误: {e}")

Human Approval Workflows

人工审批工作流

python
def handle_tool(call):
    if call.requires_approval:
        # Show to user, get confirmation
        approved = prompt_user(f"Allow {call.name}?")
        if approved:
            result = execute_tool(call.name, call.args)
            agent.submit_tool_result(call.id, result)
        else:
            agent.submit_tool_result(call.id, {"error": "Denied by user"})

response = agent.send_message(
    "Delete all temp files",
    on_tool_call=handle_tool
)
python
def handle_tool(call):
    if call.requires_approval:
        # 展示给用户,获取确认
        approved = prompt_user(f"是否允许执行{call.name}?")
        if approved:
            result = execute_tool(call.name, call.args)
            agent.submit_tool_result(call.id, result)
        else:
            agent.submit_tool_result(call.id, {"error": "用户已拒绝"})

response = agent.send_message(
    "Delete all temp files",
    on_tool_call=handle_tool
)

Reference Files

参考文档

  • Agent Patterns - Multi-agent, RAG, human-in-the-loop patterns
  • Tool Builder - Complete tool builder API reference
  • Streaming - Real-time progress updates and SSE handling
  • File Handling - Upload, download, and manage files
  • Sessions - Stateful execution with warm workers
  • Async Patterns - Parallel processing and async/await
  • Agent Patterns - 多Agent、RAG、人机协作模式
  • Tool Builder - 完整的工具构建器API参考
  • Streaming - 实时进度更新和SSE处理
  • File Handling - 上传、下载和管理文件
  • Sessions - 基于热工作进程的有状态执行
  • Async Patterns - 并行处理和async/await模式

Related Skills

相关Skills

bash
undefined
bash
undefined

JavaScript SDK

JavaScript SDK

npx skills add inference-sh/skills@javascript-sdk
npx skills add inference-sh/skills@javascript-sdk

Full platform skill (all 150+ apps via CLI)

完整平台Skill(通过CLI访问所有150+应用)

npx skills add inference-sh/skills@inference-sh
npx skills add inference-sh/skills@inference-sh

LLM models

LLM模型

npx skills add inference-sh/skills@llm-models
npx skills add inference-sh/skills@llm-models

Image generation

图片生成

npx skills add inference-sh/skills@ai-image-generation
undefined
npx skills add inference-sh/skills@ai-image-generation
undefined

Documentation

官方文档