Skip to content

Commit

Permalink
refactor ai commands
Browse files Browse the repository at this point in the history
  • Loading branch information
RamIdeas committed Sep 9, 2024
1 parent c50f326 commit d1f4b0b
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 81 deletions.
22 changes: 22 additions & 0 deletions packages/wrangler/src/ai/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineNamespace } from "../core";
import "./listCatalog";
import "./createFinetune";
import "./listFinetune";

defineNamespace({
command: "wrangler ai",
metadata: {
description: "🤖 Manage AI models\n", // TODO: remove \n when yargs --help hack is replaced with full reimplementation
status: "stable",
owner: "Product: AI",
},
});

defineNamespace({
command: "wrangler ai finetune",
metadata: {
description: "Interact with finetune files",
status: "stable",
owner: "Product: AI",
},
});
40 changes: 21 additions & 19 deletions packages/wrangler/src/ai/createFinetune.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,44 @@ import fs from "fs";
import path from "path";
import { FormData } from "undici";
import { fetchResult } from "../cfetch";
import { withConfig } from "../config";
import { defineCommand } from "../core";
import { logger } from "../logger";
import { requireAuth } from "../user";
import { getErrorMessage } from "./utils";
import type { Message } from "../parse";
import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../yargs-types";
import type { Finetune } from "./types";

const requiredAssets = ["adapter_config.json", "adapter_model.safetensors"];

type HandlerOptions = StrictYargsOptionsToInterface<typeof options>;
defineCommand({
command: "wrangler ai finetune create",

export function options(yargs: CommonYargsArgv) {
return yargs
.positional("model_name", {
metadata: {
description: "Create finetune and upload assets",
status: "stable",
owner: "Product: AI",
},

args: {
model_name: {
describe: "The catalog model name",
type: "string",
demandOption: true,
})
.positional("finetune_name", {
},
finetune_name: {
describe: "The finetune name",
type: "string",
demandOption: true,
})
.positional("folder_path", {
},
folder_path: {
describe: "The folder path containing the finetune assets",
type: "string",
demandOption: true,
});
}
},
},
positionalArgs: ["model_name", "finetune_name", "folder_path"],

export const handler = withConfig<HandlerOptions>(
async ({ finetune_name, model_name, folder_path, config }): Promise<void> => {
async handler({ finetune_name, model_name, folder_path }, { config }) {
const accountId = await requireAuth(config);

logger.log(
Expand Down Expand Up @@ -113,5 +115,5 @@ export const handler = withConfig<HandlerOptions>(
`🚨 Folder does not exist: ${getErrorMessage(e as Message)}`
);
}
}
);
},
});
30 changes: 0 additions & 30 deletions packages/wrangler/src/ai/index.ts

This file was deleted.

30 changes: 16 additions & 14 deletions packages/wrangler/src/ai/listCatalog.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { withConfig } from "../config";
import { defineCommand, SharedArgs } from "../core";
import { logger } from "../logger";
import { requireAuth } from "../user";
import { asJson } from "../yargs-types";
import { listCatalogEntries, truncateDescription } from "./utils";
import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../yargs-types";

export function options(yargs: CommonYargsArgv) {
return asJson(yargs);
}
defineCommand({
command: "wrangler ai models",

type HandlerOptions = StrictYargsOptionsToInterface<typeof options>;
export const handler = withConfig<HandlerOptions>(
async ({ json, config }): Promise<void> => {
metadata: {
description: "List catalog models",
status: "stable",
owner: "Product: AI",
},

args: {
...SharedArgs.json,
},

async handler({ json }, { config }): Promise<void> {
const accountId = await requireAuth(config);
const entries = await listCatalogEntries(accountId);

Expand All @@ -40,5 +42,5 @@ export const handler = withConfig<HandlerOptions>(
);
}
}
}
);
},
});
30 changes: 16 additions & 14 deletions packages/wrangler/src/ai/listFinetune.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { withConfig } from "../config";
import { defineCommand, SharedArgs } from "../core";
import { logger } from "../logger";
import { requireAuth } from "../user";
import { asJson } from "../yargs-types";
import { listFinetuneEntries, truncateDescription } from "./utils";
import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../yargs-types";
import type { Finetune } from "./types";

export function options(yargs: CommonYargsArgv) {
return asJson(yargs);
}
defineCommand({
command: "wrangler ai finetune list",

type HandlerOptions = StrictYargsOptionsToInterface<typeof options>;
export const handler = withConfig<HandlerOptions>(
async ({ json, config }): Promise<void> => {
metadata: {
description: "List catalog models",
status: "stable",
owner: "Product: AI",
},

args: {
...SharedArgs.json,
},

async handler({ json }, { config }): Promise<void> {
const accountId = await requireAuth(config);
const entries = await listFinetuneEntries(accountId);

Expand All @@ -37,5 +39,5 @@ export const handler = withConfig<HandlerOptions>(
);
}
}
}
);
},
});
1 change: 1 addition & 0 deletions packages/wrangler/src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import "./auth/commands";
import "./kv/index";
import "./ai/commands";
2 changes: 2 additions & 0 deletions packages/wrangler/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { defineAlias, defineCommand, defineNamespace } from "./define-command";
export * as SharedArgs from "./shared-args";
9 changes: 9 additions & 0 deletions packages/wrangler/src/core/shared-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ArgDefinition } from "./define-command";

Check failure on line 1 in packages/wrangler/src/core/shared-args.ts

View workflow job for this annotation

GitHub Actions / Checks

All imports in the declaration are only used as types. Use `import type`

export const json: Record<string, ArgDefinition> = {
json: {
describe: "Return output as clean JSON",
type: "boolean",
default: false,
},
};
5 changes: 1 addition & 4 deletions packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import chalk from "chalk";
import { ProxyAgent, setGlobalDispatcher } from "undici";
import makeCLI from "yargs";
import { version as wranglerVersion } from "../package.json";
import { ai } from "./ai";
import { cloudchamber } from "./cloudchamber";
import { loadDotEnv } from "./config";
import { createCommandRegister } from "./core/register-commands";
Expand Down Expand Up @@ -593,9 +592,7 @@ export function createCLIParser(argv: string[]) {
);

// ai
wrangler.command("ai", "🤖 Manage AI models\n", (aiYargs) => {
return ai(aiYargs.command(subHelp));
});
register.registerNamespace("ai");

/******************** CMD GROUP ***********************/
// login
Expand Down

0 comments on commit d1f4b0b

Please sign in to comment.