Package: vast-ai-api
A low-level API for Vast.aiThis project is not intended to replace the open-sourcevast.ai librarybut rather to complement it for those who want to have an easy-to-use API to build their own projects. The goal of this project is not to re-implement every functionality in the original library but to simplify it for future developers and end users.It also integratespandasdataframes for easier data manipulation.Installation/Setup instructionsvast-ai-api is available on PyPi:$pipinstallvast-ai-apior$poetryaddvast-ai-apiExport yourVast-AI API key:$exportVAST_AI_API_KEY=<YOUR_API_KEY_HERE>After you reserve an instance, in order to change its state or interact with it in any way, you will need to setup anSSH keyon Vast.ai. Then, add your private key to your ssh agent (seehere).ContributingCurrently, the API covers most of the functionality provided by the original Vast AI library, except for host actions (hosting machines, listing hosted machines, etc.). You can contribute by forking this repo, adding your changes and making a pull request.UsageInitializing the API Helper:importpandasaspdfromvast_ai_apiimportVastAPIHelperapi=VastAPIHelper()List all instances available to be rentedinstances:pd.DataFrame=api.list_available_instances()Pick an instance from the list and reserve it using its instance_idinstance:pd.Series=instances.iloc[50]instance_id=instance["id"]machine_id=instance["machine_id"]# Needed after reservingapi.launch_instance(instance_id)Instance is now launched and starting up with default parameterslaunched_instances=api.list_current_instances()Note that theinstance_idthat we got before reserving the instance changes after reservation. Instead, we have to use themachine_idto find the instance again and get its new idnewly_launched_instance=launched_instances[launched_instances["machine_id"]==machine_id]new_instance_id=newly_launched_instance["id"]Now we can perform actions on this launched instance:api.stop_instance(new_instance_id)api.start_instance(new_instance_id)api.reboot_instance(new_instance_id)# Equivalent to stopping and starting the instanceapi.get_instance_logs(new_instance_id)Connecting through SSHPrerequisites: You must have initialized the instance asapi.launch_instance(instance_id, use_jupyter_lab=False)You can connect to the instance in 2 ways: with or without a proxy server (provided by Vast.ai). Using a proxy server is recommended as it allows you to stay anonymous when connecting to the gpu provider, but will slightly increase the latency to the machine.ssh_client=api.connect_ssh(new_instance_id,use_vast_proxy=True)stdin,stdout,stderr=ssh_client.exec_command("<your_command_here>")print(stdout.readlines())Alternatively, you can connect directly via the command line by reading the necessary host and port of the machine:ssh_host=newly_launched_instance["ssh_host"]ssh_port=newly_launched_instance["ssh_port"]and then usesshand replacessh_hostandssh_portwith the values above to connect your terminal to the instance:$ssh-p${ssh_port}root@${ssh_host}-L8080:localhost:8080Transferring files via sftpPrerequisites: You must have initialized the instance asapi.launch_instance(instance_id, use_jupyter_lab=False)"""src and dst format:localhost:22:<local_path> for the local machine<remote_host>:<remote_port>:<remote_path> for the remote machine"""api.copy("localhost:22:./polkadots.jpg","ssh.vastai5.com:/home/workdir/polkadots.jpg",ssh_client)api.copy("remote:/home/workdir/polkadots.jpg","localhost:~/images/polka_dots.jpg",connect_ssh(new_instance_id))api.copy("localhost:22:./polkadots.jpg","ssh.vastai5.com:29347:/home/workdir/polkadots.jpg")Alternatively, you can get yourssh_hostandssh_portas described above and usesftplocally:sftp-P${ssh_port}root@${ssh_host}After connecting, you can interactively move files or do other commands:put./helloWorld.py./helloWorld.py
getpasswdspasswds
chmod775./script.sh
...Deployment instructionsThis repo is deployed topypiusing poetry. Simply runpoetry build, thenpoetry publish