Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add block parameter to prediction creation methods
  • Loading branch information
mattt committed Sep 25, 2024
commit 0a57882ee057c0913010d84f9e1d442cb1b4c22f
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,11 @@ declare module "replicate" {
deployment_name: string,
options: {
input: object;
/** @deprecated */
stream?: boolean;
webhook?: string;
webhook_events_filter?: WebhookEventType[];
block?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I preferred the version we had that exposed wait here. Where wait was one of:

wait?: { mode: "block", timeout?: number } | { mode: "poll", interval?: number }

This way we have only one param consistently, and it defaults to {mode: "block"}.

}
): Promise<Prediction>;
};
Expand Down Expand Up @@ -301,6 +303,7 @@ declare module "replicate" {
stream?: boolean;
webhook?: string;
webhook_events_filter?: WebhookEventType[];
block?: boolean;
} & ({ version: string } | { model: string })
): Promise<Prediction>;
get(prediction_id: string): Promise<Prediction>;
Expand Down
10 changes: 8 additions & 2 deletions lib/deployments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const { transformFileInputs } = require("./util");
* @param {string} deployment_name - Required. The name of the deployment
* @param {object} options
* @param {object} options.input - Required. An object with the model inputs
* @param {boolean} [options.stream] - Whether to stream the prediction output. Defaults to false
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
* @param {boolean} [options.block] - Whether to wait until the prediction is completed before returning. Defaults to false
* @returns {Promise<object>} Resolves with the created prediction data
*/
async function createPrediction(deployment_owner, deployment_name, options) {
const { stream, input, ...data } = options;
const { input, block, ...data } = options;

if (data.webhook) {
try {
Expand All @@ -24,10 +24,16 @@ async function createPrediction(deployment_owner, deployment_name, options) {
}
}

const headers = {};
if (block) {
headers["X-Sync"] = "true";
}

const response = await this.request(
`/deployments/${deployment_owner}/${deployment_name}/predictions`,
{
method: "POST",
headers,
data: {
...data,
input: await transformFileInputs(
Expand Down
11 changes: 9 additions & 2 deletions lib/predictions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const { transformFileInputs } = require("./util");
* @param {object} options.input - Required. An object with the model inputs
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
* @param {boolean} [options.stream] - Whether to stream the prediction output. Defaults to false. Streaming is now enabled by default for all predictions. For more information, see https://replicate.com/changelog/2024-07-15-streams-always-available-stream-parameter-deprecated
* @param {boolean} [options.block] - Whether to wait until the prediction is completed before returning. Defaults to false
* @returns {Promise<object>} Resolves with the created prediction
*/
async function createPrediction(options) {
const { model, version, input, ...data } = options;
const { model, version, input, block, ...data } = options;

if (data.webhook) {
try {
Expand All @@ -24,10 +24,16 @@ async function createPrediction(options) {
}
}

const headers = {};
if (block) {
headers["X-Sync"] = "true";
}

let response;
if (version) {
response = await this.request("/predictions", {
method: "POST",
headers,
data: {
...data,
input: await transformFileInputs(
Expand All @@ -41,6 +47,7 @@ async function createPrediction(options) {
} else if (model) {
response = await this.request(`/models/${model}/predictions`, {
method: "POST",
headers,
data: {
...data,
input: await transformFileInputs(
Expand Down