sema-replicate

0.1.0

Run Replicate models: create predictions, poll to completion, and fetch outputs

$ sema pkg install sema-replicate
Readme Versions 1 Dependencies 0

sema-replicate

Run Replicate models: create predictions, poll to completion, and fetch outputs.

Install

sema pkg add sema-replicate

Quick start

(import "sema-replicate")

(define c (replicate/client))                 ; reads REPLICATE_API_TOKEN

;; Create + poll to completion; returns the model output (usually URL[s]).
(define output
  (replicate/run c
    "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa" ; version id
    {:prompt "a red panda coding in a hammock"}))

(println output)
;; => ("https://replicate.delivery/pbxt/.../out-0.png")

;; The output is a URL — download the bytes and save them.
(file/write-bytes "panda.png"
  (get (http/get (first output) {:as :bytes}) :body))

API

Function Description
replicate/client Build a client map; API token falls back to REPLICATE_API_TOKEN.
replicate/request Low-level request escape hatch used by every wrapper.
replicate/create-prediction Start a prediction for a model version and input map.
replicate/get-prediction Fetch a prediction by id (poll its status).
replicate/run Create a prediction and poll to completion; return the output.
replicate/cancel! Cancel a running prediction by id.
replicate/done? Predicate: has a prediction reached a terminal status?
replicate/output Extract the output (URL or list of URLs) from a prediction map.

replicate/client

(replicate/client)
(replicate/client {:api-key "r8_..." :base-url "..." :timeout 60000})

Build a client. The API token falls back to the REPLICATE_API_TOKEN environment variable; if neither is present it errors naming the variable.

(replicate/client {:api-key "r8_abc123"})
;; => {:token "r8_abc123" :base-url "https://api.replicate.com/v1" :timeout 60000}

replicate/create-prediction

(replicate/create-prediction client version input)
(replicate/create-prediction client version input {:webhook "..." :webhook-events-filter (list "completed")})

Start a prediction. Returns the created prediction map immediately — it runs asynchronously with status starting.

(define p (replicate/create-prediction c version {:prompt "hello"}))
(get p :status) ; => "starting"
(get p :id)     ; => "abc123..."

replicate/get-prediction

(replicate/get-prediction client id)

Fetch a prediction's current state. Poll this until (replicate/done? p).

(get (replicate/get-prediction c "abc123") :status) ; => "processing"

replicate/run

(replicate/run client version input)
(replicate/run client version input {:interval 1000 :max-polls 300})

Create a prediction and poll (default: every 1000 ms, up to 300 polls) until it finishes, then return the output. Errors if the prediction fails, is canceled, or times out.

(replicate/run c version {:prompt "a cat"})
;; => ("https://replicate.delivery/.../out.png")

replicate/cancel!

(replicate/cancel! client id)

Cancel a running prediction; returns the updated prediction map.

(get (replicate/cancel! c "abc123") :status) ; => "canceled"

replicate/done?

(replicate/done? prediction)

Return #t when a prediction map's :status is succeeded, failed, or canceled.

replicate/output

(replicate/output prediction)

Extract the :output of a prediction map — usually a URL or a list of URLs, or nil before it succeeds. Download URLs with (http/get url {:as :bytes}).

Authentication

Create an API token at https://replicate.com/account/api-tokens and export it:

export REPLICATE_API_TOKEN="r8_..."

Or pass it explicitly: (replicate/client {:api-key "r8_..."}). The token is sent as Authorization: Bearer <token> and is never logged.

Testing

sema tests.sema

Offline tests cover client construction, argument validation, error mapping, and the pure status/output helpers. The live test hits api.replicate.com and only runs when REPLICATE_API_TOKEN is set.

License

MIT

VersionSizePublished
0.1.0 5 KB 2026-07-07 21:28:13
No dependencies