You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* @param {string} identifier - Required. The model version identifier in the format "{owner}/{name}:{version}"
78
80
* @param {object} options
79
81
* @param {object} options.input - Required. An object with the model inputs
@@ -82,8 +84,9 @@ class Replicate {
82
84
* @param {number} [options.wait.maxAttempts] - Maximum number of polling attempts. Defaults to no limit
83
85
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
84
86
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
87
+
* @param {boolean} [options.stream] - Whether to stream the prediction output. Defaults to false
85
88
* @throws {Error} If the prediction failed
86
-
* @returns {Promise<object>} - Resolves with the output of running the model
89
+
* @returns {Promise<object>|Promise<AsyncIterable<any>>} - Resolves with the output of running the model, or a stream of output objects if `options.stream` is `true`
87
90
*/
88
91
asyncrun(identifier,options){
89
92
// Define a pattern for owner and model names that allows
@@ -105,6 +108,14 @@ class Replicate {
105
108
}
106
109
107
110
const{ version }=match.groups;
111
+
112
+
if(options.stream){
113
+
returnthis.predictions.create({
114
+
...options,
115
+
version,
116
+
});
117
+
}
118
+
108
119
constprediction=awaitthis.predictions.create({
109
120
wait: true,
110
121
...options,
@@ -125,6 +136,7 @@ class Replicate {
125
136
* @param {object} parameters - Request parameters
126
137
* @param {string} [parameters.method] - HTTP method. Defaults to GET
Copy file name to clipboardExpand all lines: lib/predictions.js
+14-2Lines changed: 14 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
/**
2
2
* Create a new prediction
3
3
*
4
+
* @typedef AsyncIterable
4
5
* @param {object} options
5
6
* @param {string} options.version - Required. The model version
6
7
* @param {object} options.input - Required. An object with the model inputs
@@ -9,10 +10,15 @@
9
10
* @param {number} [options.wait.maxAttempts] - Maximum number of polling attempts. Defaults to no limit
10
11
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
11
12
* @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
13
+
* @param {boolean} [options.stream] - Whether to stream the prediction output. Defaults to false
14
+
* @returns {Promise<object>|AsyncIterable<any>} Resolves with the created prediction, or a stream of prediction output if `options.stream` is `true`
13
15
*/
14
16
asyncfunctioncreatePrediction(options){
15
-
const{ wait, ...data}=options;
17
+
const{ wait, stream, ...data}=options;
18
+
19
+
if(stream&&wait){
20
+
thrownewError('Incompatible options: stream and wait');
21
+
}
16
22
17
23
if(data.webhook){
18
24
try{
@@ -23,9 +29,15 @@ async function createPrediction(options) {
0 commit comments