Skip to content

Commit

Permalink
chore: add addDatasetElements function (#99)
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Linville <[email protected]>
  • Loading branch information
g-linville authored Oct 30, 2024
1 parent 306861b commit ea42fed
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,19 @@ export class GPTScript {
return JSON.parse(result) as DatasetElementMeta
}

async addDatasetElements(workspaceID: string, datasetID: string, elements: Array<DatasetElement>) {
if (workspaceID === "") {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
}

return await this.runBasicCommand("datasets/add-elements", {
input: JSON.stringify({datasetID, elements}),
workspaceID: workspaceID,
datasetToolRepo: this.opts.DatasetToolRepo ?? "",
env: this.opts.Env,
})
}

async listDatasetElements(workspaceID: string, datasetID: string): Promise<Array<DatasetElementMeta>> {
if (workspaceID == "") {
workspaceID = process.env.GPTSCRIPT_WORKSPACE_ID ?? ""
Expand Down
36 changes: 35 additions & 1 deletion tests/gptscript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,28 @@ describe("gptscript module", () => {
throw new Error("failed to add elements: " + e)
}

// Add two elements at once.
try {
await g.addDatasetElements(
workspaceID,
datasetID,
[
{
name: "element3",
description: "a description",
contents: "this is element 3 contents"
},
{
name: "element4",
description: "a description",
contents: "this is element 4 contents"
}
]
)
} catch (e) {
throw new Error("failed to add elements: " + e)
}

// Get elements
try {
const e1 = await g.getDatasetElement(workspaceID, datasetID, "element1")
Expand All @@ -940,16 +962,28 @@ describe("gptscript module", () => {
expect(e2.name).toEqual("element2")
expect(e2.description).toEqual("a description")
expect(e2.contents).toEqual("this is element 2 contents")

const e3 = await g.getDatasetElement(workspaceID, datasetID, "element3")
expect(e3.name).toEqual("element3")
expect(e3.description).toEqual("a description")
expect(e3.contents).toEqual("this is element 3 contents")

const e4 = await g.getDatasetElement(workspaceID, datasetID, "element4")
expect(e4.name).toEqual("element4")
expect(e4.description).toEqual("a description")
expect(e4.contents).toEqual("this is element 4 contents")
} catch (e) {
throw new Error("failed to get elements: " + e)
}

// List the elements in the dataset
try {
const elements = await g.listDatasetElements(workspaceID, datasetID)
expect(elements.length).toEqual(2)
expect(elements.length).toEqual(4)
expect(elements.map(e => e.name)).toContain("element1")
expect(elements.map(e => e.name)).toContain("element2")
expect(elements.map(e => e.name)).toContain("element3")
expect(elements.map(e => e.name)).toContain("element4")
} catch (e) {
throw new Error("failed to list elements: " + e)
}
Expand Down

0 comments on commit ea42fed

Please sign in to comment.