Skip to content

Add support for new deployment endpoints #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 19, 2024
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
30 changes: 28 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ declare module "replicate" {
created_by: Account;
configuration: {
hardware: string;
min_instances: number;
max_instances: number;
scaling: {
min_instances: number;
max_instances: number;
};
Comment on lines +36 to +39
Copy link
Contributor

Choose a reason for hiding this comment

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

This was incorrect in the original PR adding deployments.get #206

Copy link
Contributor

Choose a reason for hiding this comment

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

Was I out of touch? No, it was the OpenAPI specification that was wrong #232

};
};
}
Expand Down Expand Up @@ -194,6 +196,30 @@ declare module "replicate" {
deployment_owner: string,
deployment_name: string
): Promise<Deployment>;
create(deployment_config: {
name: string;
model: string;
version: string;
hardware: string;
min_instances: number;
max_instances: number;
}): Promise<Deployment>;
update(
deployment_owner: string,
deployment_name: string,
deployment_config: {
version?: string;
hardware?: string;
min_instances?: number;
max_instances?: number;
} & (
| { version: string }
| { hardware: string }
| { min_instances: number }
| { max_instances: number }
)
): Promise<Deployment>;
list(): Promise<Page<Deployment>>;
};

hardware: {
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class Replicate {

this.deployments = {
get: deployments.get.bind(this),
create: deployments.create.bind(this),
update: deployments.update.bind(this),
list: deployments.list.bind(this),
predictions: {
create: deployments.predictions.create.bind(this),
},
Expand Down
129 changes: 129 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,135 @@ describe("Replicate client", () => {
// Add more tests for error handling, edge cases, etc.
});

describe("deployments.create", () => {
test("Calls the correct API route with the correct payload", async () => {
nock(BASE_URL)
.post("/deployments")
.reply(200, {
owner: "acme",
name: "my-app-image-generator",
current_release: {
number: 1,
model: "stability-ai/sdxl",
version:
"da77bc59ee60423279fd632efb4795ab731d9e3ca9705ef3341091fb989b7eaf",
created_at: "2024-02-15T16:32:57.018467Z",
created_by: {
type: "organization",
username: "acme",
name: "Acme Corp, Inc.",
github_url: "https://github.com/acme",
},
configuration: {
hardware: "gpu-t4",
scaling: {
min_instances: 1,
max_instances: 5,
},
},
},
});

const deployment = await client.deployments.create({
name: "my-app-image-generator",
model: "stability-ai/sdxl",
version:
"da77bc59ee60423279fd632efb4795ab731d9e3ca9705ef3341091fb989b7eaf",
hardware: "gpu-t4",
min_instances: 1,
max_instances: 5,
});

expect(deployment.owner).toBe("acme");
expect(deployment.name).toBe("my-app-image-generator");
expect(deployment.current_release.model).toBe("stability-ai/sdxl");
});
// Add more tests for error handling, edge cases, etc.
});

describe("deployments.update", () => {
test("Calls the correct API route with the correct payload", async () => {
nock(BASE_URL)
.patch("/deployments/acme/my-app-image-generator")
.reply(200, {
owner: "acme",
name: "my-app-image-generator",
current_release: {
number: 2,
model: "stability-ai/sdxl",
version:
"632231d0d49d34d5c4633bd838aee3d81d936e59a886fbf28524702003b4c532",
created_at: "2024-02-16T08:14:22.345678Z",
created_by: {
type: "organization",
username: "acme",
name: "Acme Corp, Inc.",
github_url: "https://github.com/acme",
},
configuration: {
hardware: "gpu-a40-large",
scaling: {
min_instances: 3,
max_instances: 10,
},
},
},
});

const deployment = await client.deployments.update(
"acme",
"my-app-image-generator",
{
version:
"632231d0d49d34d5c4633bd838aee3d81d936e59a886fbf28524702003b4c532",
hardware: "gpu-a40-large",
min_instances: 3,
max_instances: 10,
}
);

expect(deployment.current_release.number).toBe(2);
expect(deployment.current_release.version).toBe(
"632231d0d49d34d5c4633bd838aee3d81d936e59a886fbf28524702003b4c532"
);
expect(deployment.current_release.configuration.hardware).toBe(
"gpu-a40-large"
);
expect(
deployment.current_release.configuration.scaling?.min_instances
).toBe(3);
expect(
deployment.current_release.configuration.scaling?.max_instances
).toBe(10);
});
// Add more tests for error handling, edge cases, etc.
});

describe("deployments.list", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL)
.get("/deployments")
.reply(200, {
next: null,
previous: null,
results: [
{
owner: "acme",
name: "my-app-image-generator",
current_release: {
// ...
},
},
// ...
],
});

const deployments = await client.deployments.list();
expect(deployments.results.length).toBe(1)
});
// Add more tests for pagination, error handling, edge cases, etc.
});

describe("predictions.create with model", () => {
test("Calls the correct API route with the correct payload", async () => {
nock(BASE_URL)
Expand Down
73 changes: 73 additions & 0 deletions lib/deployments.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,82 @@ async function getDeployment(deployment_owner, deployment_name) {
return response.json();
}

/**
* @typedef {Object} DeploymentCreateRequest - Request body for `deployments.create`
* @property {string} name - the name of the deployment
* @property {string} model - the full name of the model that you want to deploy e.g. stability-ai/sdxl
* @property {string} version - the 64-character string ID of the model version that you want to deploy
* @property {string} hardware - the SKU for the hardware used to run the model, via `replicate.hardware.list()`
* @property {number} min_instances - the minimum number of instances for scaling
* @property {number} max_instances - the maximum number of instances for scaling
*/

/**
* Create a deployment
*
* @param {DeploymentCreateRequest} config - Required. The deployment config.
* @returns {Promise<object>} Resolves with the deployment data
*/
async function createDeployment(deployment_config) {
const response = await this.request("/deployments", {
method: "POST",
data: deployment_config,
});

return response.json();
}

/**
* @typedef {Object} DeploymentUpdateRequest - Request body for `deployments.update`
* @property {string} version - the 64-character string ID of the model version that you want to deploy
* @property {string} hardware - the SKU for the hardware used to run the model, via `replicate.hardware.list()`
* @property {number} min_instances - the minimum number of instances for scaling
* @property {number} max_instances - the maximum number of instances for scaling
*/

/**
* Update an existing deployment
*
* @param {string} deployment_owner - Required. The username of the user or organization who owns the deployment
* @param {string} deployment_name - Required. The name of the deployment
* @param {DeploymentUpdateRequest} deployment_config - Required. The deployment changes.
* @returns {Promise<object>} Resolves with the deployment data
*/
async function updateDeployment(
deployment_owner,
deployment_name,
deployment_config
) {
const response = await this.request(
`/deployments/${deployment_owner}/${deployment_name}`,
{
method: "PATCH",
data: deployment_config,
}
);

return response.json();
}

/**
* List all deployments
*
* @returns {Promise<object>} - Resolves with a page of deployments
*/
async function listDeployments() {
const response = await this.request("/deployments", {
method: "GET",
});

return response.json();
}

module.exports = {
predictions: {
create: createPrediction,
},
get: getDeployment,
create: createDeployment,
update: updateDeployment,
list: listDeployments,
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"strict": true,
"allowJs": true
},
"exclude": ["integration/**", "**/node_modules"]
"exclude": ["**/node_modules", "integration"]
}