|
1 | 1 | # wavespeed-python |
2 | 2 |
|
3 | | -WaveSpeedAI Python Client — Official Python SDK for WaveSpeedAI inference platform. This library provides a clean, unified, and high-performance API and serverless integration layer for your applications. Effortlessly connect to all WaveSpeedAI models and inference services with zero infrastructure overhead. |
| 3 | +WaveSpeedAI Python Client — Official Python SDK for WaveSpeedAI inference platform. This library provides a clean, unified, and high-performance API and serverless integration layer for your applications. |
4 | 4 |
|
5 | 5 | ## Installation |
6 | 6 |
|
7 | 7 | ```bash |
8 | 8 | pip install wavespeed |
9 | 9 | ``` |
10 | 10 |
|
11 | | -## Usage |
| 11 | +## Serverless Worker |
| 12 | + |
| 13 | +Build serverless workers compatible with RunPod infrastructure. |
| 14 | + |
| 15 | +### Basic Handler |
| 16 | + |
| 17 | +```python |
| 18 | +import wavespeed.serverless as serverless |
| 19 | + |
| 20 | +def handler(job): |
| 21 | + job_input = job["input"] |
| 22 | + result = job_input.get("prompt", "").upper() |
| 23 | + return {"output": result} |
| 24 | + |
| 25 | +serverless.start({"handler": handler}) |
| 26 | +``` |
| 27 | + |
| 28 | +### Async Handler |
| 29 | + |
| 30 | +```python |
| 31 | +import wavespeed.serverless as serverless |
| 32 | + |
| 33 | +async def handler(job): |
| 34 | + job_input = job["input"] |
| 35 | + result = await process_async(job_input) |
| 36 | + return {"output": result} |
| 37 | + |
| 38 | +serverless.start({"handler": handler}) |
| 39 | +``` |
| 40 | + |
| 41 | +### Generator Handler (Streaming) |
12 | 42 |
|
13 | 43 | ```python |
14 | | -import wavespeed |
| 44 | +import wavespeed.serverless as serverless |
| 45 | + |
| 46 | +def handler(job): |
| 47 | + for i in range(10): |
| 48 | + yield {"progress": i, "partial": f"chunk-{i}"} |
| 49 | + |
| 50 | +serverless.start({"handler": handler}) |
| 51 | +``` |
| 52 | + |
| 53 | +### Input Validation |
| 54 | + |
| 55 | +```python |
| 56 | +from wavespeed.serverless.utils import validate |
| 57 | + |
| 58 | +INPUT_SCHEMA = { |
| 59 | + "prompt": {"type": str, "required": True}, |
| 60 | + "max_tokens": {"type": int, "required": False, "default": 100}, |
| 61 | + "temperature": { |
| 62 | + "type": float, |
| 63 | + "required": False, |
| 64 | + "default": 0.7, |
| 65 | + "constraints": lambda x: 0 <= x <= 2, |
| 66 | + }, |
| 67 | +} |
| 68 | + |
| 69 | +def handler(job): |
| 70 | + result = validate(job["input"], INPUT_SCHEMA) |
| 71 | + if "errors" in result: |
| 72 | + return {"error": result["errors"]} |
15 | 73 |
|
16 | | -print(wavespeed.__version__) |
| 74 | + validated = result["validated_input"] |
| 75 | + # process with validated input... |
| 76 | + return {"output": "done"} |
17 | 77 | ``` |
| 78 | + |
| 79 | +## Local Development |
| 80 | + |
| 81 | +### Test with JSON Input |
| 82 | + |
| 83 | +```bash |
| 84 | +# Using CLI argument |
| 85 | +python handler.py --test_input '{"input": {"prompt": "hello"}}' |
| 86 | + |
| 87 | +# Using test_input.json file (auto-detected) |
| 88 | +echo '{"input": {"prompt": "hello"}}' > test_input.json |
| 89 | +python handler.py |
| 90 | +``` |
| 91 | + |
| 92 | +### FastAPI Development Server |
| 93 | + |
| 94 | +```bash |
| 95 | +python handler.py --waverless_serve_api --waverless_api_port 8000 |
| 96 | +``` |
| 97 | + |
| 98 | +Then use the interactive Swagger UI at `http://localhost:8000/` or make requests: |
| 99 | + |
| 100 | +```bash |
| 101 | +# Synchronous execution |
| 102 | +curl -X POST http://localhost:8000/runsync \ |
| 103 | + -H "Content-Type: application/json" \ |
| 104 | + -d '{"input": {"prompt": "hello"}}' |
| 105 | + |
| 106 | +# Async execution |
| 107 | +curl -X POST http://localhost:8000/run \ |
| 108 | + -H "Content-Type: application/json" \ |
| 109 | + -d '{"input": {"prompt": "hello"}}' |
| 110 | +``` |
| 111 | + |
| 112 | +## CLI Options |
| 113 | + |
| 114 | +| Option | Description | |
| 115 | +|--------|-------------| |
| 116 | +| `--test_input JSON` | Run locally with JSON test input | |
| 117 | +| `--waverless_serve_api` | Start FastAPI development server | |
| 118 | +| `--waverless_api_host HOST` | API server host (default: localhost) | |
| 119 | +| `--waverless_api_port PORT` | API server port (default: 8000) | |
| 120 | +| `--waverless_log_level LEVEL` | Log level (DEBUG, INFO, WARN, ERROR) | |
| 121 | + |
| 122 | +## Environment Variables |
| 123 | + |
| 124 | +The SDK auto-detects RunPod or native Waverless environments: |
| 125 | + |
| 126 | +| Variable | Description | |
| 127 | +|----------|-------------| |
| 128 | +| `RUNPOD_WEBHOOK_GET_JOB` | Job fetch endpoint | |
| 129 | +| `RUNPOD_WEBHOOK_POST_OUTPUT` | Result submission endpoint | |
| 130 | +| `RUNPOD_AI_API_KEY` | API authentication key | |
| 131 | +| `RUNPOD_POD_ID` | Worker/pod identifier | |
| 132 | + |
| 133 | +## License |
| 134 | + |
| 135 | +MIT |
0 commit comments