Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 49125f8

Browse files
committedNov 5, 2024··
workspaceID updates
Signed-off-by: Grant Linville <grant@acorn.io>
1 parent de3018a commit 49125f8

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed
 

‎src/gptscript.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,16 @@ export class GPTScript {
387387
}
388388

389389
// returns an array of dataset IDs
390-
async listDatasets(): Promise<Array<string>> {
390+
async listDatasets(): Promise<Array<DatasetMeta>> {
391391
const result = await this.runBasicCommand("datasets", {
392-
input: JSON.stringify({"workspaceID": process.env.GPTSCRIPT_WORKSPACE_ID}),
392+
input: "{}",
393393
datasetTool: this.opts.DatasetTool ?? "",
394394
env: this.opts.Env
395395
})
396-
return JSON.parse(result) as Array<string>
396+
return JSON.parse(result) as Array<DatasetMeta>
397397
}
398398

399-
async addDatasetElements(elements: Array<DatasetElement>, datasetID?: string) {
399+
async addDatasetElements(elements: Array<DatasetElement>, opts: {name?: string, description?: string, datasetID?: string}): Promise<string> {
400400
const serializableElements = elements.map(e => {
401401
return {
402402
name: e.name,
@@ -408,18 +408,19 @@ export class GPTScript {
408408

409409
return await this.runBasicCommand("datasets/add-elements", {
410410
input: JSON.stringify({
411-
workspaceID: process.env.GPTSCRIPT_WORKSPACE_ID,
412-
datasetID: datasetID ?? "",
411+
name: opts.name ?? "",
412+
description: opts.description ?? "",
413+
datasetID: opts.datasetID ?? "",
413414
elements: serializableElements
414415
}),
415416
datasetTool: this.opts.DatasetTool ?? "",
416-
env: this.opts.Env,
417+
env: this.opts.Env
417418
})
418419
}
419420

420421
async listDatasetElements(datasetID: string): Promise<Array<DatasetElementMeta>> {
421422
const result = await this.runBasicCommand("datasets/list-elements", {
422-
input: JSON.stringify({workspaceID: process.env.GPTSCRIPT_WORKSPACE_ID, datasetID}),
423+
input: JSON.stringify({datasetID}),
423424
datasetTool: this.opts.DatasetTool ?? "",
424425
env: this.opts.Env
425426
})
@@ -428,7 +429,7 @@ export class GPTScript {
428429

429430
async getDatasetElement(datasetID: string, elementName: string): Promise<DatasetElement> {
430431
const result = await this.runBasicCommand("datasets/get-element", {
431-
input: JSON.stringify({workspaceID: process.env.GPTSCRIPT_WORKSPACE_ID, datasetID, name: elementName}),
432+
input: JSON.stringify({datasetID, name: elementName}),
432433
datasetTool: this.opts.DatasetTool ?? "",
433434
env: this.opts.Env
434435
})
@@ -1265,6 +1266,12 @@ function jsonToCredential(cred: string): Credential {
12651266
}
12661267
}
12671268

1269+
export interface DatasetMeta {
1270+
id: string
1271+
name: string
1272+
description: string
1273+
}
1274+
12681275
export interface DatasetElementMeta {
12691276
name: string
12701277
description: string

‎tests/gptscript.test.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -887,12 +887,17 @@ describe("gptscript module", () => {
887887
}, 20000)
888888

889889
test("dataset operations", async () => {
890-
process.env.GPTSCRIPT_WORKSPACE_ID = await g.createWorkspace("directory")
890+
const workspaceID = await g.createWorkspace("directory")
891+
const client = new gptscript.GPTScript({
892+
APIKey: process.env.OPENAI_API_KEY,
893+
Env: ["GPTSCRIPT_WORKSPACE_ID=" + workspaceID]
894+
})
895+
891896
let datasetID: string
892897

893898
// Create and add two elements
894899
try {
895-
datasetID = await g.addDatasetElements([
900+
datasetID = await client.addDatasetElements([
896901
{
897902
name: "element1",
898903
description: "",
@@ -903,37 +908,37 @@ describe("gptscript module", () => {
903908
description: "a description",
904909
binaryContents: Buffer.from("this is element 2 contents")
905910
}
906-
])
911+
], {name: "test-dataset", description: "a test dataset"})
907912
} catch (e) {
908913
throw new Error("failed to create dataset: " + e)
909914
}
910915

911916
// Add another element
912917
try {
913-
await g.addDatasetElements([
918+
await client.addDatasetElements([
914919
{
915920
name: "element3",
916921
description: "a description",
917922
contents: "this is element 3 contents"
918923
}
919-
], datasetID)
924+
], {datasetID: datasetID})
920925
} catch (e) {
921926
throw new Error("failed to add elements: " + e)
922927
}
923928

924929
// Get elements
925930
try {
926-
const e1 = await g.getDatasetElement(datasetID, "element1")
931+
const e1 = await client.getDatasetElement(datasetID, "element1")
927932
expect(e1.name).toEqual("element1")
928933
expect(e1.description).toEqual("")
929934
expect(e1.contents).toEqual("this is element 1 contents")
930935

931-
const e2 = await g.getDatasetElement(datasetID, "element2")
936+
const e2 = await client.getDatasetElement(datasetID, "element2")
932937
expect(e2.name).toEqual("element2")
933938
expect(e2.description).toEqual("a description")
934939
expect(e2.binaryContents).toEqual(Buffer.from("this is element 2 contents"))
935940

936-
const e3 = await g.getDatasetElement(datasetID, "element3")
941+
const e3 = await client.getDatasetElement(datasetID, "element3")
937942
expect(e3.name).toEqual("element3")
938943
expect(e3.description).toEqual("a description")
939944
expect(e3.contents).toEqual("this is element 3 contents")
@@ -943,7 +948,7 @@ describe("gptscript module", () => {
943948

944949
// List the elements in the dataset
945950
try {
946-
const elements = await g.listDatasetElements(datasetID)
951+
const elements = await client.listDatasetElements(datasetID)
947952
expect(elements.length).toEqual(3)
948953
expect(elements.map(e => e.name)).toContain("element1")
949954
expect(elements.map(e => e.name)).toContain("element2")
@@ -954,9 +959,11 @@ describe("gptscript module", () => {
954959

955960
// List datasets
956961
try {
957-
const datasets = await g.listDatasets()
962+
const datasets = await client.listDatasets()
958963
expect(datasets.length).toBeGreaterThan(0)
959-
expect(datasets).toContain(datasetID)
964+
expect(datasets[0].id).toEqual(datasetID)
965+
expect(datasets[0].name).toEqual("test-dataset")
966+
expect(datasets[0].description).toEqual("a test dataset")
960967
} catch (e) {
961968
throw new Error("failed to list datasets: " + e)
962969
}

0 commit comments

Comments
 (0)
Please sign in to comment.