Submit and poll a completion job
You submit a completion once and fetch the result later. If the connection drops, you run the script again with the same idempotency key and get the same job back.
Run it:
curl -O https://infra.acsresearch.org/examples/python_completion_jobs.py && ACS_API_KEY="acs-bm-..." python python_completion_jobs.py --model llama-8b --idempotency-key my-experiment-item-001
Prerequisites
- You need an API key. Account explains where to create one.
- You need Python 3.11 or later and
httpx, which you install withpip install httpx.
Run the script
- Download
python_completion_jobs.py. - Set
ACS_API_KEY, and setACS_API_BASEonly to call another deployment. Then run the script:
python python_completion_jobs.py \
--model llama-8b --prompt 'Hello' --max-tokens 128 \
--idempotency-key my-experiment-item-001 --deadline 900
When the job ends, the script prints the finished completion as one JSON line.
Script walkthrough
- It builds the request body from
--model,--prompt, and--max-tokens. To change the completion, add fields such asn,seed, orechoto that body. - It sends the body to
POST /v1/completion-jobswith--idempotency-keyin theIdempotency-Keyheader. - It polls the job's status URL once a second until the job is no longer
queuedorrunning, then fetches the result URL. - It stops polling after
--deadlineseconds, but the job keeps running on the server. - It checks the HTTP status of the result before it prints the completion, because a finished job can end with an error status such as
429.
Keep the same idempotency key, API key, and request parameters when you rerun the script for the same experiment item.
Next steps
- Submit a completion job has the HTTP contract, the limits, and the retention rules.
- Stream through a cold boot shows a streamed synchronous request, which jobs reject.
- Recover from a budget cap explains the
429status that a job can end with.