Skip to content

Commit 972623c

Browse files
committed
Add stream parameter to predictions.create
1 parent 9ccdf48 commit 972623c

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

README.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,14 @@ const response = await replicate.predictions.create(options);
251251
| ------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------- |
252252
| `options.version` | string | **Required**. The model version |
253253
| `options.input` | object | **Required**. An object with the model's inputs |
254+
| `options.stream` | boolean | Requests a URL for streaming output output |
254255
| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output |
255256
| `options.webhook_events_filter` | string[] | You can change which events trigger webhook requests by specifying webhook events (`start` \| `output` \| `logs` \| `completed`) |
256257

257258
```jsonc
258259
{
259260
"id": "ufawqhfynnddngldkgtslldrkq",
260261
"version": "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
261-
"urls": {
262-
"get": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq",
263-
"cancel": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel"
264-
},
265262
"status": "succeeded",
266263
"input": {
267264
"text": "Alice"
@@ -272,10 +269,56 @@ const response = await replicate.predictions.create(options);
272269
"metrics": {},
273270
"created_at": "2022-04-26T22:13:06.224088Z",
274271
"started_at": null,
275-
"completed_at": null
272+
"completed_at": null,
273+
"urls": {
274+
"get": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq",
275+
"cancel": "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel",
276+
"stream": "https://streaming.api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq" // Present only if `options.stream` is `true`
277+
}
276278
}
277279
```
278280

281+
#### Streaming
282+
283+
Specify the `stream` option when creating a prediction
284+
to request a URL to receive streaming output using
285+
[server-sent events (SSE)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events).
286+
287+
If the requested model version supports streaming,
288+
then the returned prediction will have a `stream` entry in its `urls` property
289+
with a URL that you can use to construct an
290+
[`EventSource`](https://developer.mozilla.org/en-US/docs/Web/API/EventSource).
291+
292+
```js
293+
if (prediction && prediction.urls && prediction.urls.stream) {
294+
const source = new EventSource(prediction.urls.stream);
295+
296+
source.addEventListener("output", (e) => {
297+
console.log("output", e.data);
298+
});
299+
300+
source.addEventListener("error"), (e) => {
301+
console.error("error", JSON.parse(e.data));
302+
});
303+
304+
source.addEventListener("done"), (e) => {
305+
source.close();
306+
console.log("done", JSON.parse(e.data));
307+
});
308+
}
309+
```
310+
311+
A prediction's event stream consists of the following event types:
312+
313+
| event | format | description |
314+
| -------- | ---------- | ---------------------------------------------- |
315+
| `output` | plain text | Emitted when the prediction returns new output |
316+
| `error` | JSON | Emitted when the prediction returns an error |
317+
| `done` | JSON | Emitted when the prediction finishes |
318+
319+
A `done` event is emitted when a prediction finishes successfully,
320+
is cancelled, or produces an error.
321+
279322
### `replicate.predictions.get`
280323

281324
```js

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ declare module 'replicate' {
5353
created_at: string;
5454
updated_at: string;
5555
completed_at?: string;
56+
urls: {
57+
get: string;
58+
cancel: string;
59+
stream?: string;
60+
};
5661
}
5762

5863
export type Training = Prediction;
@@ -123,6 +128,7 @@ declare module 'replicate' {
123128
create(options: {
124129
version: string;
125130
input: object;
131+
stream?: boolean;
126132
webhook?: string;
127133
webhook_events_filter?: WebhookEventType[];
128134
}): Promise<Prediction>;

index.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,24 @@ describe('Replicate client', () => {
152152
expect(prediction.id).toBe('ufawqhfynnddngldkgtslldrkq');
153153
});
154154

155+
test('Passes stream parameter to API endpoint', async () => {
156+
nock(BASE_URL)
157+
.post('/predictions')
158+
.reply(201, (_uri, body) => {
159+
expect(body[ 'stream' ]).toBe(true);
160+
return body
161+
})
162+
163+
await client.predictions.create({
164+
version:
165+
'5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa',
166+
input: {
167+
prompt: 'Tell me a story',
168+
},
169+
stream: true
170+
});
171+
});
172+
155173
test('Throws an error if webhook URL is invalid', async () => {
156174
await expect(async () => {
157175
await client.predictions.create({
@@ -506,7 +524,7 @@ describe('Replicate client', () => {
506524
status: 'processing',
507525
})
508526
.get('/predictions/ufawqhfynnddngldkgtslldrkq')
509-
.reply(200, {
527+
.reply(201, {
510528
id: 'ufawqhfynnddngldkgtslldrkq',
511529
status: 'succeeded',
512530
output: 'foobar',

lib/predictions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
* @param {number} [options.wait.maxAttempts] - Maximum number of polling attempts. Defaults to no limit
1010
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
1111
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
12-
* @returns {Promise<object>} Resolves with the created prediction data
12+
* @param {boolean} [options.stream] - Whether to stream the prediction output. Defaults to false
13+
* @returns {Promise<object>} Resolves with the created prediction
1314
*/
1415
async function createPrediction(options) {
15-
const { wait, ...data } = options;
16+
const { stream, ...data } = options;
1617

1718
if (data.webhook) {
1819
try {

0 commit comments

Comments
 (0)