Skip to content

Commit 8dd3cdd

Browse files
committed
add method for listing public models
1 parent 502b22d commit 8dd3cdd

File tree

5 files changed

+204
-211
lines changed

5 files changed

+204
-211
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,39 @@ const response = await replicate.models.get(model_owner, model_name);
199199
}
200200
```
201201

202+
### `replicate.models.list`
203+
204+
```js
205+
const response = await replicate.models.list();
206+
```
207+
208+
```jsonc
209+
{
210+
"next": null,
211+
"previous": null,
212+
"results": [
213+
{
214+
"url": "https://replicate.com/replicate/hello-world",
215+
"owner": "replicate",
216+
"name": "hello-world",
217+
"description": "A tiny model that says hello",
218+
"visibility": "public",
219+
"github_url": "https://github.com/replicate/cog-examples",
220+
"paper_url": null,
221+
"license_url": null,
222+
"run_count": 5681081,
223+
"cover_image_url": "...",
224+
"default_example": {
225+
/* ... */
226+
},
227+
"latest_version": {
228+
/* ... */
229+
}
230+
}
231+
]
232+
}
233+
```
234+
202235
### `replicate.models.versions.list`
203236

204237
```js

index.d.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ declare module 'replicate' {
6969
}
7070

7171
export default class Replicate {
72-
constructor(options?: {
73-
auth?: string;
74-
userAgent?: string;
75-
baseUrl?: string;
76-
fetch?: Function;
77-
});
72+
constructor(options?: { auth?: string; userAgent?: string; baseUrl?: string; fetch?: Function });
7873

7974
auth: string;
8075
userAgent?: string;
@@ -93,14 +88,17 @@ declare module 'replicate' {
9388
progress?: (prediction: Prediction) => void
9489
): Promise<object>;
9590

96-
request(route: string | URL, options: {
97-
method?: string;
98-
headers?: object | Headers;
99-
params?: object;
100-
data?: object;
101-
}): Promise<Response>;
91+
request(
92+
route: string | URL,
93+
options: {
94+
method?: string;
95+
headers?: object | Headers;
96+
params?: object;
97+
data?: object;
98+
}
99+
): Promise<Response>;
102100

103-
paginate<T>(endpoint: () => Promise<Page<T>>): AsyncGenerator<[ T ]>;
101+
paginate<T>(endpoint: () => Promise<Page<T>>): AsyncGenerator<[T]>;
104102

105103
wait(
106104
prediction: Prediction,
@@ -117,24 +115,15 @@ declare module 'replicate' {
117115

118116
models: {
119117
get(model_owner: string, model_name: string): Promise<Model>;
118+
list(): Promise<Page<Model>>;
120119
versions: {
121120
list(model_owner: string, model_name: string): Promise<ModelVersion[]>;
122-
get(
123-
model_owner: string,
124-
model_name: string,
125-
version_id: string
126-
): Promise<ModelVersion>;
121+
get(model_owner: string, model_name: string, version_id: string): Promise<ModelVersion>;
127122
};
128123
};
129124

130125
predictions: {
131-
create(options: {
132-
version: string;
133-
input: object;
134-
stream?: boolean;
135-
webhook?: string;
136-
webhook_events_filter?: WebhookEventType[];
137-
}): Promise<Prediction>;
126+
create(options: { version: string; input: object; stream?: boolean; webhook?: string; webhook_events_filter?: WebhookEventType[] }): Promise<Prediction>;
138127
get(prediction_id: string): Promise<Prediction>;
139128
cancel(prediction_id: string): Promise<Prediction>;
140129
list(): Promise<Page<Prediction>>;

index.js

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ class Replicate {
3939
*/
4040
constructor(options = {}) {
4141
this.auth = options.auth || process.env.REPLICATE_API_TOKEN;
42-
this.userAgent =
43-
options.userAgent || `replicate-javascript/${packageJSON.version}`;
42+
this.userAgent = options.userAgent || `replicate-javascript/${packageJSON.version}`;
4443
this.baseUrl = options.baseUrl || 'https://api.replicate.com/v1';
4544
this.fetch = options.fetch || globalThis.fetch;
4645

@@ -51,6 +50,7 @@ class Replicate {
5150

5251
this.models = {
5352
get: models.get.bind(this),
53+
list: models.list.bind(this),
5454
versions: {
5555
list: models.versions.list.bind(this),
5656
get: models.versions.get.bind(this),
@@ -74,7 +74,7 @@ class Replicate {
7474
this.deployments = {
7575
predictions: {
7676
create: deployments.predictions.create.bind(this),
77-
}
77+
},
7878
};
7979
}
8080

@@ -103,15 +103,11 @@ class Replicate {
103103

104104
// Define a pattern for "owner/name:version" format with named capturing groups.
105105
// Example: "user123/repo_a:1a2b3c"
106-
const pattern = new RegExp(
107-
`^(?<owner>${namePattern.source})/(?<name>${namePattern.source}):(?<version>[0-9a-fA-F]+)$`
108-
);
106+
const pattern = new RegExp(`^(?<owner>${namePattern.source})/(?<name>${namePattern.source}):(?<version>[0-9a-fA-F]+)$`);
109107

110108
const match = identifier.match(pattern);
111109
if (!match || !match.groups) {
112-
throw new Error(
113-
'Invalid version. It must be in the format "owner/name:version"'
114-
);
110+
throw new Error('Invalid version. It must be in the format "owner/name:version"');
115111
}
116112

117113
const { version } = match.groups;
@@ -173,17 +169,10 @@ class Replicate {
173169
if (route instanceof URL) {
174170
url = route;
175171
} else {
176-
url = new URL(
177-
route.startsWith('/') ? route.slice(1) : route,
178-
baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`
179-
);
172+
url = new URL(route.startsWith('/') ? route.slice(1) : route, baseUrl.endsWith('/') ? baseUrl : `${baseUrl}/`);
180173
}
181174

182-
const {
183-
method = 'GET',
184-
params = {},
185-
data,
186-
} = options;
175+
const { method = 'GET', params = {}, data } = options;
187176

188177
Object.entries(params).forEach(([key, value]) => {
189178
url.searchParams.append(key, value);
@@ -207,19 +196,13 @@ class Replicate {
207196
body: data ? JSON.stringify(data) : undefined,
208197
};
209198

210-
const shouldRetry = method === 'GET' ?
211-
(response) => (response.status === 429 || response.status >= 500) :
212-
(response) => (response.status === 429);
199+
const shouldRetry = method === 'GET' ? (response) => response.status === 429 || response.status >= 500 : (response) => response.status === 429;
213200
const response = await withAutomaticRetries(async () => this.fetch(url, init), { shouldRetry });
214201

215202
if (!response.ok) {
216203
const request = new Request(url, init);
217204
const responseText = await response.text();
218-
throw new ApiError(
219-
`Request to ${url} failed with status ${response.status} ${response.statusText}: ${responseText}.`,
220-
request,
221-
response,
222-
);
205+
throw new ApiError(`Request to ${url} failed with status ${response.status} ${response.statusText}: ${responseText}.`, request, response);
223206
}
224207

225208
return response;
@@ -236,7 +219,7 @@ class Replicate {
236219
* @param {Function} endpoint - Function that returns a promise for the next page of results
237220
* @yields {object[]} Each page of results
238221
*/
239-
async * paginate(endpoint) {
222+
async *paginate(endpoint) {
240223
const response = await endpoint();
241224
yield response.results;
242225
if (response.next) {
@@ -267,11 +250,7 @@ class Replicate {
267250
throw new Error('Invalid prediction');
268251
}
269252

270-
if (
271-
prediction.status === 'succeeded' ||
272-
prediction.status === 'failed' ||
273-
prediction.status === 'canceled'
274-
) {
253+
if (prediction.status === 'succeeded' || prediction.status === 'failed' || prediction.status === 'canceled') {
275254
return prediction;
276255
}
277256

@@ -282,13 +261,9 @@ class Replicate {
282261

283262
let updatedPrediction = await this.predictions.get(id);
284263

285-
while (
286-
updatedPrediction.status !== 'succeeded' &&
287-
updatedPrediction.status !== 'failed' &&
288-
updatedPrediction.status !== 'canceled'
289-
) {
264+
while (updatedPrediction.status !== 'succeeded' && updatedPrediction.status !== 'failed' && updatedPrediction.status !== 'canceled') {
290265
/* eslint-disable no-await-in-loop */
291-
if (stop && await stop(updatedPrediction) === true) {
266+
if (stop && (await stop(updatedPrediction)) === true) {
292267
break;
293268
}
294269

0 commit comments

Comments
 (0)