Skip to content

Commit d4b222c

Browse files
authored
chore: update for dataset rewrite (#102)
Signed-off-by: Grant Linville <[email protected]>
1 parent 56135fa commit d4b222c

File tree

2 files changed

+67
-149
lines changed

2 files changed

+67
-149
lines changed

src/gptscript.ts

+29-83
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface GlobalOpts {
1212
BaseURL?: string
1313
DefaultModel?: string
1414
DefaultModelProvider?: string
15-
DatasetToolRepo?: string
15+
DatasetTool?: string
1616
WorkspaceTool?: string
1717
Env?: string[]
1818
}
@@ -386,106 +386,60 @@ export class GPTScript {
386386
})
387387
}
388388

389-
// Dataset methods
390-
391-
async listDatasets(workspaceID: string): Promise<Array<DatasetMeta>> {
392-
if (workspaceID == "") {
393-
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
394-
}
395-
389+
// returns an array of dataset IDs
390+
async listDatasets(): Promise<Array<DatasetMeta>> {
396391
const result = await this.runBasicCommand("datasets", {
397-
workspaceID: workspaceID,
398-
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
392+
input: "{}",
393+
datasetTool: this.opts.DatasetTool ?? "",
399394
env: this.opts.Env
400395
})
401396
return JSON.parse(result) as Array<DatasetMeta>
402397
}
403398

404-
async createDataset(workspaceID: string, name: string, description: string): Promise<Dataset> {
405-
if (workspaceID == "") {
406-
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
407-
}
408-
409-
const result = await this.runBasicCommand("datasets/create", {
410-
input: JSON.stringify({datasetName: name, datasetDescription: description}),
411-
workspaceID: workspaceID,
412-
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
413-
env: this.opts.Env
414-
})
415-
return JSON.parse(result) as Dataset
416-
}
417-
418-
async addDatasetElement(workspaceID: string, datasetID: string, elementName: string, elementDescription: string, elementContent: ArrayBuffer): Promise<DatasetElementMeta> {
419-
if (workspaceID == "") {
420-
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
421-
}
422-
423-
const result = await this.runBasicCommand("datasets/add-element", {
424-
input: JSON.stringify({
425-
datasetID,
426-
elementName: elementName,
427-
elementDescription: elementDescription,
428-
elementContent: Buffer.from(elementContent).toString("base64")
429-
}),
430-
workspaceID: workspaceID,
431-
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
432-
env: this.opts.Env
433-
})
434-
return JSON.parse(result) as DatasetElementMeta
435-
}
436-
437-
async addDatasetElements(workspaceID: string, datasetID: string, elements: Array<DatasetElement>) {
438-
if (workspaceID === "") {
439-
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
440-
}
441-
399+
async addDatasetElements(elements: Array<DatasetElement>, opts: {name?: string, description?: string, datasetID?: string}): Promise<string> {
442400
const serializableElements = elements.map(e => {
443401
return {
444402
name: e.name,
445403
description: e.description,
446-
contents: Buffer.from(e.contents).toString("base64")
404+
contents: e.contents,
405+
binaryContents: Buffer.from(e.binaryContents ?? Buffer.from("")).toString("base64")
447406
}
448407
})
449408

450409
return await this.runBasicCommand("datasets/add-elements", {
451-
input: JSON.stringify({datasetID, elements: serializableElements}),
452-
workspaceID: workspaceID,
453-
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
454-
env: this.opts.Env,
410+
input: JSON.stringify({
411+
name: opts.name ?? "",
412+
description: opts.description ?? "",
413+
datasetID: opts.datasetID ?? "",
414+
elements: serializableElements
415+
}),
416+
datasetTool: this.opts.DatasetTool ?? "",
417+
env: this.opts.Env
455418
})
456419
}
457420

458-
async listDatasetElements(workspaceID: string, datasetID: string): Promise<Array<DatasetElementMeta>> {
459-
if (workspaceID == "") {
460-
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
461-
}
462-
421+
async listDatasetElements(datasetID: string): Promise<Array<DatasetElementMeta>> {
463422
const result = await this.runBasicCommand("datasets/list-elements", {
464423
input: JSON.stringify({datasetID}),
465-
workspaceID: workspaceID,
466-
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
424+
datasetTool: this.opts.DatasetTool ?? "",
467425
env: this.opts.Env
468426
})
469427
return JSON.parse(result) as Array<DatasetElementMeta>
470428
}
471429

472-
async getDatasetElement(workspaceID: string, datasetID: string, elementName: string): Promise<DatasetElement> {
473-
if (workspaceID == "") {
474-
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
475-
}
476-
430+
async getDatasetElement(datasetID: string, elementName: string): Promise<DatasetElement> {
477431
const result = await this.runBasicCommand("datasets/get-element", {
478-
input: JSON.stringify({datasetID, element: elementName}),
479-
workspaceID: workspaceID,
480-
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
432+
input: JSON.stringify({datasetID, name: elementName}),
433+
datasetTool: this.opts.DatasetTool ?? "",
481434
env: this.opts.Env
482435
})
483436

484437
const element = JSON.parse(result)
485438
return {
486439
name: element.name,
487440
description: element.description,
488-
contents: Buffer.from(element.contents, "base64")
441+
contents: element.contents,
442+
binaryContents: Buffer.from(element.binaryContents ?? "", "base64")
489443
}
490444
}
491445

@@ -1312,28 +1266,20 @@ function jsonToCredential(cred: string): Credential {
13121266
}
13131267
}
13141268

1315-
// Dataset types
1316-
1317-
export interface DatasetElementMeta {
1318-
name: string
1319-
description: string
1320-
}
1321-
1322-
export interface DatasetElement {
1269+
export interface DatasetMeta {
1270+
id: string
13231271
name: string
13241272
description: string
1325-
contents: ArrayBuffer
13261273
}
13271274

1328-
export interface DatasetMeta {
1329-
id: string
1275+
export interface DatasetElementMeta {
13301276
name: string
13311277
description: string
13321278
}
13331279

1334-
export interface Dataset {
1335-
id: string
1280+
export interface DatasetElement {
13361281
name: string
13371282
description: string
1338-
elements: Record<string, DatasetElementMeta>
1283+
contents?: string
1284+
binaryContents?: ArrayBuffer
13391285
}

tests/gptscript.test.ts

+38-66
Original file line numberDiff line numberDiff line change
@@ -887,112 +887,84 @@ describe("gptscript module", () => {
887887
}, 20000)
888888

889889
test("dataset operations", async () => {
890-
const datasetName = "test-" + randomBytes(10).toString("hex")
891-
const workspaceID = await g.createWorkspace("directory")
890+
process.env.GPTSCRIPT_WORKSPACE_ID = await g.createWorkspace("directory")
891+
892+
const client = new gptscript.GPTScript({
893+
APIKey: process.env.OPENAI_API_KEY,
894+
Env: Object.entries(process.env).map(([k, v]) => `${k}=${v}`)
895+
})
896+
892897
let datasetID: string
893898

894-
// Create
899+
// Create and add two elements
895900
try {
896-
const dataset = await g.createDataset(workspaceID, datasetName, "a test dataset")
897-
expect(dataset).toBeDefined()
898-
expect(dataset.name).toEqual(datasetName)
899-
expect(dataset.description).toEqual("a test dataset")
900-
expect(dataset.id.length).toBeGreaterThan(0)
901-
expect(dataset.elements).toEqual({})
902-
datasetID = dataset.id
901+
datasetID = await client.addDatasetElements([
902+
{
903+
name: "element1",
904+
description: "",
905+
contents: "this is element 1 contents"
906+
},
907+
{
908+
name: "element2",
909+
description: "a description",
910+
binaryContents: Buffer.from("this is element 2 contents")
911+
}
912+
], {name: "test-dataset", description: "a test dataset"})
903913
} catch (e) {
904914
throw new Error("failed to create dataset: " + e)
905915
}
906916

907-
// Add elements
908-
try {
909-
const e1 = await g.addDatasetElement(
910-
workspaceID,
911-
datasetID,
912-
"element1",
913-
"",
914-
Buffer.from("this is element 1 contents")
915-
)
916-
expect(e1.name).toEqual("element1")
917-
expect(e1.description).toEqual("")
918-
919-
const e2 = await g.addDatasetElement(
920-
workspaceID,
921-
datasetID,
922-
"element2",
923-
"a description",
924-
Buffer.from("this is element 2 contents")
925-
)
926-
expect(e2.name).toEqual("element2")
927-
expect(e2.description).toEqual("a description")
928-
} catch (e) {
929-
throw new Error("failed to add elements: " + e)
930-
}
931-
932-
// Add two elements at once.
917+
// Add another element
933918
try {
934-
await g.addDatasetElements(
935-
workspaceID,
936-
datasetID,
937-
[
919+
await client.addDatasetElements([
938920
{
939-
name: "element3",
940-
description: "a description",
941-
contents: Buffer.from("this is element 3 contents")
942-
},
943-
{
944-
name: "element4",
945-
description: "a description",
946-
contents: Buffer.from("this is element 4 contents")
921+
name: "element3",
922+
description: "a description",
923+
contents: "this is element 3 contents"
947924
}
948-
]
949-
)
925+
], {datasetID: datasetID})
950926
} catch (e) {
951927
throw new Error("failed to add elements: " + e)
952928
}
953929

954930
// Get elements
955931
try {
956-
const e1 = await g.getDatasetElement(workspaceID, datasetID, "element1")
932+
const e1 = await client.getDatasetElement(datasetID, "element1")
957933
expect(e1.name).toEqual("element1")
958934
expect(e1.description).toBeUndefined()
959-
expect(e1.contents).toEqual(Buffer.from("this is element 1 contents"))
935+
expect(e1.contents).toEqual("this is element 1 contents")
960936

961-
const e2 = await g.getDatasetElement(workspaceID, datasetID, "element2")
937+
const e2 = await client.getDatasetElement(datasetID, "element2")
962938
expect(e2.name).toEqual("element2")
963939
expect(e2.description).toEqual("a description")
964-
expect(e2.contents).toEqual(Buffer.from("this is element 2 contents"))
940+
expect(e2.binaryContents).toEqual(Buffer.from("this is element 2 contents"))
965941

966-
const e3 = await g.getDatasetElement(workspaceID, datasetID, "element3")
942+
const e3 = await client.getDatasetElement(datasetID, "element3")
967943
expect(e3.name).toEqual("element3")
968944
expect(e3.description).toEqual("a description")
969-
expect(e3.contents).toEqual(Buffer.from("this is element 3 contents"))
970-
971-
const e4 = await g.getDatasetElement(workspaceID, datasetID, "element4")
972-
expect(e4.name).toEqual("element4")
973-
expect(e4.description).toEqual("a description")
974-
expect(e4.contents).toEqual(Buffer.from("this is element 4 contents"))
945+
expect(e3.contents).toEqual("this is element 3 contents")
975946
} catch (e) {
976947
throw new Error("failed to get elements: " + e)
977948
}
978949

979950
// List the elements in the dataset
980951
try {
981-
const elements = await g.listDatasetElements(workspaceID, datasetID)
982-
expect(elements.length).toEqual(4)
952+
const elements = await client.listDatasetElements(datasetID)
953+
expect(elements.length).toEqual(3)
983954
expect(elements.map(e => e.name)).toContain("element1")
984955
expect(elements.map(e => e.name)).toContain("element2")
985956
expect(elements.map(e => e.name)).toContain("element3")
986-
expect(elements.map(e => e.name)).toContain("element4")
987957
} catch (e) {
988958
throw new Error("failed to list elements: " + e)
989959
}
990960

991961
// List datasets
992962
try {
993-
const datasets = await g.listDatasets(workspaceID)
963+
const datasets = await client.listDatasets()
994964
expect(datasets.length).toBeGreaterThan(0)
995-
expect(datasets.map(d => d.name)).toContain(datasetName)
965+
expect(datasets[0].id).toEqual(datasetID)
966+
expect(datasets[0].name).toEqual("test-dataset")
967+
expect(datasets[0].description).toEqual("a test dataset")
996968
} catch (e) {
997969
throw new Error("failed to list datasets: " + e)
998970
}

0 commit comments

Comments
 (0)