# Python SDK 快速入门

{% hint style="success" %}
**您将构建的内容：** 在5分钟内，您将搜索GPU市场、租用服务器并通过SSH连接——所有操作均可通过代码或CLI完成。
{% endhint %}

## 先决条件

* **Python 3.9+** 已安装
* **Clore.ai 帐户** — [在此注册](https://clore.ai)
* **API 密钥** — 从您的 [Clore.ai 仪表板 获取](https://clore.ai)
* **资金** — 最低约 \~$5，支持 BTC、CLORE、USDT 或 USDC

***

## 第1步：安装 SDK

```bash
pip install clore-ai
```

验证安装：

```bash
clore --version
```

***

## 第2步：配置您的 API 密钥

**选项 A：环境变量（推荐）**

```bash
export CLORE_API_KEY=your_api_key_here
```

**选项 B：CLI 配置（持久化到 `~/.clore/config.json`)**

```bash
clore config set api_key YOUR_API_KEY
```

***

## 第3步：搜索 GPU

### CLI

```bash
clore search --gpu "RTX 4090" --max-price 5.0 --sort price --limit 10
```

您将看到包含服务器 ID、GPU 规格、内存、价格和地点的表格。

### Python

```python
from clore_ai import CloreAI

client = CloreAI()

servers = client.marketplace(gpu="RTX 4090", max_price_usd=5.0)
for s in servers[:5]:
    print(f"Server {s.id}: {s.gpu_count}x {s.gpu_model} — ${s.price_usd:.4f}/h — {s.location}")
```

**输出：**

```
Server 142: 1x NVIDIA GeForce RTX 4090 — $0.0800/h — EU
Server 305: 1x NVIDIA GeForce RTX 4090 — $0.0950/h — US
Server 891: 2x NVIDIA GeForce RTX 4090 — $0.1200/h — EU
```

{% hint style="info" %}
**提示：** 该 `marketplace()` 方法在客户端进行过滤。您还可以按以下项过滤 `min_gpu_count`, `min_ram_gb`，以及 `available_only` （默认： `True`).
{% endhint %}

***

## 第4步：部署（租用服务器）

### CLI

```bash
clore deploy 142 \
  --image cloreai/ubuntu22.04-cuda12 \
  --type on-demand \
  --currency bitcoin \
  --ssh-password MySecurePass123 \
  --port 22:tcp \
  --port 8888:http
```

### Python

```python
order = client.create_order(
    server_id=142,
    image="cloreai/ubuntu22.04-cuda12",
    type="on-demand",
    currency="bitcoin",
    ssh_password="MySecurePass123",
    ports={"22": "tcp", "8888": "http"}
)

print(f"Order created! ID: {order.id}")
print(f"IP: {order.pub_cluster}")
print(f"Ports: {order.tcp_ports}")
```

{% hint style="warning" %}
**首次启动需要 1–5 分钟。** 服务器会拉取 Docker 镜像并启动服务。如果 `pub_cluster` 是 `None`，请等待并使用 `my_orders()`.
{% endhint %}

***

## 再次检查。

### 第5步：通过 SSH 连接

```bash
CLI（自动连接）
```

clore ssh 38 `CLI 会查找订单，找到公网 IP 和 SSH 端口，然后为您运行` ssh

### 。

```bash
手动 SSH
ssh root@<pub_cluster> -p <ssh_port>
```

***

## # 密码：MySecurePass123

第6步：清理

### CLI

```bash
完成后取消订单以停止计费：
```

### Python

```python
clore cancel 38
client.cancel_order(order_id=38, issue="Done with my work")
```

***

## print("Order cancelled")

```python
from clore_ai import CloreAI
完整脚本：搜索 → 部署 → 监控 → 取消

import time

client = CloreAI()  # 使用 CLORE_API_KEY 环境变量
servers = client.marketplace(gpu="RTX 4090", max_price_usd=5.0)
# 1. 找到最便宜的 RTX 4090

servers.sort(key=lambda s: s.price_usd or float("inf"))
    if not servers:
    print("没有低于 $5/h 的 RTX 4090 可用")

exit(1)
best = servers[0]

print(f"最佳优惠：Server {best.id} — ${best.price_usd:.4f}/h — {best.location}")
order = client.create_order(
    # 2. 部署
    image="cloreai/ubuntu22.04-cuda12",
    type="on-demand",
    currency="bitcoin",
    ssh_password="MySecurePass123",
    server_id=best.id,
)
ports={"22": "tcp"}

print(f"Order {order.id} created!")
# 3. 等待分配 IP
    for _ in range(12):
    orders = client.my_orders()
    current = next((o for o in orders if o.id == order.id), None)
        if current and current.pub_cluster:
        print(f"已就绪！SSH: ssh root@{current.pub_cluster} -p {current.tcp_ports.get('22', 22)}")
    break
    print("等待服务器启动...")

time.sleep(10)

# 4. ... 执行您的工作 ...
# 5. 完成后取消
client.cancel_order(order_id=order.id)
```

***

## print("订单已取消，计费已停止。")

| 下一步是什么                                                                                        | 指南            |
| --------------------------------------------------------------------------------------------- | ------------- |
| [您将学习到的内容](https://docs.clore.ai/guides/guides_v2-zh/gao-ji/python-sdk)                       | Python SDK 指南 |
| [异步操作、现货市场、服务器管理、错误处理](https://docs.clore.ai/guides/guides_v2-zh/gao-ji/cli-automation)       | CLI 自动化       |
| [Bash 脚本、CI/CD 集成、批量部署](https://docs.clore.ai/guides/guides_v2-zh/gao-ji/api-integration)     | API 集成        |
| [将运行在 Clore 上的 AI 服务连接到您的应用](https://docs.clore.ai/guides/guides_v2-zh/ru-men/gpu-comparison) | GPU 对比        |

***

**为您的工作负载选择合适的 GPU，祝您租 GPU 愉快！ 🚀**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/ru-men/python-quickstart.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
