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 add_dataset_elements function #63

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
24 changes: 24 additions & 0 deletions gptscript/gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,30 @@ async def add_dataset_element(self, workspace_id: str, datasetID: str, elementNa
)
return DatasetElementMeta.model_validate_json(res)

async def add_dataset_elements(self, workspace_id: str, datasetID: str, elements: List[DatasetElement]) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

workspace_id should be optional

Suggested change
async def add_dataset_elements(self, workspace_id: str, datasetID: str, elements: List[DatasetElement]) -> str:
async def add_dataset_elements(self, datasetID: str, elements: List[DatasetElement], workspace_id: str = "") -> str:

if workspace_id == "":
workspace_id = os.environ["GPTSCRIPT_WORKSPACE_ID"]

if datasetID == "":
raise ValueError("datasetID cannot be empty")
elif not elements:
raise ValueError("elements cannot be empty")

res = await self._run_basic_command(
"datasets/add-elements",
{
"input": json.dumps({
"datasetID": datasetID,
"elements": [element.model_dump() for element in elements],
}),
"workspaceID": workspace_id,
"datasetToolRepo": self.opts.DatasetToolRepo,
"env": self.opts.Env
}
)
return res


async def list_dataset_elements(self, workspace_id: str, datasetID: str) -> List[DatasetElementMeta]:
if workspace_id == "":
workspace_id = os.environ["GPTSCRIPT_WORKSPACE_ID"]
Expand Down
31 changes: 25 additions & 6 deletions tests/test_gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from gptscript.confirm import AuthResponse
from gptscript.credentials import Credential
from gptscript.datasets import DatasetElement
from gptscript.exec_utils import get_env
from gptscript.frame import RunEventType, CallFrame, RunFrame, RunState, PromptFrame
from gptscript.gptscript import GPTScript
Expand Down Expand Up @@ -775,17 +776,35 @@ async def test_datasets(gptscript):
assert element_meta.name == "element1", "Expected element name to match"
assert element_meta.description == "element1 description", "Expected element description to match"

# Get the element
element = await gptscript.get_dataset_element(workspace_id, dataset.id, "element1")
assert element.name == "element1", "Expected element name to match"
assert element.contents == "element1 contents", "Expected element contents to match"
assert element.description == "element1 description", "Expected element description to match"
# Add two more elements
await gptscript.add_dataset_elements(workspace_id, dataset.id, [
DatasetElement(name="element2", contents="element2 contents", description="element2 description"),
DatasetElement(name="element3", contents="element3 contents", description="element3 description"),
])

# Get the elements
e1 = await gptscript.get_dataset_element(workspace_id, dataset.id, "element1")
assert e1.name == "element1", "Expected element name to match"
assert e1.contents == "element1 contents", "Expected element contents to match"
assert e1.description == "element1 description", "Expected element description to match"
e2 = await gptscript.get_dataset_element(workspace_id, dataset.id, "element2")
assert e2.name == "element2", "Expected element name to match"
assert e2.contents == "element2 contents", "Expected element contents to match"
assert e2.description == "element2 description", "Expected element description to match"
e3 = await gptscript.get_dataset_element(workspace_id, dataset.id, "element3")
assert e3.name == "element3", "Expected element name to match"
assert e3.contents == "element3 contents", "Expected element contents to match"
assert e3.description == "element3 description", "Expected element description to match"

# List elements in the dataset
elements = await gptscript.list_dataset_elements(workspace_id, dataset.id)
assert len(elements) == 1, "Expected one element in the dataset"
assert len(elements) == 3, "Expected one element in the dataset"
assert elements[0].name == "element1", "Expected element name to match"
assert elements[0].description == "element1 description", "Expected element description to match"
assert elements[1].name == "element2", "Expected element name to match"
assert elements[1].description == "element2 description", "Expected element description to match"
assert elements[2].name == "element3", "Expected element name to match"
assert elements[2].description == "element3 description", "Expected element description to match"

# List datasets
datasets = await gptscript.list_datasets(workspace_id)
Expand Down