Skip to content

Commit f440c77

Browse files
zekeSBrandeis
andauthored
[Inference] [3rd party] Support versioned and unversioned Replicate models (#1106)
This PR updates the Replicate integration to support running both versioned and unversioned models via API. ### Versioned models The vast majority of models on Replicate are [versioned](https://replicate.com/docs/topics/models/versions). Each time a model author publishes changes to the model, a new version is created with a new 64-character SHA identifier. You have to specify a model's version by ID when running them with the API. Versioned models use the following HTTP API endpoint: ``` POST /v1/predictions ``` ☝🏼 This endpoint expects a `version` in the body of the request. See https://replicate.com/docs/reference/http#predictions.create ### Unversioned models Some models on Replicate like [black-forest-labs/flux-schnell](https://replicate.com/black-forest-labs/flux-schnell) are _unversioned_. Replicate staff maintains these as evergreen models, improving their performance and fixing bugs while maintaining API compatibility, so users know they're always running the latest and greatest. Unversioned models have their own HTTP API endpoint: ``` POST /v1/models/{owner}/{model}/predictions ``` See https://replicate.com/docs/reference/http#models.predictions.create --------- Co-authored-by: SBrandeis <[email protected]>
1 parent 529248a commit f440c77

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

packages/inference/src/lib/makeRequestOptions.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ export async function makeRequestOptions(
134134
case "fal-ai":
135135
return `${FAL_AI_API_BASE_URL}/${model}`;
136136
case "replicate":
137-
return `${REPLICATE_API_BASE_URL}/v1/models/${model}/predictions`;
137+
if (model.includes(":")) {
138+
// Versioned models are in the form of `owner/model:version`
139+
return `${REPLICATE_API_BASE_URL}/v1/predictions`;
140+
} else {
141+
// Unversioned models are in the form of `owner/model`
142+
return `${REPLICATE_API_BASE_URL}/v1/models/${model}/predictions`;
143+
}
138144
case "sambanova":
139145
return SAMBANOVA_API_BASE_URL;
140146
case "together":
@@ -168,6 +174,14 @@ export async function makeRequestOptions(
168174
credentials = "include";
169175
}
170176

177+
/*
178+
* Versioned Replicate models in the format `owner/model:version` expect the version in the body
179+
*/
180+
if (provider === "replicate" && model.includes(":")) {
181+
const version = model.split(":")[1];
182+
(otherArgs as typeof otherArgs & { version: string }).version = version;
183+
}
184+
171185
const info: RequestInit = {
172186
headers,
173187
method: "POST",

packages/inference/src/providers/replicate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ type ReplicateId = string;
1717
export const REPLICATE_MODEL_IDS: Record<ModelId, ReplicateId> = {
1818
/** text-to-image */
1919
"black-forest-labs/FLUX.1-schnell": "black-forest-labs/flux-schnell",
20-
"ByteDance/SDXL-Lightning": "bytedance/sdxl-lightning-4step",
20+
"ByteDance/SDXL-Lightning":
21+
"bytedance/sdxl-lightning-4step:5599ed30703defd1d160a25a63321b4dec97101d98b4674bcc56e41f62f35637",
2122
};

packages/inference/test/HfInference.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,14 +799,23 @@ describe.concurrent("HfInference", () => {
799799
() => {
800800
const client = new HfInference(env.HF_REPLICATE_KEY);
801801

802-
it("textToImage", async () => {
802+
it("textToImage canonical", async () => {
803803
const res = await client.textToImage({
804804
model: "black-forest-labs/FLUX.1-schnell",
805805
provider: "replicate",
806806
inputs: "black forest gateau cake spelling out the words FLUX SCHNELL, tasty, food photography, dynamic shot",
807807
});
808808
expect(res).toBeInstanceOf(Blob);
809809
});
810+
811+
it("textToImage versioned", async () => {
812+
const res = await client.textToImage({
813+
model: "ByteDance/SDXL-Lightning",
814+
provider: "replicate",
815+
inputs: "black forest gateau cake spelling out the words FLUX SCHNELL, tasty, food photography, dynamic shot",
816+
});
817+
expect(res).toBeInstanceOf(Blob);
818+
});
810819
},
811820
TIMEOUT
812821
);

packages/inference/test/tapes.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,5 +2525,58 @@
25252525
"content-type": "image/jpeg"
25262526
}
25272527
}
2528+
},
2529+
"84b1cb6a9d1039df7c10ffb2c90832009f4399964368f18f9b621da494d6c591": {
2530+
"url": "https://api.replicate.com/v1/predictions",
2531+
"init": {
2532+
"headers": {
2533+
"Content-Type": "application/json",
2534+
"Prefer": "wait"
2535+
},
2536+
"method": "POST",
2537+
"body": "{\"input\":{\"prompt\":\"black forest gateau cake spelling out the words FLUX SCHNELL, tasty, food photography, dynamic shot\"},\"version\":\"5599ed30703defd1d160a25a63321b4dec97101d98b4674bcc56e41f62f35637\"}"
2538+
},
2539+
"response": {
2540+
"body": "{\"id\":\"a0fyn914thrj00cmd6e8cd6e14\",\"model\":\"bytedance/sdxl-lightning-4step\",\"version\":\"5599ed30703defd1d160a25a63321b4dec97101d98b4674bcc56e41f62f35637\",\"input\":{\"prompt\":\"black forest gateau cake spelling out the words FLUX SCHNELL, tasty, food photography, dynamic shot\"},\"logs\":\"\",\"output\":[\"https://replicate.delivery/yhqm/f8ALqF3E4myiHqr66Gg765nEhsco4pvr32ABQvZX3fdFaUFUA/out-0.png\"],\"data_removed\":false,\"error\":null,\"status\":\"processing\",\"created_at\":\"2025-01-15T10:57:08.308Z\",\"urls\":{\"cancel\":\"https://api.replicate.com/v1/predictions/a0fyn914thrj00cmd6e8cd6e14/cancel\",\"get\":\"https://api.replicate.com/v1/predictions/a0fyn914thrj00cmd6e8cd6e14\",\"stream\":\"https://stream.replicate.com/v1/files/qoxq-pku5oelao43fx5g5ny4qbdm2qqyf5fqwp5yv26jpxcslw5ufwrtq\"}}",
2541+
"status": 201,
2542+
"statusText": "Created",
2543+
"headers": {
2544+
"alt-svc": "h3=\":443\"; ma=86400",
2545+
"cf-cache-status": "DYNAMIC",
2546+
"cf-ray": "902557da19120198-CDG",
2547+
"connection": "keep-alive",
2548+
"content-type": "application/json; charset=UTF-8",
2549+
"nel": "{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}",
2550+
"preference-applied": "wait=60",
2551+
"ratelimit-remaining": "599",
2552+
"ratelimit-reset": "1",
2553+
"report-to": "{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v4?s=dJWB5mgkALTqjarim6D5LybEWWkOtTzyTu4kxPRj%2FzVGVi%2BAmI0GsIe47bjsScWSZIIfuSe0XRTCtS3%2FW00TqVqiOT%2BmtKxvY903AZFVatuU2qx5IfRi1wE41l%2BlKyRMkjqs\"}],\"group\":\"cf-nel\",\"max_age\":604800}",
2554+
"server": "cloudflare",
2555+
"server-timing": "cfL4;desc=\"?proto=TCP&rtt=4793&min_rtt=3728&rtt_var=2159&sent=4&recv=5&lost=0&retrans=0&sent_bytes=2847&recv_bytes=1013&delivery_rate=776824&cwnd=251&unsent_bytes=0&cid=c3db321825f7d0a3&ts=1415&x=0\"",
2556+
"strict-transport-security": "max-age=15552000",
2557+
"vary": "Accept-Encoding"
2558+
}
2559+
}
2560+
},
2561+
"a3b664767cd6d4e44d76a7b3870730f789f02f3d05af00a26f4418c59c6b6cbc": {
2562+
"url": "https://replicate.delivery/yhqm/f8ALqF3E4myiHqr66Gg765nEhsco4pvr32ABQvZX3fdFaUFUA/out-0.png",
2563+
"init": {},
2564+
"response": {
2565+
"body": "",
2566+
"status": 200,
2567+
"statusText": "OK",
2568+
"headers": {
2569+
"accept-ranges": "bytes",
2570+
"access-control-allow-origin": "*",
2571+
"alt-svc": "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000",
2572+
"cache-control": "public,max-age=3600",
2573+
"cache-id": "PAR-31976c84",
2574+
"cache-status": "miss",
2575+
"content-type": "image/png",
2576+
"etag": "\"0e4e24383e70701c57477916dbc681de\"",
2577+
"last-modified": "Wed, 15 Jan 2025 10:57:09 GMT",
2578+
"server": "UploadServer"
2579+
}
2580+
}
25282581
}
25292582
}

packages/tasks/src/model-libraries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ export const MODEL_LIBRARIES_UI_ELEMENTS = {
772772
tabpfn: {
773773
prettyLabel: "TabPFN",
774774
repoName: "TabPFN",
775-
repoUrl: "https://github.com/PriorLabs/TabPFN"
775+
repoUrl: "https://github.com/PriorLabs/TabPFN",
776776
},
777777
"tic-clip": {
778778
prettyLabel: "TiC-CLIP",

0 commit comments

Comments
 (0)