Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,41 @@ const response = await replicate.models.get(model_owner, model_name);
}
```

### `replicate.models.list`

Get a paginated list of all public models.

```js
const response = await replicate.models.list();
```

```jsonc
{
"next": null,
"previous": null,
"results": [
{
"url": "https://replicate.com/replicate/hello-world",
"owner": "replicate",
"name": "hello-world",
"description": "A tiny model that says hello",
"visibility": "public",
"github_url": "https://github.com/replicate/cog-examples",
"paper_url": null,
"license_url": null,
"run_count": 5681081,
"cover_image_url": "...",
"default_example": {
/* ... */
},
"latest_version": {
/* ... */
}
}
]
}
```

### `replicate.models.versions.list`

Get a list of all published versions of a model, including input and output schemas for each version.
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ declare module 'replicate' {

models: {
get(model_owner: string, model_name: string): Promise<Model>;
list(): Promise<Page<Model>>;
versions: {
list(model_owner: string, model_name: string): Promise<ModelVersion[]>;
get(
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Replicate {

this.models = {
get: models.get.bind(this),
list: models.list.bind(this),
versions: {
list: models.versions.list.bind(this),
get: models.versions.get.bind(this),
Expand Down
26 changes: 25 additions & 1 deletion index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, jest, test } from '@jest/globals';
import Replicate, { ApiError, Prediction } from 'replicate';
import Replicate, { ApiError, Model, Prediction } from 'replicate';
import nock from 'nock';
import fetch from 'cross-fetch';

Expand Down Expand Up @@ -131,6 +131,30 @@ describe('Replicate client', () => {
// Add more tests for error handling, edge cases, etc.
});

describe('models.list', () => {
test('Paginates results', async () => {
nock(BASE_URL)
.get('/models')
.reply(200, {
results: [{ url: 'https://replicate.com/some-user/model-1' }],
next: 'https://api.replicate.com/v1/models?cursor=cD0yMDIyLTAxLTIxKzIzJTNBMTglM0EyNC41MzAzNTclMkIwMCUzQTAw',
})
.get('/models?cursor=cD0yMDIyLTAxLTIxKzIzJTNBMTglM0EyNC41MzAzNTclMkIwMCUzQTAw')
.reply(200, {
results: [{ url: 'https://replicate.com/some-user/model-2' }],
next: null,
});

const results: Model[] = [];
for await (const batch of client.paginate(client.models.list)) {
results.push(...batch);
}
expect(results).toEqual([{ url: 'https://replicate.com/some-user/model-1' }, { url: 'https://replicate.com/some-user/model-2' }]);

// Add more tests for error handling, edge cases, etc.
});
});

describe('predictions.create', () => {
test('Calls the correct API route with the correct payload', async () => {
nock(BASE_URL)
Expand Down
23 changes: 17 additions & 6 deletions lib/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,28 @@ async function listModelVersions(model_owner, model_name) {
* @returns {Promise<object>} Resolves with the model version data
*/
async function getModelVersion(model_owner, model_name, version_id) {
const response = await this.request(
`/models/${model_owner}/${model_name}/versions/${version_id}`,
{
method: 'GET',
}
);
const response = await this.request(`/models/${model_owner}/${model_name}/versions/${version_id}`, {
method: 'GET',
});

return response.json();
}

/**
* List all public models
*
* @returns {Promise<object>} Resolves with the model version data
*/
async function listModels() {
const response = await this.request('/models', {
method: 'GET',
});

return response.json();
}

module.exports = {
get: getModel,
list: listModels,
versions: { list: listModelVersions, get: getModelVersion },
};