Skip to content

chore: update for dataset rewrite #102

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

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
workspaceID updates
Signed-off-by: Grant Linville <grant@acorn.io>
  • Loading branch information
g-linville committed Nov 5, 2024
commit 49125f86e4a0c2af02b85373fdf647699dfcd58b
25 changes: 16 additions & 9 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,16 @@ export class GPTScript {
}

// returns an array of dataset IDs
async listDatasets(): Promise<Array<string>> {
async listDatasets(): Promise<Array<DatasetMeta>> {
const result = await this.runBasicCommand("datasets", {
input: JSON.stringify({"workspaceID": process.env.GPTSCRIPT_WORKSPACE_ID}),
input: "{}",
datasetTool: this.opts.DatasetTool ?? "",
env: this.opts.Env
})
return JSON.parse(result) as Array<string>
return JSON.parse(result) as Array<DatasetMeta>
}

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

return await this.runBasicCommand("datasets/add-elements", {
input: JSON.stringify({
workspaceID: process.env.GPTSCRIPT_WORKSPACE_ID,
datasetID: datasetID ?? "",
name: opts.name ?? "",
description: opts.description ?? "",
datasetID: opts.datasetID ?? "",
elements: serializableElements
}),
datasetTool: this.opts.DatasetTool ?? "",
env: this.opts.Env,
env: this.opts.Env
})
}

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

async getDatasetElement(datasetID: string, elementName: string): Promise<DatasetElement> {
const result = await this.runBasicCommand("datasets/get-element", {
input: JSON.stringify({workspaceID: process.env.GPTSCRIPT_WORKSPACE_ID, datasetID, name: elementName}),
input: JSON.stringify({datasetID, name: elementName}),
datasetTool: this.opts.DatasetTool ?? "",
env: this.opts.Env
})
Expand Down Expand Up @@ -1265,6 +1266,12 @@ function jsonToCredential(cred: string): Credential {
}
}

export interface DatasetMeta {
id: string
name: string
description: string
}

export interface DatasetElementMeta {
name: string
description: string
Expand Down
29 changes: 18 additions & 11 deletions tests/gptscript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,12 +887,17 @@ describe("gptscript module", () => {
}, 20000)

test("dataset operations", async () => {
process.env.GPTSCRIPT_WORKSPACE_ID = await g.createWorkspace("directory")
const workspaceID = await g.createWorkspace("directory")
const client = new gptscript.GPTScript({
APIKey: process.env.OPENAI_API_KEY,
Env: ["GPTSCRIPT_WORKSPACE_ID=" + workspaceID]
})

let datasetID: string

// Create and add two elements
try {
datasetID = await g.addDatasetElements([
datasetID = await client.addDatasetElements([
{
name: "element1",
description: "",
Expand All @@ -903,37 +908,37 @@ describe("gptscript module", () => {
description: "a description",
binaryContents: Buffer.from("this is element 2 contents")
}
])
], {name: "test-dataset", description: "a test dataset"})
} catch (e) {
throw new Error("failed to create dataset: " + e)
}

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

// Get elements
try {
const e1 = await g.getDatasetElement(datasetID, "element1")
const e1 = await client.getDatasetElement(datasetID, "element1")
expect(e1.name).toEqual("element1")
expect(e1.description).toEqual("")
expect(e1.contents).toEqual("this is element 1 contents")

const e2 = await g.getDatasetElement(datasetID, "element2")
const e2 = await client.getDatasetElement(datasetID, "element2")
expect(e2.name).toEqual("element2")
expect(e2.description).toEqual("a description")
expect(e2.binaryContents).toEqual(Buffer.from("this is element 2 contents"))

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

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

// List datasets
try {
const datasets = await g.listDatasets()
const datasets = await client.listDatasets()
expect(datasets.length).toBeGreaterThan(0)
expect(datasets).toContain(datasetID)
expect(datasets[0].id).toEqual(datasetID)
expect(datasets[0].name).toEqual("test-dataset")
expect(datasets[0].description).toEqual("a test dataset")
} catch (e) {
throw new Error("failed to list datasets: " + e)
}
Expand Down
Loading