Skip to content

Commit

Permalink
feat(v2): SDK update and interface overhaul (#32)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Pilar <tomas.pilar@ibm.com>
  • Loading branch information
pilartomas authored Mar 12, 2024
1 parent 386ac9a commit 83d5b48
Show file tree
Hide file tree
Showing 53 changed files with 1,273 additions and 1,265 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ibm-generative-ai/cli",
"version": "1.3.2",
"version": "2.0.0",
"description": "CLI for IBM Generative AI (Tech Preview)",
"keywords": [
"ai",
Expand All @@ -9,7 +9,7 @@
"ibm",
"cli"
],
"homepage": "https://workbench.res.ibm.com",
"homepage": "https://bam.res.ibm.com",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -32,7 +32,7 @@
},
"packageManager": "yarn@3.5.0",
"engines": {
"node": ">=16.10.0"
"node": ">=18.12.0"
},
"lint-staged": {
"*.js": [
Expand All @@ -44,7 +44,7 @@
]
},
"dependencies": {
"@ibm-generative-ai/node-sdk": "^1.4.2",
"@ibm-generative-ai/node-sdk": "^2.0.0",
"dayjs": "^1.11.9",
"lodash": "^4.17.21",
"stream-json": "^1.7.5",
Expand All @@ -53,9 +53,9 @@
"zod": "^3.22.2"
},
"devDependencies": {
"eslint": "^8.40.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-import": "^2.29.1",
"husky": "^8.0.0",
"prettier": "^2.8.8"
}
Expand Down
14 changes: 14 additions & 0 deletions src/commands/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { promisify } from "node:util";

import { DEFAULT_ENDPOINT } from "../../utils/constants.js";
import { mergeConfig } from "../../utils/config.js";
import { isValidFormat } from "../../utils/formatters.js";

export const defaultCommandDefinition = [
"$0",
Expand Down Expand Up @@ -48,6 +49,19 @@ export const defaultCommandDefinition = [
config.credentials.apiKey = apiKey;
}
}
const outputFormat = await question(
`Default output format [choices: "json", "yaml"] (${
args.outputFormat ?? "none"
}): `
);
if (outputFormat && isValidFormat(outputFormat)) {
if (args.profile) {
config.configuration.profiles[args.profile]["output-format"] =
outputFormat;
} else {
config.configuration["output-format"] = outputFormat;
}
}

rl.close();
mergeConfig(config);
Expand Down
6 changes: 2 additions & 4 deletions src/commands/config/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ export const profilesCommandDefinition = [
"profiles",
"List configuration profiles",
{},
() => {
(args) => {
const profiles = allProfiles();
profiles.forEach((profile) => {
console.log(profile);
});
args.print(profiles);
},
];
3 changes: 1 addition & 2 deletions src/commands/config/show.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { loadProfileConfig } from "../../utils/config.js";
import { prettyPrint } from "../../utils/print.js";

export const showCommandDefinition = [
"show",
"Show configuration",
{},
(args) => {
const config = loadProfileConfig(args.profile);
prettyPrint(config);
args.print(config);
},
];
26 changes: 14 additions & 12 deletions src/commands/files/upload.js → src/commands/files/create.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { createReadStream } from "node:fs";
import path from "node:path";

import { FilePurposeSchema } from "@ibm-generative-ai/node-sdk";

import { groupOptions } from "../../utils/yargs.js";
import { prettyPrint } from "../../utils/print.js";

export const uploadCommandDefinition = [
"upload <file>",
export const createCommandDefinition = [
"create <file>",
"Upload a file",
(yargs) =>
yargs
Expand All @@ -23,7 +20,6 @@ export const uploadCommandDefinition = [
description: "Purpose of the file",
requiresArg: true,
demandOption: true,
choices: FilePurposeSchema.options,
},
name: {
alias: "n",
Expand All @@ -34,11 +30,17 @@ export const uploadCommandDefinition = [
})
),
async (args) => {
const { id, file_name, purpose, created_at } = await args.client.file({
purpose: args.purpose,
filename: args.name ?? path.parse(args.file).base,
file: createReadStream(args.file),
});
prettyPrint({ id, name: file_name, purpose, created_at });
const { purpose, name, file } = args;
const output = await args.client.file.create(
{
purpose,
file: {
name: name ?? path.parse(file).base,
content: new Blob(await createReadStream(file).toArray()),
},
},
{ signal: args.timeout }
);
args.print(output);
},
];
2 changes: 1 addition & 1 deletion src/commands/files/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export const deleteCommandDefinition = [
description: "Identifier of the file to be deleted",
}),
async (args) => {
await args.client.file({ id: args.id }, { delete: true });
await args.client.file.delete({ id: args.id }, { signal: args.timeout });
},
];
18 changes: 9 additions & 9 deletions src/commands/files/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { clientMiddleware } from "../../middleware/client.js";

import { deleteCommandDefinition } from "./delete.js";
import { downloadCommandDefinition } from "./download.js";
import { infoCommandDefinition } from "./info.js";
import { readCommandDefinition } from "./read.js";
import { retrieveCommandDefinition } from "./retrieve.js";
import { listCommandDefinition } from "./list.js";
import { uploadCommandDefinition } from "./upload.js";
import { createCommandDefinition } from "./create.js";

export const filesCommandDefinition = [
"files",
"Upload and manage files",
"file",
"Upload, download and manage files",
(yargs) =>
yargs
.middleware(clientMiddleware)
.command(...listCommandDefinition)
.command(...infoCommandDefinition)
.command(...uploadCommandDefinition)
.command(...downloadCommandDefinition)
.command(...createCommandDefinition)
.command(...retrieveCommandDefinition)
.command(...readCommandDefinition)
.command(...deleteCommandDefinition)
.command(...listCommandDefinition)
.demandCommand(1, 1, "Please choose a command"),
];
17 changes: 0 additions & 17 deletions src/commands/files/info.js

This file was deleted.

25 changes: 17 additions & 8 deletions src/commands/files/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FilePurposeSchema } from "@ibm-generative-ai/node-sdk";

import { paginate } from "../../utils/paginate.js";
import { groupOptions } from "../../utils/yargs.js";

export const listCommandDefinition = [
Expand All @@ -11,14 +10,24 @@ export const listCommandDefinition = [
type: "array",
description: "Filter listed by purpose",
requiresArg: true,
choices: FilePurposeSchema.options,
},
}),
async (args) => {
for await (const file of args.client.files()) {
if (!args.purpose || args.purpose.includes(file.purpose)) {
console.log(`${file.id} (${file.file_name})`);
}
}
const { purpose } = args;
await paginate(async ({ offset, limit }) => {
const output = await args.client.file.list(
{
offset,
limit,
purpose,
},
{ signal: args.timeout }
);
args.print(output);
return {
totalCount: output.totalCount,
itemsCount: output.results.length,
};
});
},
];
20 changes: 12 additions & 8 deletions src/commands/files/download.js → src/commands/files/read.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { stdout } from "node:process";
import { createWriteStream } from "node:fs";
import { pipeline } from "node:stream/promises";
import { Readable } from "node:stream";

import { groupOptions } from "../../utils/yargs.js";

export const downloadCommandDefinition = [
"download <id>",
"Download a file",
export const readCommandDefinition = [
"read <id>",
"Read file contents",
(yargs) =>
yargs
.positional("id", {
type: "string",
description: "Identifier of the file to download",
description: "Identifier of the file to read",
})
.options(
groupOptions({
Expand All @@ -26,10 +27,13 @@ export const downloadCommandDefinition = [
})
),
async (args) => {
const { download } = await args.client.file({
id: args.id,
});
const readable = await download();
const blob = await args.client.file.read(
{
id: args.id,
},
{ signal: args.timeout }
);
const readable = Readable.fromWeb(blob.stream());
await pipeline(readable, args.output ?? stdout);
},
];
18 changes: 18 additions & 0 deletions src/commands/files/retrieve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const retrieveCommandDefinition = [
"retrieve <id>",
"Show detailed information about a file",
(yargs) =>
yargs.positional("id", {
type: "string",
description: "Identifier of the file",
}),
async (args) => {
const output = await args.client.file.retrieve(
{
id: args.id,
},
{ signal: args.timeout }
);
args.print(output);
},
];
94 changes: 0 additions & 94 deletions src/commands/generate/config.js

This file was deleted.

Loading

0 comments on commit 83d5b48

Please sign in to comment.