mcpField
V
vastai-sdk

vastai-sdk

github.com/vast-ai云与 DevOps225342 次安装6 天前更新
代码仓库

关于 vastai-sdk

Vast.ai Python SDK 提供同步和异步 API,用于管理 GPU 实例、卷、无服务器端点、账单及相关账户资源。它面向希望通过 Python 自动化 Vast.ai 操作的开发者,提供高级接口和底层客户端。

主要功能

  1. 01管理 GPU 实例和报价
  2. 02搜索和管理卷
  3. 03在不同存储位置间传输数据
  4. 04管理无服务器部署
  5. 05使用同步和异步客户端

适用场景

  1. 01查找 GPU 报价并启动实例
  2. 02在本地设备与实例之间传输数据
  3. 03管理无服务器推理部署

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/vast-ai/vast-cli --skill vastai-sdk

安装后在 Claude Code 中输入/vastai-sdk确认已加载。

附带文件__init__.py

详细介绍

Vast.ai Python SDK(vastai / vastai_sdk)

vastai 软件包提供 Python SDK,用于管理 Vast.ai 上的 GPU 实例、卷、无服务器端点和账单。vastai_sdk 软件包是向后兼容的垫片,会重新导出 vastai。

安装

pip install vastai

如需无服务器和异步支持:

pip install "vastai[serverless]"

身份验证

默认情况下,SDK 会从 ~/.vast_api_key 读取 API 密钥。你也可以显式传入:

from vastai import VastAI
vast = VastAI()                        # reads ~/.vast_api_key
vast = VastAI(api_key="YOUR_API_KEY")  # explicit key

从 https://console.vast.ai/manage-keys/ 获取 API 密钥。

向后兼容

旧的 vastai_sdk 导入方式仍然有效:

from vastai_sdk import VastAI  # equivalent to: from vastai import VastAI

VastAI 类(高级 SDK)

from vastai import VastAI
vast = VastAI(api_key=None, server_url=None, retry=3, raw=False, quiet=False)
实例管理
# List all your instances
instances = vast.show_instances()

# Get a single instance
instance = vast.show_instance(id=12345)

# Search GPU offers
offers = vast.search_offers(query='gpu_name=RTX_4090 num_gpus>=4 reliability>0.99')

# Create an instance from an offer
result = vast.create_instance(id=<offer_id>, image="pytorch/pytorch:latest", disk=50)

# ...as a jupyter instance on a direct connection
result = vast.create_instance(id=<offer_id>, image="pytorch/pytorch:latest", disk=50,
                              jupyter=True, direct=True, jupyter_lab=True)

# Lifecycle
vast.start_instance(id=12345)
vast.stop_instance(id=12345)
vast.reboot_instance(id=12345)
vast.destroy_instance(id=12345)

# Label an instance
vast.label_instance(id=12345, label="my-training-run")

# Get SSH connection string
ssh_url = vast.ssh_url(id=12345)   # returns "ssh -p PORT user@host"
scp_url = vast.scp_url(id=12345)   # returns scp-compatible URL
可中断(竞价)租用

可中断(竞价)实例的价格低于按需实例,但其他用户只要出价更低,就可以随时中断实例。注意:vast.search_offers(type='bid', ...) 会公开 min_bid,但 vast.create_instance(...) 默认会按 dph_total 价格创建按需实例,除非你传入 bid_price=<floor>。在执行 type='bid' 搜索后,务必传入 bid_price,否则实例将按按需实例/价格租用,而不是作为可中断实例租用。

被其他用户超过出价后,实例会变为 stopped(不会销毁),且存储费用仍会继续产生。可以通过提高出价来恢复实例:vast.change_bid(id=..., price=...)。

搜索
# Search GPU offers (use help(vast.search_offers) for full query syntax)
offers = vast.search_offers(query='gpu_name=RTX_3090 num_gpus>=2')

# Search volume offers
volumes = vast.search_volumes(query='...')

# Search network volumes
net_vols = vast.search_network_volumes()

# Search templates
templates = vast.search_templates()

# Search invoices
invoices = vast.search_invoices()
数据传输
# copy() takes vast URLs: "[C.|V.]id:path", "cloud_service[.id]:path", or "local:path"
vast.copy("local:./data/", "C.12345:/workspace/data/")   # Local → instance
vast.copy("C.12345:/workspace/results/", "local:./out/") # Instance → local
vast.copy("12345:/workspace/", "67890:/workspace/")      # Instance → instance (legacy format)
vast.copy("s3.101:/data/", "C.12345:/workspace/")        # Cloud service → instance
vast.copy("V.1234:/file", "C.5678:/workspace/")          # Volume → instance
vast.copy("V.1234:/file", "s3.101:/workspace/")          # Volume → cloud service

vast.cancel_copy(dst_id=12345)                           # Cancel an in-progress copy

# Cloud sync via a saved cloud connection (see the UI settings page for connection IDs)
vast.cloud_copy(src="./data", dst="s3://bucket/path", instance=12345,
                connection=<conn_id>, transfer="Instance To Cloud")
vast.cancel_sync(dst_id=12345)

目前,卷复制仅支持复制到其他卷、实例或云服务,不支持复制到本地。请勿将 /root 或 / 用作目标目录,否则会破坏实例上的 ssh 权限,并导致后续复制失败。请参阅 https://vast.ai/docs/gpu-instances/data-movement#constraints。

无服务器部署
# List all deployments
deployments = vast.show_deployments()

# Get a deployment
deployment = vast.show_deployment(id=42)

# Delete a deployment
vast.delete_deployment(id=42)
机器管理(托管)
machines = vast.show_machines()
machine = vast.show_machine(id=10)
vast.list_machine(id=10, price_gpu=0.30)
vast.unlist_machine(id=10)
SSH 密钥
keys = vast.show_ssh_keys()
vast.create_ssh_key(ssh_key="ssh-rsa AAAA...")
vast.delete_ssh_key(id=5)
团队管理
members = vast.show_members()
vast.invite_member(email="[email protected]", role="developer")
vast.remove_member(id=7)

SyncClient(底层同步客户端)

SyncClient 提供对 GPU 报价和实例的类型化同步访问。

from vastai import SyncClient

client = SyncClient(api_key="YOUR_API_KEY")  # or reads ~/.vast_api_key

# Search offers with structured filters
offers = client.search(
    num_gpus=2,
    gpu_name="RTX_4090",
    min_reliability=0.99,
    max_dph_total=2.0,
)

# Create an instance (SyncClient takes an InstanceConfig, not loose kwargs)
from vastai.data.instance import InstanceConfig

instance = client.create_instance(
    offer_id=<id>,
    config=InstanceConfig(image="pytorch/pytorch:latest", disk=50),
)

# List your instances
instances = client.show_instances()  # returns list[SyncInstance]

# Destroy an instance
client.destroy_instance(instance_or_id=12345)

AsyncClient(底层异步客户端)

AsyncClient 提供对 GPU 报价和实例的异步访问。请将其用作异步上下文管理器。

import asyncio
from vastai import AsyncClient
from vastai.data.instance import InstanceConfig

async def main():
    async with AsyncClient(api_key="YOUR_API_KEY") as client:
        # Search offers
        offers = await client.search(num_gpus=1, gpu_name="A100")

        # Create instance
        instance = await client.create_instance(
            offer_id=<id>, config=InstanceConfig(image="ubuntu:22.04"))

        # List instances
        instances = await client.show_instances()  # returns list[AsyncInstance]

        # Destroy instance
        await client.destroy_instance(instance_or_id=instance.id)

asyncio.run(main())

无服务器客户端

用于推理端点(需要 pip install "vastai[serverless]"):

import asyncio
from vastai import Serverless

async def main():
    serverless = Serverless()  # reads ~/.vast_api_key

    # Get an endpoint
    endpoint = await serverless.get_endpoint("my-endpoint")

    # Make a request
    response = await serverless.request("/v1/completions", {
        "model": "Qwen/Qwen3-8B",
        "prompt": "Who are you?",
        "max_tokens": 100,
        "temperature": 0.7,
    })

    text = response["response"]["choices"][0]["text"]
    print(text)

asyncio.run(main())

常见模式

# Find cheapest 4x RTX 4090 and launch a job
from vastai import VastAI
vast = VastAI()

offers = vast.search_offers(query='gpu_name=RTX_4090 num_gpus=4 reliability>0.99')
cheapest = min(offers, key=lambda o: o['dph_total'])
result = vast.create_instance(id=cheapest['id'], image="pytorch/pytorch:latest", disk=100)
print(f"Launched instance: {result['new_contract']}")

# Use help() to explore method signatures
help(vast.search_offers)
help(vast.create_instance)

评论

支持 Markdown