Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add addDatasetElements function #99

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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