Skip to content

Commit

Permalink
feat: add load method
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Aug 14, 2024
1 parent 67f6e5c commit cdc08a9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gptscript/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from gptscript.gptscript import GPTScript
from gptscript.confirm import AuthResponse
from gptscript.frame import RunFrame, CallFrame, PromptFrame
from gptscript.frame import RunFrame, CallFrame, PromptFrame, Program
from gptscript.opts import GlobalOptions
from gptscript.prompt import PromptResponse
from gptscript.run import Run, RunBasicCommand, Options
Expand Down
9 changes: 6 additions & 3 deletions gptscript/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ def __init__(self,
self.name = name
self.entryToolId = entryToolId
self.toolSet = toolSet
for tool in toolSet:
if isinstance(self.toolSet[tool], dict):
self.toolSet[tool] = Tool(**self.toolSet[tool])
if self.toolSet is None:
self.toolSet = {}
else:
for tool in toolSet:
if isinstance(self.toolSet[tool], dict):
self.toolSet[tool] = Tool(**self.toolSet[tool])


class RunFrame:
Expand Down
7 changes: 6 additions & 1 deletion gptscript/gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import requests

from gptscript.confirm import AuthResponse
from gptscript.frame import RunFrame, CallFrame, PromptFrame
from gptscript.frame import RunFrame, CallFrame, PromptFrame, Program
from gptscript.opts import GlobalOptions
from gptscript.prompt import PromptResponse
from gptscript.run import Run, RunBasicCommand, Options
Expand Down Expand Up @@ -94,6 +94,11 @@ def run(
"" if opts is None else opts.input
)

async def load(self, file_path: str) -> Program:
out = await self._run_basic_command("load", {"file": file_path})
parsed_nodes = json.loads(out)
return Program(**parsed_nodes.get("program", {}))

async def parse(self, file_path: str, disable_cache: bool = False) -> list[Text | Tool]:
out = await self._run_basic_command("parse", {"file": file_path, "disableCache": disable_cache})
parsed_nodes = json.loads(out)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_gptscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ async def test_eval_with_context(gptscript):

assert "Acorn Labs" == await run.text(), "Unexpected output from eval using context"

@pytest.mark.asyncio
async def test_load_simple_file(gptscript):
wd = os.getcwd()
prg = await gptscript.load(wd + "/tests/fixtures/test.gpt")
assert prg.toolSet[prg.entryToolId].instructions == "Who was the president of the United States in 1986?", \
"Unexpected output from parsing simple file"


@pytest.mark.asyncio
async def test_parse_simple_file(gptscript):
Expand Down

0 comments on commit cdc08a9

Please sign in to comment.